<template>
  <div>
    <el-card shadow="never" v-loading="loading">
      <el-tabs v-model="activeName" type="card" @tab-click="changeActive">
        <el-tab-pane label="未完成工单" name="first"></el-tab-pane>
        <el-tab-pane label="已完成工单" name="second"></el-tab-pane>
      </el-tabs>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :key="activeName"
        :columns="newColumns"
        :datasource="datasource"
        row-key="id"
        :cache-key="`${activeName}produceOrderTable`"
        :selection.sync="selection"
        @sort-change="onSortChange"
        autoAmendPage
        :parse-data="parseData"
      >
        <template v-slot:formingNum="{ row }">
          <span> {{ row.formingNum }} {{ row.unit }} </span>
        </template>

        <template v-slot:formingWeight="{ row }">
          <span> {{ row.formingNum }} {{ row.weightUnit }} </span>
        </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 { getPage, fieldModel } from '@/api/produceOrder/index.js';
  export default {
    props: ['code'],
    components: {},
    data() {
      return {
        activeName: 'first',

        // 加载状态
        loading: false,
        pageType: 'add',
        dialogTitle: '',
        isBindPlan: false,
        statusOpt: {
          first: [
            { label: '所有状态', value: '5,4' },
            { label: '待生产', value: '4' },
            { label: '生产中', value: '5' }
            // { label: '已延期', value: '7' }
          ],
          second: [{ label: '已完成', value: '6' }]
        },
        planType: [
          { label: '所有计划类型', value: null },
          { label: '内销计划', value: '1' },
          { label: '外销计划', value: '2' },
          { label: '预制计划', value: '3' }
        ],
        selection: [],
        newColumns: []
      };
    },
    computed: {
      // 表格列配置
      columns() {
        const opt = {
          first: [
            // {
            //   prop: 'deliveryTime',
            //   label: '预测交货日期',
            //   align: 'center',
            //   showOverflowTooltip: true,
            //   minWidth: 110
            // }
          ],
          second: [
            {
              prop: 'completeTime',
              label: '完成时间',
              align: 'center'
            },
            {
              prop: 'cycle',
              label: '生产周期',
              align: 'center'
            }
          ]
        };

        return [
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center',
            fixed: 'left'
          },

          {
            prop: 'batchNo',
            label: '批次号',
            align: 'center',
            minWidth: 130
          },

          {
            prop: 'code',
            label: '生产工单号',
            align: 'center',
            minWidth: 110
          },
          // {
          //   prop: 'originalCode',
          //   label: '原始工单号',
          //   align: 'center',
          //   minWidth: 110
          // },

          // {
          //   slot: 'QRcode',
          //   label: '二维码',
          //   align: 'center',
          //   minWidth: 110
          // },

          {
            prop: 'productionPlanCode',
            label: '计划编号',
            align: 'center',
            minWidth: 110
          },
          {
            prop: 'planType',
            label: '计划类型',
            align: 'center',
            formatter: (row) => {
              const obj = this.planType.find((i) => i.value == row.planType);
              return obj && obj.label;
            }
          },
          {
            prop: 'produceRoutingName',
            label: '工艺路线',
            align: 'center'
          },
          {
            prop: 'productCode',
            label: '编码',
            align: 'center'
          },
          {
            prop: 'productName',
            label: '名称',
            align: 'center'
          },
          {
            prop: 'brandNo',
            label: '牌号',
            align: 'center'
          },
          {
            prop: 'model',
            label: '型号',
            align: 'center'
          },

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

          {
            prop: 'formingNum',
            label: '要求生产数量',
            align: 'center',
            slot: 'formingNum',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'formingWeight',
            label: '要求生产重量',
            slot: 'formingWeight',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'formedNum',
            label: '已生产数量',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'formedWeight',
            label: '已生产重量',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'planStartTime',
            label: '计划开始时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'planCompleteTime',
            label: '计划结束时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'startTime',
            label: '实际开始时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },

          ...opt[this.activeName],
          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            slot: 'status',
            label: '状态',
            align: 'center',
            formatter: (row) => {
              const obj = this.statusOpt[this.activeName].find(
                (i) => i.value == row.status
              );
              return obj && obj.label;
            }
          },

          {
            prop: 'teamName',
            label: '班组',
            align: 'center',
            showOverflowTooltip: true
          }
        ];
      },

      clientEnvironmentId() {
        return this.$store.state.user.info.clientEnvironmentId;
      }
    },
    created() {
      this.getFieldModel();
    },
    methods: {
      statusFormatter(status) {
        const obj = this.statusOpt[this.activeName].find(
          (i) => i.value == status
        );

        return obj && obj.label;
      },
      /* 表格数据源 */
      async datasource({ page, limit, where, order }) {
        let res = await getPage({
          ...where,
          ...order,
          pageNum: page,
          size: limit,
          status: this.statusOpt[this.activeName][0].value,
          deviceCode: this.code,
          statusList: (
            where.status || this.statusOpt[this.activeName][0].value
          ).split(','),
          workOrderType: 1,
          ...this.sort
        });

        return res;
      },
      changeActive(tab) {
        this.activeName = tab.name;
        if (this.activeName === 'second') {
          let aaa = {
            status: this.statusOpt[this.activeName][0].value,
            statusList: this.statusOpt[this.activeName][0].value.split(',')
          };
          this.$refs.table.reload(aaa);
        } else {
          this.reload();
        }
      },

      reload(where = {}) {
        this.$nextTick(() => {
          where.statusList = (
            where.status || this.statusOpt[this.activeName][0].value
          ).split(',');
          this.$refs.table.reload({ page: 1, where });
        });
      },
      onSortChange(e) {
        let sort = {
          orderBy: e.order,
          sortName: e.prop
        };
        this.sort = sort;
        this.reload();
      },
      flattenArray(arr) {
        var result = []; // 存放结果的数组
        for (let i = 0; i < arr.length; i++) {
          if (Array.isArray(arr[i].subWorkOrder)) {
            let _arr = [];
            _arr = _arr.concat(arr[i].subWorkOrder);
            delete arr[i].subWorkOrder;
            result.push(arr[i]);
            result.push(..._arr);
          } else {
            result.push(arr[i]);
          }
        }

        return result;
      },

      /* 数据转为树形结构 */
      parseData(data) {
        return {
          ...data,
          list: this.$util.toTreeData({
            data: data.list,
            count: data.total,
            idField: 'code',
            parentIdField: 'originalCode'
          })
        };
      },

      /* 数据转为树形结构 */
      createSuccess() {
        this.reload();
      },
      getFieldModel() {
        fieldModel({ 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();
        });
      },

      handleTabChange() {
        this.$refs.searchRef.reset();
      }
    }
  };
</script>
<style lang="scss" scoped>
  ::v-deep .el-card__body {
    padding: 0px !important;
  }
</style>
