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

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        row-key="id"
        cache-key="returnKey"
        autoAmendPage
      >
        

    
        <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/mes/materialReturn.js';
  import {getByCode} from "@/api/system/dictionary-data";
  import returnSearch from './return-search.vue';

  // import returnPop from './returnPop.vue';

  export default {
    components: {
      returnSearch,
      // returnPop
    },
    props: {
    saleOrderData: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
    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 }) {
        if (this.saleOrderData.id) {
        where['saleOrderCode'] = this.saleOrderData.orderNo;
      }
        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 });
      },

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

            idField: 'id',
            parentIdField: 'parentId'
          })
        };
      }
    }
  };
</script>
