<template>
  <div>
    <el-dialog
      title="选择委外订单"
      :visible.sync="dialogVisible"
      width="90%"
      :before-close="handleClose"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="main">
        <ele-pro-table
          ref="table"
          :initLoad="false"
          :columns="columns"
          :current.sync="current"
          highlight-current-row
          :datasource="datasource"
          tool-class="ele-toolbar-form"
          cache-key="systemOrgUserTable"
          @row-click="chooseRow"
        >
          <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>
          </template>

          <template v-slot:status="{ row }">
            <el-tag>{{ row.status == 1 ? '已发布' : '未发布' }}</el-tag>
          </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>
          </template>
        </ele-pro-table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleMine">确 定</el-button>
      </span>
    </el-dialog>
    <detail ref="detailsRef"></detail>
  </div>
</template>

<script>
  import { getOutsourcedOrdersList } from '@/api/mes';
  import detail from './details.vue';
  export default {
    components: { detail },
    data() {
      return {
        dialogVisible: false,
        pages: {
          pageNum: 1,
          size: 10
        },
        total: 0,
        tableData: [],
        current: {},
        columns: [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },

          {
            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: 'remark',
            label: '备注',
            align: 'center'
          },

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

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

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

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

          {
            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
          }
        ]
      };
    },
    methods: {
      async open() {
        const listParent = this.$parent.outsourcedOrdersRow;
        if (JSON.stringify(listParent) == '{}') {
          this.current = null;
        }
        this.dialogVisible = true;
        this.$nextTick(() => {
          this.$refs.table.reload({
            pageNum: 1,
            size: 10,
            where: {}
          });
        });
      },
      handleDetails(row) {
        this.$refs.detailsRef.open(row);
      },
      async handleMine() {
        if (this.current) {
          this.$emit('success', this.current);
          this.dialogVisible = false;
        } else {
          this.$message.warning('请先选择委外单！');
        }
      },
      async datasource({ page, limit, where }) {
        const { data } = await getOutsourcedOrdersList({
          pageNum: page,
          size: limit,
          ...where
        });
        console.log(data);
        return data;
      },
      chooseRow(row) {
        this.current = row;
      },
      handleClose() {
        this.dialogVisible = false;
      }
    }
  };
</script>
