<template>
  <el-row>
    <el-col :span="5">
      <div class="box">
        <el-scrollbar>
          <basic-container>
            <avue-tree :option="treeOption" :data="treeData" @node-click="nodeClick"/>
          </basic-container>
        </el-scrollbar>
      </div>
    </el-col>
    <el-col :span="19">
      <basic-container>
        <avue-crud :option="option"
                   :data="dataList"
                   ref="crud"
                   v-model="form"
                   :page.sync="page"
                   @row-del="rowDel"
                   @row-update="rowUpdate"
                   :before-open="beforeOpen"
                   :before-close="beforeClose"
                   @row-save="rowSave"
                   @search-change="searchChange"
                   @search-reset="searchReset"
                   @selection-change="selectionChange"
                   @current-change="currentChange"
                   @size-change="sizeChange"
                   @refresh-change="refreshChange"
                   @on-load="onLoad"
                   @tree-load="treeLoad">
        </avue-crud>
      </basic-container>
    </el-col>
  </el-row>
</template>

<script>
import option from "./configuration/mainList.json";
import {customerList, typeSave,detail,deleteDetails,getDeptLazyTree,getDeptTree} from "@/api/basicData/basicStorageDesc"

export default {
  name: "customerInformation",
  data() {
    return {
      form: {},
      option: option,
      treeOption: {
        nodeKey: 'id',
        lazy: true,
        treeLoad: function (node, resolve) {
          const parentId = (node.level === 0) ? 0 : node.data.id;
          getDeptLazyTree(parentId).then(res => {
            resolve(res.data.data.map(item => {
              return {
                ...item,
                leaf: !item.hasChildren
              }
            }))
          });
        },
        addBtn: false,
        menu: false,
        size: 'small',
        props: {
          labelText: '标题',
          label: 'title',
          value: 'value',
          children: 'children'
        }
      },
      parentId:0,
      dataList: [],
      page: {
        pageSize: 10,
        pagerCount: 5,
        total: 0,
      },
      treeDeptId:'',
    }
  },
  created() {
    // customerList({parentId:0}).then(res => {
    //   console.log(res.data.data.records)
    //   this.dataList = res.data.data.records
    // })
  },
  mounted() {
    option.height = window.innerHeight - 350 ;
    //查询服务类别字典项
    getDeptTree().then(res => {
      this.findObject(this.option.column, "storageTypeId").dicData = res.data.data;
    });
  },
  methods: {
    //展开主页左边类型
    nodeClick(data) {
      this.treeDeptId = data.id;
      this.page.currentPage = 1;
      this.onLoad(this.page);
    },
    //删除列表后面的删除按钮触发触发(row, index, done)
    rowDel(row, index, done) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
            return deleteDetails(row.id);
          }).then(() => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            // 数据回调进行刷新
            done(row);
          });
    },
    //修改时的修改按钮点击触发
    rowUpdate(row, index, done, loading) {
      typeSave(row).then(() => {
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        // 数据回调进行刷新
        done(row);
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    //新增修改时保存触发
    rowSave(row, done, loading) {
      console.log(row)
      typeSave(row).then(res => {
        console.log(res)
        this.page.currentPage = 1;
        this.onLoad(this.page, {});
        done()
      }, error => {
        window.console.log(error);
        loading();
      })
    },
    //查询全部
    initData(){
      getDeptTree().then(res => {
        const column = this.findObject(this.option.column, "storageTypeId");
        column.dicData = res.data.data;
      });
    },
    //新增子项触发
    handleAdd(row) {
      this.parentId = row.id;
      // const column = this.findObject(this.option.column, "parentId");
      // column.value = row.id;
      // column.addDisabled = true;
      this.$refs.crud.rowAdd();
    },
    //新增子项和新增触发查询所有
    beforeOpen(done, type) {
      if (["add", "edit"].includes(type)) {
        this.option.column.forEach(e=>{
          if(e.prop=='storageTypeId'){
            this.$set(this.option.column,3,{...e,value:this.treeDeptId})
          }
        })
      }
      if (["edit", "view"].includes(type)) {
        detail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    //点击新增时触发
    beforeClose(done) {
      this.parentId = "";
      // const column = this.findObject(this.option.column, "parentId");
      // column.value = "";
      // column.addDisabled = false;
      done();
    },
    //点击搜索按钮触发
    searchChange(params, done) {
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done()
    },
    searchReset() {
      this.query = {};
      this.treeDeptId = '';
      this.onLoad(this.page);
    },
    selectionChange() {
      console.log('1')
    },
    currentChange() {
      console.log('1')
    },
    sizeChange() {
      console.log('1')
    },
    refreshChange() {
      console.log('1')
    },
    onLoad(page, params = {parentId:0}) {
      let queryParams = Object.assign({}, params, {size: page.pageSize, current: page.currentPage,parentId:0,storageTypeId:this.treeDeptId})
      customerList(queryParams).then(res => {
        this.dataList = res.data.data.records
        this.page.total = res.data.data.total
      })
    },
    //树桩列点击展开触发
    treeLoad(tree, treeNode, resolve) {
      const parentId = tree.id;
      customerList({parentId:parentId}).then(res => {
        resolve(res.data.data.records);
      });
    },
  }

}
// {
//   "label": "仓库",
//   "prop": "storageTypeId",
//   "dicData": [],
//   "type": "tree",
//   "overHidden": true,
//   "width":150,
//   "addDisabled": false,
//   "multiple": false,
//   "props": {
//   "label": "title"
// },
//   "rules": [{
//   "required": true,
//   "message": " ",
//   "trigger": "click"
// }],
//   "dicUrl": "/api/blade-system/tenant/select",
//   "hide": "!website.tenantMode",
//   "addDisplay": "website.tenantMode",
//   "editDisplay": "website.tenantMode",
//   "viewDisplay": "website.tenantMode"
// },
</script>

<style scoped>

</style>
