<template>
  <div>
    <seek-page :seekList="seekList" @search="search"></seek-page>
    <ele-pro-table
      ref="table"
      row-key="id"
      :columns="columns"
      :datasource="datasource"
      :cache-key="cacheKeyUrl"
      autoAmendPage
      height="calc(86vh - 230px)"
    >
      <template v-slot:action="{ row }">
        <el-link type="primary" :underline="false" @click="goToDetail(row)">
          详情
        </el-link>
      </template>

      <template v-slot:code="{ row }">
        <el-link type="primary" @click="goToDetail(row)">{{
          row.code
        }}</el-link>
      </template>
    </ele-pro-table>

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

<script>
  import dictMixins from '@/mixins/dictMixins';
  import tableColumnsMixin from '@/mixins/tableColumnsMixin';
  import { batchRecordPage } from '@/api/returnmaterials/index';
  import returnPop from '@/views/materialReturn/components/returnPop.vue';
  import { getByCode } from '@/api/system/dictionary-data';

  export default {
    mixins: [dictMixins, tableColumnsMixin],
    components: { returnPop },
    props: {
      tableQuery: {
        type: Object,
        default: () => {
          return {};
        }
      }
    },
    data() {
      return {
        columns: [
          {
            width: 50,
            type: 'index',
            columnKey: 'index',
            align: 'center',
            label: '序号'
          },
          {
            prop: 'code',
            label: '退料单编号',
            align: 'center',
            minWidth: 110,
            showOverflowTooltip: true,
            slot: 'code'
          },
          {
            prop: 'name',
            label: '退料单名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'scene',
            label: '退料场景',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'createUserName',
            label: '退料人',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'createTime',
            label: '退料时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'workOrderCode',
            label: '关联领料单编号',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            label: '操作',
            columnKey: 'action',
            slot: 'action',
            showOverflowTooltip: true,
            minWidth: 110,
            align: 'center',
            fixed: 'right'
          }
        ],
        cacheKeyUrl: 'mes-259231042-material-return-table',
        returnShow: false,
        returnDetailsId: null
      };
    },
    computed: {
      seekList() {
        return [
          {
            label: '工单编码：',
            value: 'workOrderCode',
            type: 'input',
            placeholder: '请输入'
          },
          {
            label: '退料单编号',
            value: 'returnMaterialsCode',
            type: 'input',
            placeholder: '请输入'
          },
          {
            label: '退料单名称',
            value: 'returnMaterialsName',
            type: 'input',
            placeholder: '请输入'
          }
        ];
      }
    },
    created() {
      this.getByCodeFn();
    },
    methods: {
      // 刷新表格
      reload(where = {}) {
        this.$refs.table.reload({
          where,
          ...this.tableQuery
        });
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        // 参数
        const body = {
          ...where,
          ...order,
          pageNum: page,
          size: limit,
          ...this.tableQuery
        };
        return batchRecordPage(body);
      },
      search(where) {
        this.reload(where);
      },
      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;
        });
      },
      goToDetail(row) {
        this.returnDetailsId = row.id;
        this.returnShow = true;
      },
      close(val) {
        if (val) {
          this.reload();
        }
        this.returnDetailsId = null;
        this.returnShow = false;
      }
    }
  };
</script>

<style></style>
