<template>
  <div>
    <seek-page :seekList="seekList" @search="search"></seek-page>
    <ele-pro-table
      ref="table"
      row-key="id"
      :columns="columns"
      :datasource="datasource"
      :cache-key="cacheKeyUrl"
      autoAmendPage
      height="calc(86vh - 230px)"
    >
      <template v-slot:action="{ row }">
        <el-link type="primary" :underline="false" @click="goToDetail(row)">
          详情
        </el-link>
      </template>

      <template v-slot:code="{ row }">
        <el-link type="primary" @click="goToDetail(row)">{{
          row.code
        }}</el-link>
      </template>
      <template v-slot:status="{ row }">
        <el-tag
          :type="['info', 'success', 'warning', 'danger'][row.status]"
          effect="dark"
          >{{ statusList[row.status] }}</el-tag
        >
      </template>
    </ele-pro-table>

    <detailed
      @detailedClose="detailedClose"
      v-if="detailedShow && detailedObj"
      :detailedObj="detailedObj"
    ></detailed>

    <selfDetailed
      @detailedClose="detailedClose"
      v-if="selfDetailedShow && detailedObj"
      :detailedObj="detailedObj"
    ></selfDetailed>
  </div>
</template>

<script>
  import dictMixins from '@/mixins/dictMixins';
  import tableColumnsMixin from '@/mixins/tableColumnsMixin';
  import { batchRecordPage } from '@/api/pickorder/index';
  import detailed from '@/views/produce/components/picking/detailed.vue';
  import selfDetailed from '@/views/pick/pickApply/components/selfDetailed.vue';

  import { getDetails } from '@/api/pick/pickApply';

  export default {
    mixins: [dictMixins, tableColumnsMixin],
    components: { detailed, selfDetailed },
    props: {
      tableQuery: {
        type: Object,
        default: () => {
          return {};
        }
      }
    },
    data() {
      return {
        columns: [
          {
            width: 50,
            type: 'index',
            columnKey: 'index',
            align: 'center',
            label: '序号'
          },
          {
            prop: 'code',
            label: '领料单编码',
            align: 'center',
            minWidth: 110,
            showOverflowTooltip: true,
            slot: 'code'
          },
          {
            prop: 'workOrderCode',
            label: '关联工单编号',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'warehouseName',
            label: '领料仓库名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'createUserName',
            label: '领料人',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'createTime',
            label: '领料时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'status',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150,
            slot: 'status'
          },
          {
            label: '操作',
            columnKey: 'action',
            slot: 'action',
            showOverflowTooltip: true,
            minWidth: 110,
            align: 'center',
            fixed: 'right'
          }
        ],
        cacheKeyUrl: 'mes-259231040-material-table',
        detailedShow: false,
        selfDetailedShow: false,
        detailedObj: null,
        statusList: ['未领料', '领料中', '已出库', '已驳回']
      };
    },
    computed: {
      seekList() {
        return [
          {
            label: '工单编码：',
            value: 'workOrderCode',
            type: 'input',
            placeholder: '请输入'
          },
          {
            label: '领料单编码：',
            value: 'pickOrderCode',
            type: 'input',
            placeholder: '请输入',
            labelWidth: 110
          }
          // {
          //   label: '领料名称：',
          //   value: 'pickOrderName',
          //   type: 'input',
          //   placeholder: '请输入'
          // }
        ];
      }
    },
    methods: {
      // 刷新表格
      reload(where = {}) {
        this.$refs.table.reload({
          where,
          ...this.tableQuery
        });
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        // 参数
        const body = {
          ...where,
          ...order,
          pageNum: page,
          size: limit,
          ...this.tableQuery
        };
        return batchRecordPage(body);
      },
      search(where) {
        this.reload(where);
      },
      async goToDetail(row) {
        const res = await getDetails(row.id);

        this.detailedObj = JSON.stringify(res);

        this.$nextTick(() => {
          if (this.detailedObj.type == 1) {
            this.selfDetailedShow = true;
          } else {
            this.detailedShow = true;
          }
        });
      },
      detailedClose() {
        this.detailedShow = false;
        this.selfDetailedShow = false;
      }
    }
  };
</script>

<style></style>
