<template>
  <div>
    <ele-pro-table
      ref="table"
      :columns="columns"
      height="calc(100vh - 280px)"
      width="100%"
      :datasource="datasource"
      :selection.sync="selection"
      @selection-change="handleSelectionChange"
      @cell-click="cellClick"
      highlight-current-row
      @select="cellClick"
      :need-page="false"
      @sort-change="onSortChange"
      @columns-change="handleColumnChange"
      :cache-key="cacheKeyUrl"
    >
      <!-- <template v-slot:toolbar>
        <div class="rx-bc">
          <div class="c_title">工单列表 </div>

          <div>
            <el-input
              style="width: 180px"
              clearable
              v-model="code"
              placeholder="请输入工单号"
            />
            <el-button  size="mini" type="primary" style="margin: 0 5px" @click="handleSearch">查询</el-button>
          </div>
        </div>
      </template> -->

      <template v-slot:code="{ row }">
        <el-link type="primary" @click="handRoute(row)" :underline="false">{{
          row.code
        }}</el-link>
      </template>

      <template v-slot:singleReport="{ row }">
        <el-tag size="mini">
          {{ row.singleReport == 1 ? '单个报工' : '批量报工' }}</el-tag
        >
      </template>

      <template v-slot:taskName="{ row }">
        <span> {{ row.taskName }}</span>
      </template>

      <template v-slot:status="{ row }">
        <span> {{ sumStatus(row) }}</span>
      </template>

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

      <template v-slot:formingWeight="{ row }">
        <span> {{ row.formingWeight }} {{ row.weightUnit }} </span>
      </template>
    </ele-pro-table>

    <!-- //工单列表 -->
    <routings
      v-if="routingShow"
      :routeObj="routeObj"
      @closeRoute="closeRoute"
    ></routings>
  </div>
</template>

<script>
  import { workorderPage2 } from '@/api/produce/workOrder.js';
  import routings from './routings.vue';
  import { isFullscreen } from 'ele-admin';
  import tableColumnsMixin from '@/mixins/tableColumnsMixin';
  export default {
    components: { routings },
    mixins: [tableColumnsMixin],
    data() {
      return {
        loading: false,
        selection: [],

        routeObj: {},
        routingShow: false,

        code: '',

        internalIsFullscreen: isFullscreen(), // 初始值
        cacheKeyUrl: 'mes-145878-mes-produce',
        columnsVersion: 0
      };
    },
    computed: {
      // 表格列配置
      columns() {
        let columnsVersion=this.columnsVersion;
        return [
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center',
            fixed: true
          },
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'batchNo',
            label: '批次号',
            align: 'center'
          },

          {
            prop: 'code',
            slot: 'code',
            label: '生产工单号',
            align: 'center',
            minWidth: '160'
          },

          {
            prop: 'productCode',
            label: '编码',
            align: 'center',
            minWidth: '160',
            showOverflowTooltip: true
          },
          {
            prop: 'productName',
            label: '名称',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            prop: 'singleReport',
            slot: 'singleReport',
            label: '报工类型',
            align: 'center',
            filters: [
              { text: '单个报工', value: 1 },
              { text: '批量报工', value: 0 }
            ],
            filterMethod: this.filterMethod,
            width: 100
          },
          {
            prop: 'model',
            label: '型号',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'productionCodes',
            label: '生产编号',
            align: 'center',
            minWidth: 150,
            showOverflowTooltip: true
          },
          {
            prop: 'brandNo',
            label: '牌号',
            align: 'center',
            showOverflowTooltip: true
          },

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

          {
            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
          },

          {
            slot: 'taskName',
            prop: 'taskName',
            label: '工序进度',
            align: 'center',
            showOverflowTooltip: true,
            sortable: 'custom',
            minWidth: 110
          },
          {
            slot: 'status',
            prop: 'status',
            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
          },

          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          }
        ];
      },

      taskObj() {
        return this.$store.state.user.taskObj;
      },
      current() {
        console.log(this.$store.state.user, 'this.$store.state.user');

        return this.$store.state.user.currentObj.id;
      }
    },

    watch: {
      taskObj: {
        handler(val) {
          this.reload();
        },
        // immediate: true,
        deep: true
      },
      current: {
        handler(val) {
          this.reload();
        },
        // immediate: true,
        deep: true
      }
    },
    created() {},

    mounted() {
      // 添加窗口resize事件监听器
      window.addEventListener('resize', this.handleResize);
    },

    beforeDestroy() {
      // 组件销毁前移除事件监听器，防止内存泄漏
      window.removeEventListener('resize', this.handleResize);
    },
    methods: {
      filterMethod(value, row, column) {
        if (value == row.singleReport) {
          return row;
        }
      },
      sumStatus(row) {
        if (row.reportStatus === 1) {
          return '已报工';
        }
        if (row.feedStatus === 1) {
          return '已投料';
        }
        // {{  row.reportStatus }}  {{  row.feedStatus }}
      },

      /* 表格数据源 */
      datasource({ page, where }) {
        if (!this.taskObj?.id) {
          return [];
        }
        return workorderPage2({
          pageNum: page,
          size: 1000,
          workOrderId: this.$route.query.workOrderId
            ? this.$route.query.workOrderId
            : null,
          taskId: this.taskObj && this.taskObj.id,
          deviceId: this.current && this.current.deviceId,
          keyWord: this.keyWord,
          taskName: this.taskName,
          ...where
        });
      },

      onSortChange(e) {
        console.log(e);
        let sort = {
          orderBy: e.order,
          sortName: e.prop
        };
        this.where = sort;
        this.reload();
      },
      handleSearch(obj) {
        this.keyWord = obj.keyWord;
        this.taskName = obj.taskName;
        this.reload();
      },

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

      handleSelectionChange(val) {
        let initReportValue = val[0] && val[0].singleReport;
        let allSame = true;

        val.forEach((item) => {
          if (item.singleReport !== initReportValue) {
            allSame = false;
          }
        });

        if (!allSame) {
          return this.$message.warning('请选择报工类型相同的工单');
        }

        let ids = [];
        ids = val.map((item) => {
          return item.id;
        });
        this.$emit('workSelect', { ids: ids, list: val });
      },

      rowClick(e) {
        this.$emit('rowClick', e, this.taskObj.id);
      },
      cellClick() {
        this.$nextTick(() => {
          this.$emit('getTaskName', this.selection);
        });
      },

      handRoute(row) {
        this.routeObj = row;
        this.routingShow = true;
      },

      closeRoute() {
        this.routingShow = false;
      },

      handleResize() {
        this.internalIsFullscreen = isFullscreen();
      }
    }
  };
</script>

<style lang="scss" scoped></style>
