<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="by_product_list"
        :selection.sync="selection"
        autoAmendPage
        :parse-data="parseData"
        @update:selection="handleSelectionChange"
        :height="tableHeight"
        @fullscreen-change="fullscreenChange"
        :pageSize="20"
      >
        <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> -->
          <el-button type="text" size="mini" @click="disopseProduct(row)"
            >处置</el-button
          >
        </template>
      </ele-pro-table>
    </el-card>

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

    <el-dialog
      title="仓库处置"
      :visible.sync="visible"
      v-if="visible"
      :before-close="handleClose"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body
      width="500px"
    >
      <el-form size="mini" :model="form" label-width="96px">
        <el-form-item label="仓库" prop="warehouseList">
          <el-select
            v-model="form.warehouseId"
            filterable
            placeholder="请选择仓库"
          >
            <el-option
              v-for="item in warehouseList"
              :label="item.name"
              :value="item.id"
              :key="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <template slot="footer">
        <el-button size="mini" @click="visible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="warehouseSave"
          >保 存</el-button
        >
      </template>
    </el-dialog>
  </div>
</template>

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

  export default {
    components: {
      byProductSearch,
      addByProduct
    },

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

        addByProductShow: false,
        visible: false,
        warehouseList: [],
        form: {
          warehouseId: ''
        },
        tableHeight: 'calc(100vh - 300px)',
        disposeData: null
      };
    },
    computed: {
      columns() {
        return [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'code',
            label: '回收单号',
            align: 'left',
            width: '300',
            showOverflowTooltip: true
          },

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

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

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

          {
            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,
            showOverflowTooltip: true
          },

          {
            prop: '',
            label: '操作',
            width: 80,
            align: 'center',
            resizable: false,
            fixed: 'right',
            slot: 'action'
          }
        ];
      }
    },
    created() {
      getWarehouseList().then((res) => {
        this.warehouseList = res;
      });
    },
    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 });
      },

      handleClose() {
        this.visible = false;
      },

      warehouseSave() {
        if (!this.form.warehouseId) {
          return this.$message.warning('请选择仓库');
        }

        const data = this.warehouseList.filter(
          (it) => this.form.warehouseId == it.id
        );

        let parm = {
          id: this.disposeData.id,
          warehouseCode: data[0].code,
          warehouseName: data[0].name,
          warehouseId: data[0].id
        };

        byProductDisposal(parm).then((res) => {
          this.$message.success('处置成功');
          this.reload();
          this.visible = false;
        });
      },

      disopseProduct(item) {
        this.disposeData = item;
        this.form.warehouseId = '';
        this.visible = true;
      },

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

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