<template>
  <!-- 公司管理 -->
  <div class="home">
    <actionBar :operateList="operateList" :formList="formList" :ruleForm="searchRuleForm" @openModel="openModel"
      @searchProtocol="searchProtocol"></actionBar>
    <div class="card_content">
      <!-- 表单 -->
      <treeTable :treeData="treeData" :treeDataList="treeDataList" @buttonData="buttonData"></treeTable>
      <!-- 分页 -->
      <div v-if="Total">
        <pagination :total="Total" :currentPage="Pagination.PageIndex" @changeSize="changeSize"
          @changeCurrent="changeCurrent">
        </pagination>
      </div>
    </div>
    <el-dialog :title="staffTitle" :visible.sync="staffDialogVisible" width="600px" :close-on-click-modal="false"
      @close="closeDialog">
      <forms ref="childRules" :formNewList="formRuleList" :ruleForm="ruleForm" labelWidth="150px"></forms>
      <span slot="footer" class="dialog-footer">
        <el-button plain @click="staffDialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="confirmLoading" @click="handleAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getCompany,
    getCompanyDetails,
    addCompany,
    putCompany,
    delCompany
  } from '@/api/company'
  import actionBar from '@/components/actionBar'
  import treeTable from '@/components/treeTable'
  import pagination from '@/components/pagination'
  import forms from '@/components/forms'
  import {
    formRules,
    employee
  } from "./company.js";
  export default {
    name: 'UserManagement',
    components: {
      actionBar,
      treeTable,
      pagination,
      forms,
    },
    data() {
      return {
        operateList: [{
          type: 'add',
          title: '添加公司',
          icon: 'el-icon-plus',
        }],
        formList: [{
          type: 'input',
          label: '名称',
          field: 'name',
          placeholder: '名称',
        }],
        searchRuleForm: {
          name: '',
        },
        searchValue: {},
        Pagination: {
          PageIndex: 1,
          PageSize: 10,
        },
        Total: 0,
        operationType: '',
        tableList: employee(),
        tableData: [],
        staffTitle: '添加',
        staffDialogVisible: false,
        formRuleList: [],
        ruleForm: {
          name: '',
          companyAddress: '',
          coldKey: '',
          isIceReleaseCold: false,
          isCoolerReleaseCold: false,
          isOutStorage: false,
          remark: '',
        },
        confirmLoading: false,

        treeData: employee(),
        treeDataList: [],
        parentId: null,
      }
    },
    mounted() {
      const dataList = formRules();
      this.formRuleList = dataList;
      this.getList()
    },
    methods: {
      // 搜索
      searchProtocol(value) {
        this.searchValue = value
        this.getList()
      },
      // 获取公司列表
      getList() {
        var params = {
          page: this.Pagination.PageIndex,
          pageSize: this.Pagination.PageSize,
          ...this.searchValue
        }
        getCompany(params).then(res => {
          if (res.code == 200) {
            this.treeDataList = res.data
            this.Total = res.data.count
          }
        })
      },
      // 弹窗表单添加
      handleAdd() {
        let flag = this.$refs['childRules'].validateForm();
        if (flag) {
          if (this.operationType == 'add' || this.operationType == 'subAdd') {
            this.confirmLoading = true
            var params = {
              ...this.ruleForm
            }
            if (this.operationType == 'subAdd') {
              params.parentId = this.parentId
            }
            addCompany(params).then(res => {
              if (res.code == 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
                this.getList()
              }
              this.staffDialogVisible = false
              this.confirmLoading = false
            }).catch(() => {
              this.confirmLoading = false
            })
          } else if (this.operationType == 'edit') {
            this.confirmLoading = true
            var params = {
              id: this.ruleForm.id,
              name: this.ruleForm.name,
              remark: this.ruleForm.remark,
              coldKey: this.ruleForm.coldKey,
              companyAddress: this.ruleForm.companyAddress,
              isIceReleaseCold: this.ruleForm.isIceReleaseCold,
              isCoolerReleaseCold: this.ruleForm.isCoolerReleaseCold,
              isOutStorage: this.ruleForm.isOutStorage,
            }
            putCompany(params).then(res => {
              if (res.code == 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
                this.getList()
              }
              this.staffDialogVisible = false
              this.confirmLoading = false
            }).catch(() => {
              this.confirmLoading = false
            })
          }
        } else {
          this.$message.error('表单信息不完整，请继续填写完整');
        }
      },
      buttonData(row, type) {
        this.operationType = type
        if (type == 'edit') {
          this.staffTitle = '编辑'
          this.staffDialogVisible = true
          setTimeout(() => {
            this.$nextTick(() => {
              this.ruleForm = JSON.parse(JSON.stringify(row))
            })
          })
        } else if (type == 'del') {
          this.deleteUser(row.id)
        } else if (type == 'subAdd') {
          this.staffTitle = '添加子级'
          this.staffDialogVisible = true
          this.parentId = row.id
        }
      },
      openModel(type) {
        this.staffTitle = '添加'
        this.staffDialogVisible = true
        this.operationType = type
      },
      // 删除公司
      deleteUser(id) {
        this.$confirm('此操作将永久删除该公司, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delCompany({
            id: id,
          }).then(res => {
            if (res.code == 200) {
              this.$message({
                message: '操作成功',
                type: 'success'
              });
              this.getList()
            }
          })
        }).catch(() => {});
      },
      changeSize(val) {
        this.Pagination.PageSize = val
        this.getList()
      },
      changeCurrent(val) {
        this.Pagination.PageIndex = val
        this.getList()
      },
      // 清空表单
      closeDialog() {
        this.ruleForm = {}
        this.$refs.childRules.resetCheck();
      }
    }
  }
</script>
