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

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        row-key="id"
        cache-key="pickKey"
        :selection.sync="selection"
        autoAmendPage
        :parse-data="parseData"
        @update:selection="handleSelectionChange"
      >
        <template v-slot:toolbar>
          <el-button type="primary" size="mini" @click="handByProd">新建回收单</el-button>
        </template>

        <template v-slot:formedNum="{ row }">
          {{ row.notFormedNum || 0 }} / {{ row.formedNum || 0 }}
        </template>

        <template v-slot:weight="{ row }">
          {{ row.weight || 0 }} / {{ row.weightUnit }}
        </template>

        <template v-slot:action="{ row }">
          <el-button type="text" size="mini">详情</el-button>
        </template>
      </ele-pro-table>
    </el-card>


    <addByProduct v-if="addByProductShow" @close="close"></addByProduct>
    

  </div>
</template>

<script>
  import { getPage } from '@/api/byProduct/index';
  import byProductSearch from './components/byProduct-search.vue';
  import  addByProduct from './components/addByProduct.vue';

  export default {
    components: {
      byProductSearch,
      addByProduct
    },

    data() {
      return {
        // 加载状态
        loading: false,
        selection: [],

        addByProductShow: false
      };
    },
    computed: {
      columns() {
        return [
        {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'code',
            label: '回收单号',
            align: 'left',
            width: '300'
          },

          {
            prop: 'workOrderCode',
            label: '工单编码',
            align: 'center'
          },
          {
            prop: 'taskName',
            label: '工序',
            align: 'center'
          },

          {
            prop: 'categoryCode',
            label: '物品编码',
            align: 'center'
          },

          {
            prop: 'categoryName',
            label: '物品名称',
            align: 'center'
          },

          {
            prop: 'formedNum',
            slot: 'formedNum',
            label: '不合格品/合格品数量',
            align: 'center',
            width: 140
          },

          {
            prop: 'weight',
            slot: 'weight',
            label: '重量',
            align: 'center',
            width: 140
          },

          {
            prop: 'executorName',
            label: '回收人',
            align: 'center',
            width: 95
          },
          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            width: 95
          },

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

        let res = await getPage(parma);

        return res;
      },

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

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

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


      handByProd() {
        this.addByProductShow = true;
      },
      close(val) {
        if(val) {
           this.reload();

        }
        this.addByProductShow = false;
      },
      
    }
  };
</script>
