<template>
  <div class="container">
    <!-- 查询配置 -->
    <div >
      <div ref="lxHeader">
        <LxHeader icon="md-apps" text="部门管理" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
          <div slot="content"></div>
          <slot>
            <div>
              <Button type="primary" @click="load" style="margin-left: 10px">查询</Button>
              <Button type="primary" @click="edit()" style="margin-left: 10px">新增部门</Button>
            </div>
          </slot>
        </LxHeader>
      </div>
      <div class="table-tree">
        <el-table :data="tableData" :height="tableHeight" class="v-table" style="width: 100%;margin-bottom: 20px;"
          row-key="id" border default-expand-all :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
          <el-table-column type="index" align="center">
          </el-table-column>
          <el-table-column prop="name" label="部门名称" sortable>
          </el-table-column>
          <el-table-column  prop="manger" label="部门负责人" sortable  width="180">
            <template slot-scope="scope">
              <el-tag type="primary" disable-transitions>{{scope.row.manager?scope.row.manager.name:"无"}}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="leader" label="部门分管人" sortable width="180">
            <template slot-scope="scope">
              <el-tag type="success" disable-transitions>{{scope.row.leader?scope.row.leader.name:"无"}}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="sortnumber" align="center" label="排序" sortable width="80">
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="300">
            <template slot-scope="scope">
              <Button type="primary" @click="addchildren(scope.row)" size="small" style="margin-left: 10px;"
                ghost>下级部门</Button>
              <Button type="error" @click="del(scope.row)" size="small" style="margin-left: 10px;" ghost>删除</Button>
              <Button type="primary" @click="edit(scope.row)" size="small" style="margin-left: 10px;" ghost>编辑</Button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <el-dialog title="部门编辑" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" :rules="rules" ref="form" label-position="right" :label-width="formLabelWidth">
        <el-form-item label="上级部门">
          <el-input v-model="form.pname" disabled autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-autocomplete class="inline-input" v-model="form.manager" :fetch-suggestions="querySearch"
            placeholder="请输入部门负责人" :trigger-on-focus="false" @select="handleSelectManager"></el-autocomplete>
        </el-form-item>
        <el-form-item label="部门分管人" prop="leader">
          <el-autocomplete class="inline-input" v-model="form.leader" :fetch-suggestions="querySearch"
            placeholder="请输入部门分管人" :trigger-on-focus="false" @select="handleSelectLeader"></el-autocomplete>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="form.sortnumber" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('form')">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
  import LxHeader from "@/components/LxHeader/index.vue";
  import {
    listdept,
    save,
    del
  } from "../../api/system/department.js";
  import {
    listuser
  } from "../../api/system/user.js";

  export default {
    components: {
      LxHeader
    },
    created() {
      this.initLoad();
      this.load();
    },
    mounted() {},
    data() {
      return {
        dialogFormVisible: false,
        formLabelWidth: "120px",
        form: {
          name: "",
          id: "",
          pid: "0",
          manager_id: "",
          leader_id: "",
          manager: "",
          leader: "",
          sortnumber: 0,
          icon: "",
          pname: "上级部门"
        },
        rules: {
          name: [{
            required: true,
            message: '请输入部门名称',
            trigger: 'blur'
          }]
        },
        tableHeight: 0,
        //查询条件字段
        searchFields: {
          KeyWord: ""
        },
        tableData: []
      }
    },
    methods: {
      querySearch(queryString, cb) {
        listuser().then(response => {
          var data = response.data;
          for (var m of data) {
            m.value = m.name;
          }
          cb(data)
        }).catch(error => {
          //reject(error)
        })
      },
      handleSelectManager(item) {
        this.form.manager_id = item.id;
      },
      handleSelectLeader(item) {
        this.form.leader_id = item.id;
      },
      initLoad() {
        var that = this;
        var clientHeight = document.documentElement.clientHeight
        var lxHeader_height = 96.5; //查询 头部
        var paginationHeight = 37; //分页的高度
        var topHeight = 50; //页面 头部
        let tableHeight = clientHeight - lxHeader_height - topHeight - paginationHeight - 20;
        that.tableHeight = tableHeight;
      },
      load() {
        var that = this;
        listdept().then(response => {
          that.tableData = response;
        }).catch(error => {
          //reject(error)
        })

      },
      edit(obj) {
        this.form = this.$options.data().form
        if (obj) {
          var result = Object.assign(this.form, obj);
          if (obj.leader)
            result.leader = obj.leader.name
          if (obj.manager)
            result.manager = obj.manager.name
          this.form = result;
        }
        this.dialogFormVisible = true;
      },
      addchildren(obj) {
        this.form = this.$options.data().form
        if (obj) {
          this.form.pname = obj.name;
          this.form.pid = obj.id;
          this.dialogFormVisible = true;
        }
      },
      submitForm(formName) {
        var that = this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            save(that.form).then(response => {
              console.log(response)
              this.$Message.success('操作成功');
              that.dialogFormVisible = false;
              that.load();
            }).catch(error => {
              reject(error)
            })
          } else {
            this.$Message.error('数据校验失败');
            return false;
          }
        });
      },
      resetForm(formName) {
        var that = this;
        this.$refs[formName].resetFields();
        that.dialogFormVisible = false;
      },
      del(obj) {
        var that = this;
        if (obj) {
          this.$Modal.confirm({
            title: '确认要删除数据？',
            onOk: () => {
              del({
                id: obj.id
              }).then(response => {
                this.$Message.success('操作成功');
                that.load();
              }).catch(error => {
                console.log(error)
                reject(error)
              })
            },
            onCancel: () => {
              //this.$Message.info('Clicked cancel');
            }
          });
        }
      },
    }
  };
</script>
