<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <div class="switch">
        <div class="switch_left">
          <ul>
            <li
              v-for="item in tabOptions"
              :key="item.key"
              :class="{ active: activeComp == item.key }"
              @click="activeComp = item.key"
            >
              {{ item.name }}
            </li>
          </ul>
        </div>
      </div>
      <div class="main" style="padding: 0 10px">
        <div v-if="activeComp == 'saleorder'">
          <div class="ele-border-lighter form-content" v-loading="loading">
            <search-table @search="reload"></search-table>

            <!-- 数据表格 -->
            <ele-pro-table
              ref="table"
              :columns="columns"
              :datasource="datasource"
              height="calc(100vh - 410px)"
              style="margin-bottom: 10px"
              full-height="calc(100vh - 116px)"
              tool-class="ele-toolbar-form"
              :selection.sync="selection"
              cache-key="eomContactPageTable"
            >
              <!-- 表头工具栏 -->
              <template v-slot:toolbar>
                <el-button
                  size="small"
                  type="primary"
                  icon="el-icon-plus"
                  class="ele-btn-icon"
                  @click="openEdit('add', {})"
                >
                  新建
                </el-button>

                <el-button
                  size="small"
                  type="danger"
                  el-icon-delete
                  class="ele-btn-icon"
                  @click="allDelBtn"
                  :disabled="selection?.length === 0"
                >
                  批量删除
                </el-button>
              </template>

              <!-- 查看详情列 -->
              <template v-slot:contractNo="{ row }">
                <el-link
                  type="primary"
                  :underline="false"
                  @click="opencontractDetail(row)"
                >
                  {{ row.contractNo }}
                </el-link
                >
              </template>
              <template v-slot:orderNo="{ row }">
                <el-link
                  type="primary"
                  :underline="false"
                  @click="openorderDetail(row)"
                >
                  {{ row.orderNo }}
                </el-link
                >
              </template>
              <!-- 操作列 -->
              <template v-slot:action="{ row }">
                <el-link
                  type="primary"
                  :underline="false"
                  icon="el-icon-edit"
                  @click="openEdit('edit', row)"
                  v-if="[0, 3].includes(row.orderStatus)"
                >
                  修改
                </el-link>
                <el-link
                  type="primary"
                  :underline="false"
                  icon="el-icon-plus"
                  @click="saleOrderSubmit(row)"
                  v-if="[0, 3].includes(row.orderStatus)"
                >
                  提交
                </el-link>

                <el-popconfirm
                  class="ele-action"
                  title="确定要删除此信息吗？"
                  @confirm="remove([row.id])"
                  v-if="[0, 3].includes(row.orderStatus)"
                >
                  <template v-slot:reference>
                    <el-link
                      type="danger"
                      :underline="false"
                      icon="el-icon-delete"
                    >
                      删除
                    </el-link>
                  </template>
                </el-popconfirm>
                <el-dropdown
                  @command="(command) => handleCommand(command, row)"
                  v-if="[2].includes(row.orderStatus)"
                >
                  <span class="el-dropdown-link">
                    操作菜单<i class="el-icon-arrow-down el-icon--right"></i>
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="invoice"
                    >创建发货单
                    </el-dropdown-item
                    >
                    <el-dropdown-item command="returnOrder"
                    >创建退货单
                    </el-dropdown-item
                    >
                  </el-dropdown-menu>
                </el-dropdown>
              </template>
            </ele-pro-table>
          </div>
        </div>
        <div v-if="activeComp == 'invoice'">
          <invoice></invoice>
        </div>
        <div v-if="activeComp == 'entrustedReceive'">
          <entrustedReceive></entrustedReceive>
        </div>
        <div v-if="activeComp == 'returnorder'" class="returnorder">
          <return-goods></return-goods>
        </div>
        <div v-if="activeComp == 'accountstatement'">
          <accountstatement></accountstatement>
        </div>
        <div v-if="activeComp == 'customerReturnOrder'">
          <customerReturnOrder></customerReturnOrder>
        </div>
      </div>
    </el-card>

    <add-dialog ref="addDialogRef" @done="reload"></add-dialog>
    <add-invoice-dialog
      ref="invoiceDialogRef"
      @done="reload"
    ></add-invoice-dialog>
    <add-return-goods-dialog
      ref="addReturnGoodsRef"
      @done="reload"
    ></add-return-goods-dialog>
    <contractr-detail ref="contractDetailRef"></contractr-detail>
    <detail-dialog ref="contactDetailDialogRef"></detail-dialog>
    <!-- 多选删除弹窗 -->
    <pop-modal
      :visible.sync="delVisible"
      content="是否确定删除？"
      @done="commitBtn"
    />
  </div>
</template>

<script>
import searchTable from './components/searchTable.vue';
import addDialog from './components/addDialog.vue';
import invoice from './invoice/index.vue';
import detailDialog from './components/detailDialog.vue';
import contractrDetail from '@/views/contractManage/contractBook/components/detailDialog.vue';
import addInvoiceDialog from '@/views/saleManage/saleOrder/invoice/components/addInvoiceDialog';
import addReturnGoodsDialog from '@/views/saleManage/saleOrder/returnGoods/components/addReturnGoodsDialog';
import popModal from '@/components/pop-modal';
import accountstatement from './accountstatement/index.vue';
import customerReturnOrder from './customerReturnOrder/index.vue';
import returnGoods from './returnGoods/index.vue';
import {getTableList, deleteInformation, submit} from '@/api/saleManage/saleorder';
import dictMixins from '@/mixins/dictMixins';
import {reviewStatus} from '@/enum/dict';
import entrustedReceive from "@/views/saleManage/saleOrder/entrustedReceive/index.vue";

export default {
  mixins: [dictMixins],
  components: {
    searchTable,
    returnGoods,
    accountstatement,
    popModal,
    contractrDetail,
    addReturnGoodsDialog,
    addInvoiceDialog,
    invoice,
    addDialog,
    detailDialog,
    customerReturnOrder,
    entrustedReceive
  },
  data() {
    return {
      activeComp: 'saleorder',
      tabOptions: [
        {key: 'saleorder', name: '销售订单'},
        {key: 'entrustedReceive', name: '受托收货单'},
        {key: 'invoice', name: '发货单'},
        {key: 'returnorder', name: '退货单'},
        {key: 'customerReturnOrder', name: '退货处理单'},
        {key: 'accountstatement', name: '对账单'}
      ],
      selection: [], //单选中集合
      delVisible: false, //批量删除弹框状态
      loading: false, // 加载状态
      columns: [
        {
          width: 45,
          type: 'selection',
          columnKey: 'selection',
          align: 'center'
        },
        {
          columnKey: 'index',
          label: '序号',
          type: 'index',
          width: 55,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          prop: 'orderNo',
          label: '订单编码',
          align: 'center',
          slot: 'orderNo',
          showOverflowTooltip: true,
          minWidth: 200
        },

        // {
        //   prop: 'deliveryDate',
        //   label: '交货日期',
        //   align: 'center',
        //   showOverflowTooltip: true,
        //   minWidth: 200
        // },
        {
          prop: 'productNames',
          label: '产品名称',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 140
        },
        {
          prop: 'saleTypeName',
          label: '销售类型',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 140
        },
        {
          prop: 'partaName',
          label: '客户名称',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 180
        },
        {
          prop: 'partaLinkName',
          label: '客户联系人',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 130
        },

        {
          prop: 'partaTel',
          label: '客户联系电话',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 130
        },
        {
          prop: 'partbName',
          label: '售出方名称',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 130
        },
        {
          prop: 'partbLinkName',
          label: '售出方联系人',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 130
        },
        {
          prop: 'partbTel',
          label: '售出方联系电话',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 130
        },
        {
          prop: 'payAmount',
          label: '金额(元)',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 170
        },

        {
          prop: 'orderStatus',
          label: '审核状态',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
          formatter: (_row, _column, cellValue) => {
            return reviewStatus[_row.orderStatus];
          }
        },
        {
          prop: 'contractNo',
          label: '合同编码',
          align: 'center',
          slot: 'contractNo',
          showOverflowTooltip: true,
          minWidth: 200
        },
        {
          prop: 'contractNumber',
          label: '合同编号',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 200
        },
        {
          prop: 'createTime',
          label: '创建时间',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 170
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 230,
          align: 'center',
          resizable: false,
          slot: 'action',
          showOverflowTooltip: true,
          fixed: 'right'
        }
      ]
    };
  },
  computed: {},
  created() {
    this.requestDict('客户状态');
  },
  methods: {
    //更多菜单
    handleCommand(command, row) {
      if (command === 'invoice') {
        this.$refs.invoiceDialogRef.open('add', {}, row.id);
      }
      if (command === 'returnOrder') {
        this.$refs.addReturnGoodsRef.open('add', {});
      }
    },
    //点击左边分类
    handleNodeClick(data, node) {
      // this.curNodeData = data;
      this.reload({categoryId: data.id});
    },
    /* 表格数据源 */
    datasource({page, limit, where, order}) {
      return getTableList({
        pageNum: page,
        size: limit,
        ...where
      });
    },

    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({page: 1, where});
    },

    //新增编辑
    openEdit(type, row) {
      this.$refs.addDialogRef.open(type, row, row.id);
      this.$refs.addDialogRef.$refs.form &&
      this.$refs.addDialogRef.$refs.form.clearValidate();
    },

    //批量删除
    allDelBtn() {
      if (this.selection.length === 0) return;
      let flag = this.selection.some(item => [1, 2].includes(item.orderStatus))
      if (flag) return this.$message.warning('抱歉已审核、审核中的数据不能删除,请检查')
      this.delVisible = true;
    },

    //删除接口
    remove(delData) {
      deleteInformation(delData).then((res) => {
        this.$message.success('删除成功！');
        this.reload();
      });
    },

    //删除弹框确定
    commitBtn() {
      const dataId = this.selection.map((v) => v.id);
      this.remove(dataId);
    },

    //查看详情
    openorderDetail(row) {
      this.$refs.contactDetailDialogRef.open(row);
    },
    saleOrderSubmit(res) {
      submit({
        businessId: res.id
      }).then((res) => {
        this.$message.success('提交成功');
        this.reload();
      });
    },
    //查看合同详情
    opencontractDetail(row) {
      let data = {
        id: row.contractId
      };
      this.$refs.contractDetailRef.open(data);
    }
  }
};
</script>

<style lang="scss" scoped>
:deep .el-card__body {
  padding: 0;
}

:deep(.el-link--inner) {
  margin-left: 0px !important;
}

.sys-organization-list {
  height: calc(100vh - 264px);
  box-sizing: border-box;
  border-width: 1px;
  border-style: solid;
  overflow: auto;
}

.sys-organization-list :deep(.el-tree-node__content) {
  height: 40px;

  & > .el-tree-node__expand-icon {
    margin-left: 10px;
  }
}


.el-dropdown-link {
  cursor: pointer;
  color: var(--color-primary-5);
}

.el-icon-arrow-down {
  font-size: 12px;
}
</style>
