<!-- 物品信息表 -->
<template>
  <el-dialog
    :title="`添加${typeName}`"
    :visible.sync="dialogVisible"
    width="70%"
    :before-close="handleClose"
    append-to-body
  >
    <div class="search_wrapper">
      <el-input
        v-model="searchParams.code"
        class="search_input"
        placeholder="请输入编码"
      ></el-input>
      <el-button type="primary" @click="doSearch">搜索</el-button>
    </div>
    <el-row style="height: 80vh">
      <el-col class="tree-col" :span="6">
        <div class="table-add" style="margin-left: 10px"></div>
        <AssetTree
          @handleNodeClick="handleNodeClick"
          @setRootId="setRootId"
          :type="type"
          :paramsType="'type'"
          ref="treeList"
        />
      </el-col>
      <el-col :span="18">
        <el-table
          ref="dialogMultipleTable"
          :data="tableData"
          tooltip-effect="dark"
          v-loading="tableLoading"
          style="width: 100%; overflow: auto"
          height="80vh"
          border
          @select="pickSpareData"
          @select-all="pickSpareData"
          :header-cell-style="{ background: '#F0F3F3' }"
        >
          <el-table-column prop="code" label="物品编码"></el-table-column>
          <el-table-column prop="name" label="物品名称"></el-table-column>
          <el-table-column
            :prop="item.prop"
            :label="item.label"
            v-for="(item, index) in tableHeader"
            :key="index"
          >
            <template slot-scope="{ row }">
              <template v-if="item.formatter">{{
                item.formatter(row)
              }}</template>
              <template v-else>{{ row[item.prop] }}</template>
            </template></el-table-column
          >

          <el-table-column type="selection" width="55" align="center">
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>

    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="submit" type="primary">提 交</el-button>
      <el-button size="small" @click="handleClose">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import AssetTree from '@/components/AssetTree';
  import { getList as getCategoryList } from '@/api/classifyManage/itemInformation';
  import { tableHeader } from '@/utils/category.js';
  import dictMixins from '@/mixins/dictMixins';
  export default {
    components: {
      // CommonTree,
      AssetTree
    },
    mixins: [dictMixins],
    props: {
      // 类型
      type: {
        type: Array,
        default: () => []
      },
      // 反显列表
      memoList: {
        type: Array,
        default: () => []
      }
    },
    data () {
      return {
        dialogVisible: false,
        searchParams: {
          code: '',
          page: 1,
          size: 99999
        },
        tableLoading: false,
        tableData: [],
        currentTreeData: {},
        selectionList: [],
        rootId: null
      };
    },
    created () {
      this.requestDict('类型用途');
    },
    computed: {
      typeName () {
        return this.getDictValue('类型用途', this.type);
      },
      tableHeader () {
        return tableHeader(this.type);
      }
    },
    methods: {
      open () {
        this.selectionList = [];
        this.dialogVisible = true;
      },

      async doSearch () {
        const params = {
          categoryLevelId: this.currentTreeData.id,
          rootCategoryLevelId: this.rootId,
          code: this.searchParams.code
        };
        this._getClassificationList(params);
      },

      // 获取根节点id
      setRootId (id) {
        this.rootId = id;
      },

      // 树结构点击事件
      async handleNodeClick (data, node) {
        this.currentTreeData = data;
        this._getClassificationList(data);
      },
      // 获取添物品信息表格数据
      async _getClassificationList (data) {
        this.tableLoading = true;
        let params = {
          page: 1,
          size: 99999,
          categoryLevelId: this.currentTreeData.id
        };
        const res = await getCategoryList(params);
        this.tableLoading = false;
        if (res.list.length) {
          this.tableData = res.list;
          // 切换树节点时回显添加了的备品备件
          this.$nextTick(() => {
            this.memoList.map((item) => {
              this.tableData.forEach((c) => {
                if (item.id === c.id) {
                  this.$refs.dialogMultipleTable.toggleRowSelection(c, true);
                }
              });
            });
          });
        } else {
          this.tableData = [];
        }
      },
      // 备件单选框选择时事件
      pickSpareData (data) {
        this.selectionList = data;
      },

      // 关闭弹窗
      handleClose () {
        this.$refs.dialogMultipleTable.clearSelection();
        this.dialogVisible = false;
      },

      async submit () {
        this.$emit('success', this.selectionList);
        this.handleClose();
        //   this.form.exportList = this.selectionList
        //   this.$forceUpdate()
      }
    }
  };
</script>

<style lang="scss" scoped>
  .tree-col {
    border: 1px solid #e6ebf5;
    margin-right: 10px;
    width: 20%;
    padding: 0 !important;
    height: 100%;
    overflow: auto;
  }
  .search_wrapper {
    margin-bottom: 10px;
    .search_input {
      width: 200px;
      margin-right: 10px;
    }
  }
</style>
