<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>
              <Input style="width: 200px; margin-right: 10px" v-model.number="searchFields.keyword" placeholder="关键字搜索" />
              <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" class="v-table" :height="tableHeight" border style="width: 100%">
          <el-table-column type="index" align="center">
          </el-table-column>
          <el-table-column prop="name" label="用户" width="180">
          </el-table-column>
          <el-table-column prop="deptName" label="部门">
            <template slot-scope="scope">
              {{scope.row.department?scope.row.department.name:"无"}}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="300">
            <template slot-scope="scope">
              <Button ghost size="small" @click="edit(scope.row)" type="primary" style="margin-left: 10px;">编辑</Button>
              <Button ghost size="small" @click="del(scope.row)" type="error" style="margin-left: 10px;">删除</Button>
            </template>
          </el-table-column>
        </el-table>

        <div class="pagination">
          <el-pagination @current-change="handleCurrentChange" :current-page="paginations.page"
            :page-size="paginations.page_size" background layout="prev, pager, next" :total="paginations.total">
          </el-pagination>
        </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="姓名" prop="name">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="部门" prop="department_id">
            <treeselect v-model="form.department_id" :normalizer="normalizer" :multiple="false" clearable
              style="width: 300px;" :show-count="true" placeholder="选择图书类目" :options="deptList" />
          </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>
  </div>
</template>

<script>
  import LxHeader from "@/components/LxHeader/index.vue";
  import {
    save,
    show,
    list as listuser,
    del
  } from "../../api/staff/staffIndex.js";
  import {
    list as listDept
  } from "../../api/staff/deptIndex.js";
  // import the component
  import Treeselect from '@riophae/vue-treeselect'
  // import the styles
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  export default { // register the component
    components: {
      Treeselect,
      LxHeader
    },
    created() {
      var that = this;
      this.initLoad();
      that.loadDept();
      that.load();
    },
    mounted() {},
    data() {

      return {
        normalizer(node) {
          return {
            id: node.id,
            label: node.name,
            children: node.children,
          }
        },
        deptList: [],
        checkAll: false,
        isIndeterminate: true,
        dialogFormVisible: false,
        formLabelWidth: "120px",
        userNameStatus: true,
        form: {
          name: "",
          department_id: "",
        },
        rules: {
          name: [{
            required: true,
            message: '请输入姓名',
            trigger: 'blur'
          }],
          department_id: [{
            required: true,
            message: '请选择部门ID',
            trigger: 'change'
          }]
        },
        paginations: {
          page: 1,
          page_size: 15,
          total: 0
        },
        tableHeight: 0,
        //查询条件字段
        searchFields: {
          keyword: ""
        },
        tableData: []
      }
    },
    methods: {
      handleCurrentChange(page) {
        this.paginations.page = page;
        this.load();
      },
      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;
      },
      loadDept() {
        listDept({
          is_tree: 1
        }).then(response => {
          this.deptList = response;
        }).catch(error => {
          //reject(error)
        })
      },
      load() {
        var that = this;
        listuser({
          page: that.paginations.page,
          ...that.searchFields
        }).then(response => {
          var data = response.data;
          that.tableData = data;
          this.paginations.total = response.total;
        }).catch(error => {
          console.log(error)
          //reject(error)
        })
      },
      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');
            }
          });
        }
      },
      edit(obj) {
        this.form = this.$options.data().form
        if (obj) {
          var result = Object.assign(this.form, obj);
          this.form = result;
        }
        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.load();
              that.dialogFormVisible = false;
            }).catch(error => {
              //reject(error)
            })
          } else {
            this.$Message.error('数据校验失败');
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        var that = this;
        this.$refs[formName].resetFields();
        that.dialogFormVisible = false;
      }
    }
  };
</script>

<style>
</style>
