<template>
  <el-dialog
    title="添加备品备件"
    :visible.sync="dialogVisible"
    width="70%"
    :before-close="handleClose"
    append-to-body
  >
    <div class="search_wrapper">
      <el-input
        size="small"
        v-model="searchParams.code"
        class="search_input"
        placeholder="请输入编码"
      ></el-input>
      <el-button type="primary" size="small" @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="'7'"
          :paramsType="'type'"
          ref="treeList"
          id="6"
        />
      </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
            type="selection"
            label="操作"
            width="55"
            align="center"
          />
          <el-table-column prop="code" label="备件类别编码" />
          <el-table-column prop="name" label="备件类别名称" />
          <el-table-column label="型号" prop="modelType"> </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 { getCategoryList } from '@/api/ledgerAssets';
  import { getList } from '@/api/classifyManage/itemInformation';
  export default {
    components: {
      // CommonTree,
      AssetTree
    },
    props: {
      selectedSpare: {
        type: Array,
        default: () => []
      }
    },
    data() {
      return {
        dialogVisible: false,
        searchParams: {
          code: '',
          page: 1,
          size: 99999
        },
        tableLoading: false,
        tableData: [],
        currentTreeData: {},
        pickSpareDataObject: [],
        rootId: null
      };
    },
    watch: {
      selectedSpare(val) {
        val.map((item) => {
          this.tableData.forEach((c) => {
            if (item.id === c.id) {
              this.$refs.dialogMultipleTable.toggleRowSelection(c, true);
            }
          });
        });
      }
    },
    methods: {
      open() {
        this.pickSpareDataObject = [];
        this.dialogVisible = true;
        this.$nextTick(() => {
          this.selectedSpare.map((item) => {
            this.tableData.forEach((c) => {
              if (item.id === c.id) {
                this.$refs.dialogMultipleTable.toggleRowSelection(c, true);
              }
            });
          });
        });
      },

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

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

      // 树结构点击事件
      async handleNodeClick(data, node) {
        this.currentTreeData = data;
        this._getClassificationSpareList(data);
      },

      // setDefaultList(id){
      //   let data = {id:id}
      //    this._getClassificationSpareList(data)
      // },

      // 获取添加备件弹窗表格数据
      async _getClassificationSpareList(data) {
        console.log(1);
        this.tableLoading = true;
        let params = {
          page: 1,
          size: 10,
          categoryLevelId: this.currentTreeData.id
        };
        const res = await getList(params);
        console.log('res', res);
        this.tableLoading = false;
        if (res.list.length) {
          this.tableData = res.list;
          // 切换树节点时回显添加了的备品备件
          this.$nextTick(() => {
            this.selectedSpare.map((item) => {
              this.tableData.forEach((c) => {
                if (item.id === c.id) {
                  this.$refs.dialogMultipleTable.toggleRowSelection(c, true);
                }
              });
            });
          });
        } else {
          this.tableData = [];
        }
      },
      // 备件单选框选择时事件
      pickSpareData(data) {
        this.pickSpareDataObject = data;
      },

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

      async submit() {
        this.$emit('submit', this.pickSpareDataObject);
        this.handleClose();
        //   this.form.exportList = this.pickSpareDataObject
        //   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>
