<!-- 物品信息表 -->
<template>
  <el-dialog
    :title="`添加${typeName}`"
    :visible.sync="dialogVisible"
    width="70%"
    :before-close="handleClose"
    append-to-body
  >
    <div class="search_wrapper">
      <el-input
        v-model="code"
        class="search_input"
        placeholder="请输入编码"
      ></el-input>
      <el-button type="primary" @click="doSearch">搜索</el-button>
      <el-button type="primary" @click="reset">重置</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"
          id="0"
          :paramsType="'type'"
          ref="treeList"
          eqDevice="sb"
        />
        <!-- <AssetTree
          @handleNodeClick="handleNodeClick"
          @setRootId="setRootId"
          :type="type"
          :paramsType="'type'"
          ref="treeList"
          id="4"
        /> -->
      </el-col>
      <el-col class="table_box" :span="18">
        <el-table
          ref="dialogMultipleTable"
          :data="tableData"
          tooltip-effect="dark"
          v-loading="tableLoading"
          style="width: 100%; overflow: auto"
          height="80vh"
          border
          @select-all="onSelectAll"
          @selection-change="selectItem"
          :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="fixCode" label="固资编码"></el-table-column>
          <el-table-column prop="codeNumber" 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>
        <div class="pagination">
          <el-pagination
            background
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            :page-sizes="[10, 30, 50]"
            :page-size="pageSize"
            :current-page.sync="pageNum"
            @current-change="handleCurrent"
            @size-change="handleSize"
          >
          </el-pagination>
        </div>
      </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 { getAssetList } from '@/api/ledgerAssets';
  import { tableHeader } from '@/utils/category.js';
  import dictMixins from '@/mixins/dictMixins';
  export default {
    components: {
      // CommonTree,
      AssetTree
    },
    mixins: [dictMixins],
    props: {
      // 类型
      type: {
        type: String,
        default: () => ''
      },
      // 反显列表
      memoList: {
        type: Array,
        default: () => []
      }
    },
    data() {
      return {
        dialogVisible: false,
        code: '',
        tableLoading: false,
        tableData: [],
        currentTreeData: {},
        selectionList: [],
        rootId: null,
        pageNum: 1,
        pageSize: 10,
        total: 0
      };
    },
    created() {
      this.requestDict('类型用途');
    },
    computed: {
      typeName() {
        return this.getDictValue('类型用途', this.type);
      },
      tableHeader() {
        return tableHeader(this.type);
      }
    },
    methods: {
      open() {
        this.selectionList = [];
        this.dialogVisible = true;
      },

      async doSearch() {
        this._getClassificationList();
      },

      async reset() {
        this.code = '';
        this._getClassificationList();
      },

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

      // 树结构点击事件
      async handleNodeClick(data, node) {
        this.rootId = data.parentId;
        this.currentTreeData = data;
        this._getClassificationList();
      },
      handleCurrent(page) {
        this.pageNum = page;
        this._getClassificationList();
      },
      handleSize(size) {
        this.pageNum = 1;
        this.pageSize = size;
        this._getClassificationList();
      },
      // 获取添物品信息表格数据
      async _getClassificationList() {
        this.tableLoading = true;
        let params = {
          pageNum: this.pageNum,
          size: this.pageSize,
          categoryLevelId: this.currentTreeData.id,
          rootCategoryLevelId: this.rootId,
          code: this.code
        };
        const res = await getAssetList(params);
        this.tableLoading = false;
        if (res.list.length) {
          this.total = res.count;
          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 = [];
        }
      },
      // 备件单选框选择时事件
      onSelectAll() {
        this.$refs.dialogMultipleTable.clearSelection();
      },
      selectItem(rows) {
        if (rows.length > 1) {
          const newRows = rows.filter((it, index) => {
            if (index == rows.length - 1) {
              this.$refs.dialogMultipleTable.toggleRowSelection(it, true);
              return true;
            } else {
              this.$refs.dialogMultipleTable.toggleRowSelection(it, false);
              return false;
            }
          });
          this.selectionList = newRows;
        } else {
          this.selectionList = rows;
        }
      },

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

      async submit() {
        this.$emit('success', this.selectionList[0].id);
        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;
    }
  }
  .table_box {
    display: flex;
    flex-direction: column;
    .el-table {
      flex: 1;
    }
    .pagination {
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
</style>
