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

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        cache-key="workOrderTable"
        :pageSize="20"
        :height="tableHeight"
        @fullscreen-change="fullscreenChange"
      >
        <template v-slot:feedStatus="{ row }">
          {{
            row.feedStatus === 1
              ? '报工中'
              : row.feedStatus === 0
              ? '投料中'
              : ''
          }}
        </template>

        <template v-slot:taskStayTimeDuration="{ row }">
          <span>{{ getTimeData(row.taskStayTimeDuration) }}</span>
        </template>
      </ele-pro-table>
    </el-card>
  </div>
</template>

<script>
  import { getList } from '@/api/InTheSystem/index.js';

  import search from './components/search.vue';

  export default {
    components: {
      search
    },
    data() {
      return {
        loading: false,
        tableHeight: 'calc(100vh - 340px)'
      };
    },
    computed: {
      // 表格列配置
      columns() {
        return [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            fixed: 'left'
          },
          {
            prop: 'batchNo',
            label: '批次号',
            align: 'center',
            fixed: 'left',
            showOverflowTooltip: true
          },

          {
            prop: 'workOrderCode',
            label: '工单编码',
            align: 'center',
            minWidth: 110,
            fixed: 'left',
            showOverflowTooltip: true
          },

          {
            prop: 'qualityTaskName',
            label: '当前工序',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            prop: 'taskName',
            label: '处置工序',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            prop: 'categoryCode',
            label: '物品编码',
            align: 'center',
            minWidth: 160,
            showOverflowTooltip: true
          },

          {
            prop: 'name',
            label: '物品名称',
            align: 'center',
            minWidth: 160,
            showOverflowTooltip: true
          },

          {
            slot: 'feedStatus',
            label: '类型',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'number',
            label: '数量',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            prop: 'deviceName',
            label: '设备名称',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'taskStayTimeDuration',
            slot: 'taskStayTimeDuration',
            label: '停留时间',
            align: 'center',
            width: 120,
            showOverflowTooltip: true
          },
          {
            width: 150,
            prop: 'modelType',
            label: '型号',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            width: 150,
            prop: 'specification',
            label: '规格',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'colorKey',
            label: '颜色',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'modelKey',
            label: '机型',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'workstationName',
            label: '工位',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            prop: 'factoriesName',
            label: '所属工厂',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            prop: 'createName',
            label: '创建人',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            showOverflowTooltip: true
          }
        ];
      }
    },
    created() {},
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where }) {
        where = { rootCategoryLevelId: ['2'], ...where };

        return getList({
          pageNum: page,
          size: limit,
          ...where
        });
      },

      fullscreenChange(fullscreen) {
        if (fullscreen) {
          this.tableHeight = 'calc(100vh - 120px)';
        } else {
          this.tableHeight = 'calc(100vh - 340px)';
        }
      },

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

      getTimeData(time) {
        if (time) {
          const days = Math.floor(time / 1440);
          const remainingMinutes = time % 1440;
          const hours = Math.floor(remainingMinutes / 60);
          const minutes = remainingMinutes % 60;

          // 返回格式化的字符串
          if (days) {
            return `${days}天${hours}小时${minutes}分钟`;
          } else if (hours) {
            return `${hours}小时${minutes}分钟`;
          } else {
            return `${minutes}分钟`;
          }
        }
      }
    }
  };
</script>

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