<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <productionPlan-search
        @search="reload"
        ref="searchRef"
        :statusOpt="statusOpt"
        :planType="planType"
        :activeName="activeName"
      >
      </productionPlan-search>

      <el-tabs v-model="activeName" type="card" size="mini">
        <el-tab-pane label="未发布" name="first"></el-tab-pane>
        <el-tab-pane label="已发布" name="second"></el-tab-pane>
        <el-tab-pane label="已变更" name="change"></el-tab-pane>
      </el-tabs>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :key="activeName"
        :initLoad="false"
        :columns="newColumns"
        :datasource="datasource"
        :cache-key="`${activeName}ProductionPlanTable`"
        @sort-change="onSortChange"
        autoAmendPage
      >
        <template v-slot:productNum="{ row }">
          {{ row.productNum }} {{ row.unit }}
        </template>

        <template v-slot:productWeight="{ row }">
          {{ row.productWeight }} {{ row.weightUnit }}
        </template>

        <template v-slot:requiredFormingNum="{ row }">
          {{ row.requiredFormingNum }} {{ row.unit }}
        </template>

        <template v-slot:newSumOrderWeight="{ row }">
          {{ row.newSumOrderWeight }} {{ row.newWeightUnit }}
        </template>

        <template v-slot:status="{ row }">
          <span :class="{ 'ele-text-danger': row.status == 3 }">
            {{ statusFormatter(row.status) }}
          </span>
        </template>
      </ele-pro-table>
    </el-card>
  </div>
</template>
  
  <script>
import { getList } from '@/api/aps/index.js';
import productionPlanSearch from './productionPlan-search.vue';
import { fieldModelAPI } from '@/api/main';

export default {
  components: {
    productionPlanSearch
  },
  props: {
    timeDimensionPlanType: { type: Number, default: 1 },
    saleOrderData: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      activeName: 'first',

      // 加载状态
      loading: false,
      statusOpt: {
        first: [
          { label: '所有状态', value: '3,2' },
          { label: '待发布', value: '2' },
          { label: '发布失败', value: '3' },
          { label: '已发布', value: '4' }
        ],
        second: [
          { label: '所有状态', value: '7,4,5,6' },
          { label: '待生产', value: '4' },
          { label: '生产中', value: '5' },
          { label: '已完成', value: '6' },
          { label: '已延期', value: '7' }
        ],
        change: [{ label: '已变更', value: '9' }]
      },
      planType: [
        { label: '所有计划类型', value: null },
        { label: '内销计划', value: '1' },
        { label: '外销计划', value: '2' },
        { label: '预制计划', value: '3' }
      ],
      newColumns: []
    };
  },
  computed: {
    clientEnvironmentId() {
      return this.$store.state.user.info.clientEnvironmentId;
    },
    // 表格列配置
    columns() {
      const opt = {
        first: [],
        second: [
          {
            prop: 'releaseTime',
            label: '工单发布日期',
            align: 'center',

            minWidth: 110
          },
          {
            prop: 'planFormingTime',
            label: '预测生产日期',
            align: 'center',

            minWidth: 110
          },
          {
            prop: 'deliveryTime',
            label: '预测交货日期',
            align: 'center',

            minWidth: 110
          },
          {
            prop: 'formingTime',
            label: '实际交货日期',
            align: 'center',

            minWidth: 110
          }
        ],
        change: []
      };

      return [
        {
          columnKey: 'index',
          label: '序号',
          type: 'index',
          width: 55,
          align: 'center',
          showOverflowTooltip: true
        },

        {
          slot: 'batchNo',
          prop: 'batchNo',
          label: '批次号',
          align: 'center',
          minWidth: 140,
          sortable: true
        },

        {
          slot: 'code',
          prop: 'code',
          action: 'code',
          label: '计划编号',
          align: 'center',

          minWidth: 160,
          sortable: true
        },

        {
          prop: 'salesCode',
          action: 'salesCode',
          label: '销售订单号',
          align: 'center',
          minWidth: 160
        },

        {
          prop: 'productCode',
          label: '编码',
          align: 'center',

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

          minWidth: 140
        },

        {
          prop: 'brandNo',
          label: '牌号',
          align: 'center',
          showOverflowTooltip: true
        },

        {
          prop: 'specification',
          label: '规格',
          align: 'center',
          minWidth: 150,

          showOverflowTooltip: true
        },
        {
          prop: 'model',
          label: '型号',
          align: 'center',
          showOverflowTooltip: true
        },

        {
          prop: 'priority',
          label: '优先级',
          align: 'center',
          minWidth: 120,
          slot: 'priority',
          sortable: 'custom'
        },

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

        {
          prop: 'productNum',
          label: '计划数量',
          align: 'center',

          slot: 'productNum'
        },
        {
          prop: 'productWeight',
          label: '计划重量',
          align: 'center',

          slot: 'productWeight'
        },
        {
          prop: 'requiredFormingNum',
          label: '要求生产数量',
          align: 'center',

          slot: 'requiredFormingNum'
        },

        {
          prop: 'newSumOrderWeight',
          label: '要求生产重量',
          align: 'center',

          slot: 'newSumOrderWeight'
        },

        {
          prop: 'scheduleStatusName',
          label: '进度状态',
          align: 'center',
          minWidth: 100
        },

        {
          prop: '',
          label: '已排产数量',
          align: 'center',
          showOverflowTooltip: true
        },

        {
          prop: '',
          label: '未排产数量',
          align: 'center',
          showOverflowTooltip: true
        },

        {
          prop: '',
          label: '已生产数量',
          align: 'center',
          showOverflowTooltip: true
        },

        {
          prop: '',
          label: '未生产数量',
          align: 'center',
          showOverflowTooltip: true
        },

        {
          prop: 'factoryName',
          label: '所属工厂',
          align: 'center'
        },

        {
          prop: '',
          label: '合格品数',
          align: 'center'
        },

        {
          prop: '',
          label: '不合格品数',
          align: 'center'
        },

        {
          prop: '',
          label: '合格率',
          align: 'center'
        },

        {
          prop: 'moCount',
          label: '模数',
          align: 'center',
          show: this.clientEnvironmentId == '4'
        },

        {
          prop: 'blockCount',
          label: '块数',
          align: 'center',
          show: this.clientEnvironmentId == '4'
        },
        {
          prop: 'noWordCount',
          label: '未排程块数',
          align: 'center',

          show: this.clientEnvironmentId == '4',
          minWidth: 110
        },
        {
          prop: 'reqMoldTime',
          label: '计划完成日期',
          align: 'center',

          minWidth: 110
        },

        ...opt[this.activeName],

        {
          prop: 'orderType',
          label: '计划类型',
          align: 'center',

          formatter: (row) => {
            const obj = this.planType.find((i) => i.value == row.planType);
            return obj && obj.label;
          }
        },
        {
          prop: 'version',
          label: '版本',
          align: 'center',

          minWidth: 80
        },
        {
          prop: 'createTime',
          label: '创建时间',
          align: 'center',

          minWidth: 110
        },
        {
          columnKey: 'status',
          slot: 'status',
          label: '状态',
          align: 'center',
          formatter: (row) => {
            const obj = this.statusOpt[this.activeName].find(
              (i) => i.value == row.status
            );
            return obj && obj.label;
          }
        }
      ];
    }
  },
  created() {
    this.getFieldModel();
  },
  methods: {
    statusFormatter(status) {
      const obj = this.statusOpt[this.activeName].find(
        (i) => i.value == status
      );

      return obj && obj.label;
    },
    /* 表格数据源 */
    datasource({ page, limit, where }) {
      if (this.saleOrderData.id) {
        where['saleOrderCode'] = this.saleOrderData.orderNo;
      }
      return getList({
        pageNum: page,
        timeDimensionPlanType: this.timeDimensionPlanType,
        size: limit,
        ...where,
        ...this.sort
      });
    },

    getFieldModel() {
      fieldModelAPI({ fieldModel: 't_main_category' }).then((res) => {
        let newRes = res.map((m) => {
          return {
            prop: 'extField.' + m.prop,
            label: m.label,
            align: 'center',
            showOverflowTooltip: true
          };
        });

        this.newColumns = [...this.columns, ...newRes];

        this.$forceUpdate();
      });
    },

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

    onSortChange(e) {
      let sort = {
        orderBy: e.order,
        sortName: e.prop
      };
      this.sort = sort;
      this.reload();
    }
  }
};
</script>
  
  <style lang="scss" scoped>
.btn_box {
  margin-bottom: 6px;
}

.check_box {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.check {
  width: 14px;
  height: 14px;
  border: 1px solid #dddddd;

  display: flex;
  align-items: center;
  justify-content: center;
}

.act_check {
  border: 1px solid #409eff;
  background: #409eff;

  .el-icon-check {
    color: #fff;
    font-size: 10px;
  }
}
</style>
  