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

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        row-key="id"
        cache-key="pickKey"
        :selection.sync="selection"
        autoAmendPage
        :parse-data="parseData"
      >
        <template v-slot:toolbar>
          <el-button type="primary" size="mini" @click="handPick"
            >工单领料</el-button
          >
          <el-button type="primary" size="mini" @click="handSelfPick"
            >自建领料单</el-button
          >
        </template>
        <template v-slot:status="{ row }">
          <el-tag
            :type="row.status == '0' ? 'danger' : 'success'"
            effect="dark"
            >{{
              row.status == '0'
                ? '未领料'
                : row.status == '1'
                ? '领料中'
                : row.status == '2'
                ? '已出库'
                : ''
            }}</el-tag
          >
        </template>
      </ele-pro-table>
    </el-card>

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

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

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

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

  export default {
    components: {
      pickSearch,
      addPick,
      selfBuildPick
    },
    data() {
      return {
        // 加载状态
        loading: false,
        selection: [],

        addPickShow: false,
        selfBuildPickShow: false
      };
    },
    computed: {
      columns() {
        return [
          {
            prop: 'code',
            label: '领料单编号',
            align: 'center'
          },

          {
            prop: '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 res = await getPage({
          ...where,

          pageNum: page,
          size: limit
        });

        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;
      },

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