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

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        row-key="id"
        cache-key="pickKey"
        :selection.sync="selection"
        autoAmendPage
        :parse-data="parseData"
        @update:selection="handleSelectionChange"
      >
        <template v-slot:toolbar>
          <el-button
            type="primary"
            v-if="!isReturn"
            size="mini"
            @click="handPick"
            >工单领料</el-button
          >
          <el-button
            type="primary"
            v-if="!isReturn"
            size="mini"
            @click="handSelfPick"
            >自建领料单</el-button
          >
        </template>

        <template v-slot:joinWorkOrderCode="{ row }">
          <el-tag type="warning" v-if="row.type == 1">自建领料单</el-tag>
          <div v-else>{{ row.joinWorkOrderCode }}</div>
        </template>

        <template v-slot:status="{ row }">
          <el-tag
            :type="['danger', 'success', 'warning', 'danger'][row.status]"
            effect="dark"
            >{{ statusList[row.status] }}</el-tag
          >
        </template>

        <template v-slot:action="{ row }">
          <el-button type="text" size="mini" @click="handDetailed(row)"
            >详情</el-button
          >
        </template>
      </ele-pro-table>
    </el-card>

    <addPick v-if="addPickShow" @close="close"></addPick>
    <selfBuildPick v-if="selfBuildPickShow" @close="close"></selfBuildPick>

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

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

<script>
  import { getPage, returnPage } from '@/api/pick/pickApply';

  import pickSearch from './components/pick-search.vue';

  import addPick from './components/addPick.vue';
  import selfBuildPick from './components/selfBuildPick.vue';

  import detailed from '@/views/produce/components/picking/detailed.vue';
  import selfDetailed from './components/selfDetailed.vue';

  export default {
    components: {
      pickSearch,
      addPick,
      selfBuildPick,
      detailed,
      selfDetailed
    },

    props: {
      isReturn: {
        type: Boolean,
        default() {
          return false;
        }
      }
    },
    data() {
      return {
        // 加载状态
        loading: false,
        selection: [],

        addPickShow: false,
        selfBuildPickShow: false,

        detailedShow: false,
        detailedObj: null,

        selfDetailedShow: false,

        statusList: ['未领料', '领料中', '已出库', '已驳回']
      };
    },
    computed: {
      columns() {
        return [
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center',
            reserveSelection: true,
            show: this.isReturn
          },
          {
            prop: 'code',
            label: '领料单编号',
            align: 'center',
            align: 'left'
          },

          {
            prop: 'joinWorkOrderCode',
            slot: 'joinWorkOrderCode',
            label: '关联工单编号',
            align: 'center'
          },

          {
            prop: 'joinWarehouseName',
            label: '领料仓库名称',
            align: 'center'
          },

          {
            prop: 'joinReviewerName',
            label: '关联审核人',
            align: 'center'
          },

          {
            prop: 'executorName',
            label: '领料人',
            align: 'center'
          },

          {
            prop: 'createTime',
            label: '领料时间',
            align: 'center'
          },
          {
            prop: 'status',
            slot: 'status',
            label: '状态',
            align: 'center'
          },

          {
            prop: '',
            label: '操作',
            width: 80,
            align: 'center',
            resizable: false,
            fixed: 'right',
            slot: 'action'
          }
        ];
      }
    },
    created() {},
    methods: {
      /* 表格数据源 */
      async datasource({ page, limit, where }) {
        let URL = this.isReturn ? returnPage : getPage;

        let parma = {
          ...where,
          pageNum: page,
          size: limit
        };
        if (this.isReturn) {
          parma.status = 2;
        }

        let res = await URL(parma);

        return res;
      },

      /* 数据转为树形结构 */
      parseData(data) {
        return {
          ...data,
          list: this.$util.toTreeData({
            data: data.list,
            count: data.total,

            idField: 'id',
            parentIdField: 'parentId'
          })
        };
      },

      handPick() {
        this.addPickShow = true;
      },

      close(val) {
        if (val) {
          this.reload();
        }
        this.addPickShow = false;
        this.selfBuildPickShow = false;
      },

      handSelfPick() {
        this.selfBuildPickShow = true;
      },

      selfClose(val) {
        if (val) {
          this.reload();
        }
        this.selfBuildPickShow = false;
      },

      handDetailed(row) {
        this.detailedObj = JSON.stringify(row);
        if (row.type == 1) {
          this.selfDetailedShow = true;
        } else {
          this.detailedShow = true;
        }
      },

      detailedClose() {
        this.detailedShow = false;
        this.selfDetailedShow = false;
      },

      handleSelectionChange(data) {
        let ids = [];
        ids = data.map((item) => item.id);
        this.$emit('selectionChange', ids);
      },
      /* 刷新表格 */
      reload(where = {}) {
        this.$refs.table.reload({ page: 1, where });
      }
    }
  };
</script>
