<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"
      >
        <template v-slot:code="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="handleDetails(row)"
            >{{ row.code }}</el-link
          >
        </template>
        <template v-slot:requireDeliveryTime="{ row }">
          <span v-if="row.deliveryMethod == 1">{{
            row.requireDeliveryTime
          }}</span>

          <el-link
            v-if="row.deliveryMethod == 2"
            type="primary"
            :underline="false"
            @click.native="handleMethod(row)"
            >分批时间</el-link
          >
        </template>

        <template v-slot:type="{ row }">
          <div v-if="row.type == 1">采购委外</div>
          <div v-if="row.type == 2">直接发货委外</div>
          <div v-if="row.type == 3">无采购委外</div>
          <div v-if="row.type == 4">有料委外</div>
          <div v-if="row.type == 5">无料委外</div>
        </template>

        <template v-slot:status="{ row }">
          <el-tag>{{ row.status == 1 ? '已发布' : '未发布' }}</el-tag>
        </template>

        <template v-slot:outsourceScene="{ row }">
          <span>{{ row.outsourceScene | outsourceSceneText }}</span>
        </template>

        <template v-slot:technicalDrawings="{ row }">
          <div v-if="row.technicalDrawings && row.technicalDrawings?.length">
            <el-link
              v-for="link in row.technicalDrawings"
              :key="link.id"
              type="primary"
              :underline="false"
              @click="downloadFile(link)"
            >
              {{ link.name }}</el-link
            >
          </div>
        </template>

        <template v-slot:files="{ row }">
          <div v-if="row.files && row.files?.length">
            <el-link
              v-for="link in row.files"
              :key="link.id"
              type="primary"
              :underline="false"
              @click="downloadFile(link)"
            >
              {{ link.name }}</el-link
            >
          </div>
        </template>

        <template v-slot:action="{ row }">
          <!-- <el-link
            type="primary"
            :underline="false"
            @click="handleDetails(row)"
          >
            详情
          </el-link> -->

          <el-link
            type="primary"
            v-if="row.status == 1"
            :underline="false"
            @click="handleFlow(row)"
          >
            流程
          </el-link>

          <el-link
            type="primary"
            v-if="row.status != 1"
            :underline="false"
            @click="handleRelease(row)"
          >
            发布
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <flow ref="flowRef"></flow>

    <release ref="releaseRef" @refresh="releaseRefresh"></release>
    <detail ref="detailsRef"></detail>

    <timeDialog ref="timeDialogRef"></timeDialog>
  </div>
</template>

<script>
  import { getList } from '@/api/outsourcing/index.js';
  import flow from './components/flow.vue';
  import search from './components/search.vue';
  import release from './components/release.vue';
  import Detail from './components/details.vue';
  import timeDialog from './components/timeDialog.vue';
  import { getFile } from '@/api/system/file';
  export default {
    components: {
      search,
      flow,
      release,
      Detail,
      timeDialog
    },
    data() {
      return {
        loading: false
      };
    },
    filters: {
      outsourceSceneText(text) {
        switch (text) {
          case 1:
            return '首工序委外';
          case 2:
            return '单工序';
          case 3:
            return '多工序';
          case 4:
            return '首工序及多工序';
          default:
            return '';
        }
      }
    },
    computed: {
      // 表格列配置
      columns() {
        return [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },

          {
            prop: 'batchNo',
            label: '批次号',
            align: 'center',
            minWidth: 100
          },
          {
            slot: 'code',
            prop: 'code',
            label: '委外单编码',
            align: 'center'
          },
          {
            prop: 'name',
            label: '委外单名称',
            align: 'center'
          },

          {
            prop: 'workOrderCode',
            label: '工单编码',
            align: 'center'
          },

          {
            prop: 'taskName',
            label: '工序',
            align: 'center'
          },

          {
            prop: 'totalCount',
            label: '委外数量',
            align: 'center'
          },

          {
            prop: 'totalWeight',
            label: '委外重量',
            align: 'center'
          },

          {
            slot: 'type',
            label: '委外类型',
            align: 'center'
          },
          {
            prop: 'outsourceScene',
            slot: 'outsourceScene',
            label: '委外场景',
            align: 'center'
          },
          {
            prop: 'remark',
            label: '备注',
            align: 'center'
          },

          {
            slot: 'requireDeliveryTime',
            label: '预计到货日期',
            align: 'center',
            width: 100
          },

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

          {
            prop: 'warehouseName',
            label: '仓库',
            align: 'center'
          },

          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            width: 100
          },

          {
            slot: 'status',
            label: '状态',
            align: 'center'
          },

          {
            label: '图片附件',
            slot: 'technicalDrawings',
            action: 'technicalDrawings',
            minWidth: 100
          },

          {
            label: '附件',
            slot: 'files',
            action: 'files',
            minWidth: 100
          },

          {
            columnKey: 'action',
            label: '操作',
            width: 140,
            align: 'center',
            resizable: false,
            fixed: 'right',
            slot: 'action',
            showOverflowTooltip: true
          }
        ];
      },

      clientEnvironmentId() {
        return this.$store.state.user.info.clientEnvironmentId;
      }
    },
    created() {},

    methods: {
      statusFormatter(status) {
        const obj = this.statusOpt.find((i) => i.value == status);
        return obj && obj.label;
      },
      /* 表格数据源 */
      datasource({ page, limit, where }) {
        return getList({
          pageNum: page,
          size: limit,
          ...where
        });
      },

      downloadFile(file) {
        getFile({ objectName: file.storePath }, file.name);
      },

      handleFlow(row) {
        if (!row.processInstanceId) {
          return this.$message.warning('暂无流程图');
        }
        this.$refs.flowRef.open(row.processInstanceId);
      },

      handleDetails(row) {
        this.$refs.detailsRef.open(row);
      },

      handleMethod(row) {
        this.$refs.timeDialogRef.open(row, 'details');
      },

      handleRelease(row) {
        this.$refs.releaseRef.open(row);
      },

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

      releaseRefresh() {
        this.$refs.table.reload();
      }
    }
  };
</script>

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