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

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        row-key="id"
        cache-key="returnKey"
        autoAmendPage
        :parse-data="parseData"
        :selection.sync="selection"
        :height="tableHeight"
        :pageSize="20"
        @fullscreen-change="fullscreenChange"
      >
        <template v-slot:toolbar>
          <el-button type="primary" size="mini" @click="handleReturn"
            >新建</el-button
          >
        </template>

        <template v-slot:scene="{ row }">
          {{ sceneListFn(row.scene) }}
        </template>

        <template v-slot:storageInfo="{ row }">
          <span v-if="row.storageInfo">
            {{ row.storageInfo.approvalUserName }}
          </span>
        </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>

    <returnPop
      v-if="returnShow"
      :returnDetailsId="returnDetailsId"
      @close="close"
      :sceneList="sceneList"
    ></returnPop>

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

<script>
  import { returnPage } from '@/api/materialReturn/index.js';
  import returnSearch from './components/return-search.vue';
  import flow from './components/flow.vue';
  import returnPop from './components/returnPop.vue';
  import { getByCode } from '@/api/system/dictionary-data';

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

        returnShow: false,
        returnDetailsId: null,

        sceneList: [],
        tableHeight: 'calc(100vh - 300px)'
      };
    },
    computed: {
      columns() {
        return [
          {
            prop: 'code',
            label: '退料单编号',
            align: 'center'
          },

          {
            prop: 'name',
            label: '退料单名称',
            align: 'center'
          },

          {
            slot: 'scene',
            prop: 'scene',
            label: '退料场景',
            align: 'center'
          },

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

          {
            prop: 'executorTime',
            label: '退料时间',
            align: 'center'
          },
          {
            prop: 'remark',
            label: '退料描述',
            align: 'center'
          },
          {
            prop: 'storageInfo',
            slot: 'storageInfo',
            label: '审核人',
            align: 'center'
          },

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

          pageNum: page,
          size: limit
        });

        return res;
      },

      // 新增退料
      handleReturn() {
        this.returnDetailsId = null;
        this.returnShow = true;
      },

      close(val) {
        if (val) {
          this.reload();
        }
        this.returnDetailsId = null;
        this.returnShow = false;
      },

      //查看详情
      handDetailed(row) {
        this.returnDetailsId = row.id;
        this.returnShow = true;
      },

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

      getByCodeFn() {
        getByCode('returnScenario').then((res) => {
          let _arr = [];
          res.data.map((item) => {
            const key = Object.keys(item)[0];
            const value = item[key];
            _arr.push({ label: value, value: key });
          });

          this.sceneList = _arr;
        });
      },

      sceneListFn(val) {
        let _arr = this.sceneList;
        for (const item of _arr) {
          if (item.value == val) {
            return item.label;
          }
        }
      },

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

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

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

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