<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">自建领料单</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>
  </div>
</template>

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

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

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

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

        addPickShow: 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() {
        this.addPickShow = false;
      },

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