<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"
        :selection.sync="selection"
      >
        <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:action="{ row }">
          <el-button type="text" size="mini" @click="handDetailed(row)"
            >详情</el-button
          >
        </template>
      </ele-pro-table>
    </el-card>

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

<script>
  import { returnPage } from '@/api/materialReturn/index.js';

  import returnSearch from './components/return-search.vue';

  import returnPop from './components/returnPop.vue';
  import { getByCode } from '@/api/system/dictionary-data';

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

        returnShow: false,
        returnDetailsId: null ,

        sceneList: []
      };
    },
    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: '',
            label: '操作',
            width: 80,
            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
      },

      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 });
      }
    }
  };
</script>
