<template>
  <div>
    <div>
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <w-row :gutter="10">
          <w-col :xl="5" :lg="5" :md="8" :sm="24">
            <j-dict-select-tag v-model="queryParam.disFlag" dict-code="yn" placeholder="请选择驳回状态" />
          </w-col>
          <w-col :xl="5" :lg="5" :md="8" :sm="24">
            <w-input v-model="queryParam.partNo" placeholder="请输入料号" />
          </w-col>
          <w-col :xl="5" :lg="5" :md="8" :sm="24">
            <w-input v-model="queryParam.rdCategory" placeholder="请输入研发分类" />
          </w-col>

          <w-col :xl="5" :lg="5" :md="8" :sm="24">
            <span v-has="'MergerReqsBody:query'" class="table-page-search-submitButtons">
              <w-button @click="searchQuery" type="primary" icon="search">搜索</w-button>
              <w-button @click="searchReset" type="primary">重置</w-button>
              <j-super-query ref="superQueryModal" :field-list="refColumns" @handleSuperQuery="handleSuperQuery" />
            </span>
          </w-col>

          <!-- <div class="leftArea" v-has="'MergerReqsBody:export'">
            <div class="filterBox">
              <span class="iconfont icon-download-2" @click="exportExcel()"></span>
            </div>
          </div> -->
        </w-row>
      </div>
      <!-- 查询区域-END -->

      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <w-button
          @click="handlesAdd"
          v-has="'MergerReqsBody:add'"
          :disabled="!(head && head.id && head.status === '000')"
          type="primary"
        >
          新增
        </w-button>
        <w-button
          @click="handlesVirtual"
          :disabled="!((head.status === '000' || head.status === '090') && head.reqsType === '2')"
          v-has="'MergerReqsBody:virtual'"
          type="primary"
        >
          生成虚拟料号
        </w-button>
        <w-button
          @click="handlesExtract"
          :disabled="!(head.status === '000' || head.status === '090')"
          v-has="'MergerReqsBody:extract'"
          type="primary"
        >
          提取归类要素
        </w-button>
        <!-- <w-button
          @click="handlesSplit"
          v-has="'MergerReqsBody:split'"
          type="primary"
          :disabled="!(head.status === '000' || head.status === '090')"
        >
          数据拆分
        </w-button> -->
        <w-button
          @click="handlesMerge"
          v-has="'MergerReqsBody:merge'"
          type="primary"
          :disabled="!(head.status === '000' || head.status === '090')"
        >
          数据合并
        </w-button>
        <w-button
          @click="importModel"
          v-has="'MergerReqsBody:import'"
          type="primary"
          :disabled="!(head && head.status === '000')"
        >
          导入料号
        </w-button>
        <w-button
          v-has="'MergerReqs:import'"
          type="primary"
          @click="importEleModel"
          :disabled="!(dataSource.length > 0 && head.status === '000')"
        >
          导入要素
        </w-button>
        <w-button
          v-has="'MergerReqs:eleExport'"
          type="primary"
          @click="exportEleModel"
          :disabled="dataSource.length === 0"
        >
          导出要素
        </w-button>
        <w-button
          @click="handleFileUploadModel"
          v-has="'MergerReqsBody:fileUpload'"
          type="primary"
          :disabled="!(head && (head.status === '000' || head.status === '090'))"
        >
          附件上传
        </w-button>
      </div>

      <!-- table区域-begin -->
      <div>
        <a-table
          ref="table"
          :id="refColumnsKey"
          :columns="refColumns"
          :scroll="{ ...getTableScroll(), y: true }"
          :data-source="dataSource"
          :pagination="ipagination"
          :loading="loading"
          :custom-header-row="customHeaderRow"
          :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          size="middle"
          @change="handleTableChange"
          bordered
          :components="resizableComponents"
          row-key="id"
          :custom-row="selectRow"
          class="j-table-force-nowrap"
        >
          <span slot="action" slot-scope="text, record">
            <a v-has="'MergerReqsBody:edit'" @click="handlesEdit(record)">编辑</a>

            <span v-has="'MergerReqsBody:split'">
              <w-divider type="vertical" />
              <a @click="handlesSplit(record)" :disabled="!(head.status === '000' || head.status === '090')"> 拆分 </a>
            </span>

            <span v-has="'MergerReqsBody:del'">
              <w-divider type="vertical" />
              <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                <a :disabled="!(head.status === '000' || record.disFlag === '1')">删除</a>
              </a-popconfirm>
            </span>
          </span>
        </a-table>
      </div>
    </div>
    <w-table-columns-win ref="choose" @columnsChange="columnsChange" @resetColumns="resetColumns" :columns="columns" />

    <excel-import-model ref="importModel" @close="modalFormClose" />
    <ExcelExportModel ref="excelExport" :hidden-columns="hiddenColumns" />
    <MergerReqsModalBodyFileUpload
      ref="MergerReqsModalBodyFileUpload"
      @ok="modalFormOk"
      @close="modalFormClose"
      :head="head"
    />
  </div>
</template>

<script>
import { getAction, postAction, deleteAction } from '@/api/manage';
import { JeecgListMixin, WGridResizeByCssMixin } from '@/mixins/JeecgListMixin';
import { mixinDevice } from '@/utils/mixin';
import ExcelExportModel from '@/views/excel/modules/ExcelExportModel.vue';
import ExcelImportModel from '@/views/excel/modules/ExcelImportModel.vue';
import { filterDictTextByFormatCache } from '@comp/dict/JDictSelectUtil';
import MergerReqsModalBodyFileUpload from './MergerReqsModalBodyFileUpload.vue';

export default {
  name: 'MergerReqsModalBody',
  components: {
    ExcelExportModel,
    ExcelImportModel,
    MergerReqsModalBodyFileUpload,
  },
  mixins: [JeecgListMixin, mixinDevice, WGridResizeByCssMixin],
  props: {
    head: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      description: '归类需求申请表体',
      initeLoadData: false,
      // 自动高度
      autoHeight: false,
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          ellipsis: true,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          },
        },
        {
          title: '是否驳回',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'disFlag',
          customRender: (text) => {
            return filterDictTextByFormatCache('yn', text);
          },
        },
        {
          title: '驳回原因',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'disReason',
        },
        {
          title: '料号',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'partNo',
          scopedSlots: { customRender: 'partNo' },
        },
        {
          title: '研发分类',
          align: 'left',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'rdCategory',
        },
        {
          title: '产品名称',
          align: 'center',
          width: 130,
          ellipsis: true,
          sorter: true,
          dataIndex: 'productName',
        },
        {
          title: '产品描述',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'productDesc',
        },
        {
          title: '英文名称',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'productEngName',
        },
        {
          title: '英文描述',
          align: 'center',
          width: 200,
          ellipsis: true,
          sorter: true,
          dataIndex: 'productEngDesc',
        },
        {
          title: 'ERP单位',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'erpUnit',
        },

        {
          title: '进口国归类状态',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'impCtryStatus_dictText',
        },
        {
          title: '出口国归类状态',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'expCtryStatus_dictText',
        },
        {
          title: '全球归类状态',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'gloStatus_dictText',
        },
        {
          title: '进口国归类完成时间',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'impCtryCompTime',
        },
        {
          title: '出口国归类完成时间',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'expCtryCompTime',
        },
        {
          title: '全球归类完成时间',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'gloCompTime',
        },

        {
          title: '原委托单行ID',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'originalIds',
        },
        {
          title: '进口国归类执行人',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'impExecuteBy',
          customRender: (text, record) => {
            if (text) {
              return text + '-' + record.impExecuteName;
            } else {
              return '-';
            }
          },
        },
        {
          title: '进口国归类审核人',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'impRevBy',
          customRender: (text, record) => {
            if (text) {
              return text + '-' + record.impRevName;
            } else {
              return '-';
            }
          },
        },
        {
          title: '出口国归类执行人',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'expExecuteBy',
          customRender: (text, record) => {
            if (text) {
              return text + '-' + record.expExecuteName;
            } else {
              return '-';
            }
          },
        },
        {
          title: '出口国归类审核人',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'expRevBy',
          customRender: (text, record) => {
            if (text) {
              return text + '-' + record.expRevName;
            } else {
              return '-';
            }
          },
        },
        {
          title: '全球归类执行人',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'gloExecuteBy',
          customRender: (text, record) => {
            if (text) {
              return text + '-' + record.gloExecuteName;
            } else {
              return '-';
            }
          },
        },
        {
          title: '全球归类审核人',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true,
          dataIndex: 'gloRevBy',
          customRender: (text, record) => {
            if (text) {
              return text + '-' + record.gloRevName;
            } else {
              return '-';
            }
          },
        },

        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          width: 150,
          scopedSlots: {
            customRender: 'action',
          },
          ellipsis: true,
          fixed: 'right',
        },
      ],
      url: {
        list: '/merger/mergerReqsList/list',
        delete: '/merger/mergerReqsList/delete',
        deleteBatch: '/merger/mergerReqsList/deleteBatch',
        exportXlsUrl: '/merger/mergerReqsList/exportXls',
        importExcelUrl: 'merger/mergerReqsList/importExcel',
        extractRdElem: 'merger/mergerReqsRdElem/extractRdElem',
        checkGeneratePartNo: 'merger/mergerReqsList/checkGeneratePartNo',
        importEleExcelUrl: 'merger/mergerReqsRdElem/importExcel',
        exportXlsEleUrl: 'merger/mergerReqsRdElem/exportXls',
      },
      isorter: {
        column: 'createTime',
        order: 'asc',
      },
      hiddenColumns: true,
    };
  },
  watch: {
    head: {
      handler(val) {
        if (val && val.id) {
          this.extQueryParam.headId = val.id;
          this.searchQuery();
        } else {
          this.extQueryParam.headId = null;
          this.dataSource = [];
        }
      },
      deep: true,
    },
  },
  methods: {
    /* 先加载字典、后加载数据 */
    async initDictConfig() {},
    handleDelete(id) {
      deleteAction(this.url.delete, { id: id }).then((res) => {
        if (res.success) {
          //重新计算分页问题
          this.reCalculatePage(1);
          this.$message.success(res.message);
          this.loadData();
          this.onClearSelected();
          this.afterDelete();
          this.$emit('modalFormClose');
        } else {
          this.$message.warning(res.message);
        }
      });
    },
    add() {
      this.extQueryParam.headId = null;
      this.dataSource = [];
      this.queryParam = {};
    },
    edit(record) {
      this.extQueryParam.headId = record.id;
      this.searchQuery();
    },
    /* 生成虚拟料号 */
    handlesVirtual() {
      const that = this;
      this.$confirm({
        title: '确认',
        content: '确认生成虚拟料号吗?',
        onOk: function () {
          getAction(that.url.checkGeneratePartNo, {
            id: that.head.id,
          }).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.loadData();
            } else {
              that.$message.warning(res.message);
            }
          });
        },
      });
    },
    /* 提取归类要素 */
    handlesExtract() {
      if (this.selectedRowKeys.length === 0) {
        this.$message.warning('请选择至少一条数据进行操作！');
        return;
      }
      const ids = this.selectedRowKeys.join(',');
      this.handlesExtractConfirm('确认提取归类要素吗?', { ids });
    },
    /* 提取归类要素 确认框 */
    handlesExtractConfirm(message, params) {
      const that = this;
      that.$confirm({
        title: '确认',
        content: message,
        onOk: function () {
          postAction(that.url.extractRdElem, params).then((res) => {
            if (res.success && res.code === 2022) {
              that.handlesExtractConfirm(res.message, {
                ...params,
                confirmFlg: '1',
              });
            } else if (res.success) {
              that.$message.success(res.message);
              that.loadData();
            } else {
              that.$message.warning(res.message);
            }
          });
        },
      });
    },
    /* 数据拆分 */
    handlesSplit(record) {
      // if (this.selectedRowKeys.length !== 1) {
      //   this.$message.warning('请选择一条数据进行操作！')
      //   return
      // }
      this.$emit('openBodyData', 'split', [record]);
    },
    /* 数据合并 */
    handlesMerge() {
      if (this.selectedRowKeys.length <= 1) {
        this.$message.warning('请选择至少两条数据进行操作！');
        return;
      }
      this.$emit('openBodyData', 'merge', this.selectionRows);
    },
    handlesAdd() {
      this.$emit('openBodyForm', 'add');
    },
    handlesEdit(record) {
      this.$emit('openBodyForm', 'edit', record);
    },
    exportExcel() {
      const para = this.getQueryParams();
      para.ids = this.selectedRowKeys;
      this.$refs.excelExport.show(this.url.exportXlsUrl, para, this.columns, 'MIDEA_MERGER_REQS_LIST');
    },
    handleFileUploadModel() {
      this.$refs.MergerReqsModalBodyFileUpload.show();
    },
    importModel() {
      this.$refs.importModel.show(this.url.importExcelUrl, 'MIDEA_MERGER_REQS_LIST', {
        id: this.head.id,
      });
    },
    /* 要素批量导入 */
    importEleModel() {
      this.$refs.importModel.show(this.url.importEleExcelUrl, 'MIDEA_MERGER_REQS_RD_ELEM', {
        // headId: this.head.id
      });
    },
    /* 要素批量导出 */
    exportEleModel() {
      this.$refs.excelExport.show(
        this.url.exportXlsEleUrl,
        {
          ids: this.head.id,
          id: this.head.id,
        },
        this.columns,
        'MIDEA_MERGER_REQS_RD_ELEM'
      );
    },
    searchReset() {
      this.queryParam = {};
      this.searchQuery();
    },
    searchQuery() {
      if (this.extQueryParam && this.extQueryParam.headId) {
        this.loadData(1);
      }
    },
  },
};
</script>
