<template>
  <ele-modal
    width="80vw"
    :visible.sync="visible"
    :close-on-click-modal="false"
    row-key="code"
    custom-class="ele-dialog-form"
    :title="'库存详情'"
    :maxable="true"
  >
    <div class="form-wrapper">
      <el-form ref="form" :model="form" :rules="rules">
        <ele-pro-table
          ref="table"
          :needPage="true"
          :columns="columns"
          :datasource="datasource"
          :init-load="false"
        >
          <template v-slot:action="{ $index }">
            <el-link type="primary" :underline="false" icon="el-icon-view">
              库存
            </el-link>
            <el-link type="primary" :underline="false" icon="el-icon-view">
              在途
            </el-link>
          </template>
        </ele-pro-table>
      </el-form>
    </div>
    <div slot="footer">
      <el-button plain @click="cancel">取消</el-button>
      <el-button type="primary" @click="confirm">确定</el-button>
    </div>
  </ele-modal>
</template>

<script>
  import { getStockList } from '@/api/beEntrusted';
  export default {
    components: {},
    data() {
      return {
        visible: false,

        formData: {},
        requestData: {
          deviceCode: '',
          deviceName: '',
          deviceId: ''
        },
        requiredFormingNum: 0,

        form: {
          stockList: []
        },

        rules: {},

        columns: [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'categoryCode',
            label: '物品编码',
            align: 'center',
            minWidth: 100,
            showOverflowTooltip: true
          },
          {
            prop: 'categoryName',
            label: '物品名称',
            align: 'center',
            minWidth: 100
          },
          {
            prop: 'modelType',
            label: '型号',
            align: 'center',
            minWidth: 100,
            showOverflowTooltip: true
          },
          {
            prop: 'specification',
            label: '规格',
            align: 'center',
            minWidth: 100
          },
          {
            prop: 'level',
            label: '级别',
            align: 'center',
            minWidth: 100
          },
          {
            prop: 'measureQuantity',
            label: '计量数量',
            align: 'center',
            minWidth: 100
          }
        ],
        activeName: 'first',
        selection: [],
        request: {
          categoryCode: ''
        }
      };
    },
    methods: {
      reload(where) {
        this.$nextTick(() => {
          this.$refs.table.reload({ page: 1, where });
        });
      },
      /* 表格数据源 */
      datasource({ page, limit, where }) {
        return getStockList({
          pageNum: page,
          size: limit,
          ...where
        });
      },
      handleSelectionChange(list) {
        if (list.length > 1) {
          let _list = [];
          list.forEach((e) => {
            if (e.childList.length > 0 && e.splitBatch != 2) {
              _list.push(...e.childList);
            } else {
              _list.push(e);
            }
          });
          this.selection = _list;
        } else {
          this.selection = [];
        }
      },
      /* 数据转为树形结构 */
      parseData(data) {
        return {
          ...data,
          list: this.$util.toTreeData({
            data: data.list,
            count: data.total,

            idField: 'code',
            parentIdField: 'joinPlanCode'
          })
        };
      },
      onSortChange(e) {
        let sort = {
          orderBy: e.order,
          sortName: e.prop
        };
        this.sort = sort;
        this.reload();
      },
      open(row) {
        this.visible = true;
        this.reload({ categoryCode: row.bomCode });
      },
      cancel() {
        this.formData = {};
        this.visible = false;
      },

      confirm() {
        this.visible = false;
      }
    }
  };
</script>

<style lang="scss" scoped>
  .mt20 {
    margin-top: 20px;
  }

  .el-form-item {
    margin-bottom: 0 !important;
  }
</style>
