<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"
        cache-key="pickKey"
        :selection.sync="selection"
        autoAmendPage
        :parse-data="parseData"
        @update:selection="handleSelectionChange"
        :pageSize="20"
        :height="tableHeight"
        @fullscreen-change="fullscreenChange"
      >
        <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:code="{ row }">
          <el-link type="primary" :underline="false" @click="handDetailed(row)">
            {{ row.code }}
          </el-link>
        </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:type="{ row }">
          <el-tag type="warning" v-if="row.type == 1">自建领料</el-tag>
          <el-tag type="success" v-if="row.type == 2">工单领料</el-tag>
          <el-tag type="info" v-if="row.type == 3">委外领料</el-tag>
        </template>

        <template v-slot:status="{ row }">
          <el-tag
            :type="['info', '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
          > -->

          <el-button type="text" size="mini" @click="checkProcess(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>

    <flow ref="flowRef"></flow>
  </div>
</template>

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

  import pickSearch from './components/pick-search.vue';
  import flow from '@/views/materialReturn/components/flow.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,
      flow
    },

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

        addPickShow: false,
        selfBuildPickShow: false,

        detailedShow: false,
        detailedObj: null,

        selfDetailedShow: false,

        statusList: ['未领料', '领料中', '已出库', '已驳回'],
        tableHeight: 'calc(100vh - 300px)'
      };
    },
    computed: {
      columns() {
        return [
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center',
            reserveSelection: true,
            show: this.isReturn
          },
          {
            prop: 'code',
            slot: '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: 'type',
            slot: 'type',
            label: '类型',
            align: 'center'
          },
          {
            prop: 'status',
            slot: 'status',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true
          },

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

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

      //查看流程
      checkProcess(row) {
        if (!row.processInstanceId) {
          return this.$message.warning('暂无流程图');
        }
        this.$refs.flowRef.open(row.processInstanceId);
      },

      fullscreenChange(fullscreen) {
        if (fullscreen) {
          this.tableHeight = 'calc(100vh - 120px)';
        } else {
          this.tableHeight = 'calc(100vh - 300px)';
        }
      }
    }
  };
</script>




