<template>
  <el-dialog
    :title="title"
    v-if="visible"
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="true"
    :close-on-press-escape="false"
    append-to-body
    width="90%"
  >
    <el-card shadow="never">
      <saleOrderSearch @search="reload" />

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        row-key="id"
        :initLoad="false"
      >
      </ele-pro-table>
    </el-card>

    <div class="btns">
      <el-button type="primary" size="small" @click="selected">选择</el-button>
      <el-button size="small" @click="handleClose">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import saleOrderSearch from './saleOrder-search';
  import { getPageList } from '@/api/saleOrder';
  import dictMixins from '@/mixins/dictMixins';
  export default {
    components: { saleOrderSearch },
    mixins: [dictMixins],
    data() {
      return {
        visible: false,
        title: '销售订单',

        // 表格列配置
        columns: [
          {
            columnKey: 'selection',
            type: 'selection',
            width: 45,
            align: 'center',
            // selectable: (row, index) => {
            //   return !this.tableData.some(
            //     (it) => it.id == row.id || it.salesOrderId == row.id
            //   );
            // },
            reserveSelection: true,
            fixed: 'left'
          },
          {
            prop: 'purchaseType',
            label: '采购类型',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 120,
            filters: [
              { value: '1', text: '未采购' },
              { value: '2', text: '已采购' }
            ],
            filterMethod: (value, row, column) => {
              if (value == row.purchaseType) {
                return row;
              }
            },
            formatter: (row) => {
              return row.purchaseType == '2' ? '已采购' : '未采购';
            }
          },
          {
            prop: 'code',
            label: '销售订单号',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150,
            slot: 'code',
            sortable: true
          },
          {
            prop: 'lineNumber',
            label: '行号',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'productCode',
            label: '编码',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140,
            sortable: true
          },

          {
            prop: 'productName',
            label: '名称',
            align: 'center',
            minWidth: 120
          },

          {
            prop: 'produceRoutingName',
            label: '路线',
            align: 'center',
            minWidth: 120
          },

          {
            prop: 'brandNo',
            label: '牌号',
            align: 'center'
          },
          {
            prop: 'specification',
            label: '规格',
            align: 'center',
            minWidth: 150,

            showOverflowTooltip: true
          },
          {
            prop: 'model',
            label: '型号',
            align: 'center',
            minWidth: 120
          },
          {
            prop: 'priority',
            label: '优先级',
            align: 'center',
            minWidth: 120,
            slot: 'priority',
            sortable: 'custom'
          },

          {
            prop: 'productSumWeight',
            label: '订单重量',
            align: 'center',
            slot: 'productSumWeight'
          },
          {
            prop: 'contractNum',
            slot: 'contractNum',
            label: this.clientEnvironmentId == '4' ? '交付数量' : '订单数量',
            align: 'center'
          },
          {
            prop: 'lackNum',
            label: '欠交数量',
            align: 'center'
          },
          {
            prop: 'moCount',
            label: '模数',
            align: 'center',
            show: this.clientEnvironmentId == '4'
          },

          {
            prop: 'planedNum',
            label: '未排完数量',
            align: 'center',
            formatter: (_row, _column, cellValue) => {
              return _row.contractNum - _row.planedNum;
            }
          },

          {
            prop: 'blockCount',
            label: '块数',
            align: 'center',
            show: this.clientEnvironmentId == '4'
          },
          {
            prop: 'orderLibraryType',
            label: '按单按库',
            align: 'center',
            showOverflowTooltip: true,
            formatter: (_row, _column, cellValue) => {
              return this.getDictValue('按单按库', _row.orderLibraryType);
            }
          },
          {
            prop: 'deliveryRequirements',
            label: '交付要求',
            align: 'center',
            showOverflowTooltip: true,
            formatter: (_row, _column, cellValue) => {
              return this.getDictValue('交付要求', _row.deliveryRequirements);
            }
          },
          {
            prop: 'orderType',
            label: '订单类型',
            align: 'center',
            showOverflowTooltip: true,
            formatter: (_row, _column, cellValue) => {
              return this.getDictValue('订单类型', _row.orderType);
            }
          },
          {
            prop: 'orderSource',
            label: '订单来源',
            align: 'center',
            showOverflowTooltip: true,
            formatter: (_row, _column, cellValue) => {
              return this.getDictValue('订单来源', _row.orderSource);
            }
          },
          {
            prop: 'status',
            label: '生产状态',
            align: 'center',
            showOverflowTooltip: true,
            formatter: (_row, _column, cellValue) => {
              return this.getDictValue('生产状态', _row.status);
            }
          },
          {
            prop: 'releaseTime',
            label: '下达时间',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'deliveryTime',
            label: '交付日期',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'customerName',
            label: '客户名称',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            prop: 'serialNo',
            label: '客户代号',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            prop: 'simpleName',
            label: '客户简称',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            prop: 'salesman',
            label: '业务员',
            showOverflowTooltip: true
          }
        ],

        // 表格选中数据
        selection: [],

        tableData: [],
        current: null
      };
    },

    watch: {},
    created() {
      this.requestDict('按单按库');
      this.requestDict('交付要求');
      this.requestDict('订单类型');
      this.requestDict('订单来源');
      this.requestDict('生产状态');
    },
    methods: {
      open(item) {
        if (item) {
          this.tableData = item;
        }
        this.visible = true;

        this.reload({ purchaseType: '1' });
      },

      /* 表格数据源 */
      async datasource({ page, limit, where }) {
        where.status = [1, 2, 3, 4, 7, 8];

        const data = await getPageList({
          ...where,
          pageNum: page,
          size: limit
        });
        return data;
      },

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

      handleClose() {
        this.visible = false;
        this.$refs.table.setSelectedRows([]);
        this.selection = [];
      },
      selected() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        const selectedIds = new Set(this.selection.map((item) => item.code));
        const hasIds = this.tableData.some((item) =>
          selectedIds.has(item.code)
        );

        if (hasIds) {
          this.$confirm('您勾选了已存在的数据，是否继续?', '提示', {
            type: 'warning'
          })
            .then(() => {
              this.$emit('chooseOrder', this.selection);
              this.handleClose();
            })
            .catch(() => {});
        } else {
          this.$emit('chooseOrder', this.selection);
          this.handleClose();
        }
      }
    }
  };
</script>

<style lang="scss" scoped>
  .btns {
    text-align: center;
    padding: 10px 0;
  }
</style>
