<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-row class="last">
        <p><span>搜索条件</span> <i></i></p>
        <el-col :span="24">
          <el-col :span="8">
            <el-form-item label="人员姓名" prop="userId">
              <el-select   @change="nsles"  filterable style="width: 280px;" v-model="queryParams.userId" placeholder="请选择人员" clearable>
                <el-option
                  v-for="dict in userList"
                  :key="dict.userId"
                  :label="dict.userName"
                  :value="dict.userId"
                  ref="mySelect"
                />
              </el-select>
              <!-- <el-input v-model="form.userName" placeholder="请输入人员姓名" /> -->
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-col>

      </el-row>

    </el-form>
   <div class="navde">
     <el-row :gutter="10" class="mb8" style="background-color: #fafafa ;padding-top: 0;">
       <el-col :span="24">
         <div style="display: flex; align-items: center;justify-content: space-between;">
           <p style="border-left: 5px solid #3F73FD;padding-left: 5px;"><span>补卡/请假配置</span> </p>
           <div>
             <el-button
                 type="primary"
                 plain
                 size="mini"
                 @click="handleAdd"
                 v-hasPermi="['system:configuration:add']"
               >新增</el-button>
               <el-button
                 type="success"
                 plain
                 size="mini"
                 :disabled="single"
                 @click="handleUpdate"
                 v-hasPermi="['system:configuration:edit']"
               >修改</el-button>
         <el-button
           type="danger"
           plain
           size="mini"
           :disabled="multiple"
           @click="handleDelete"
           v-hasPermi="['system:configuration:remove']"
         >删除</el-button>
           </div>
         </div>
       </el-col>


       <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
     </el-row>

     <el-table :height="tableMaxHeight" v-loading="loading" :data="configurationList" @selection-change="handleSelectionChange">
       <el-table-column type="selection" width="55" align="center" />
       <el-table-column label="人员姓名" align="center" prop="userName" />
       <el-table-column label="部门名称" align="center" prop="deptName" />
       <el-table-column label="配置类型 " align="center" prop="type">
         <template slot-scope="scope">
           <dict-tag :options="dict.type.sys_configuration_type" :value="scope.row.type"/>
         </template>
       </el-table-column>
       <el-table-column label="角色" align="center" prop="role">
         <template slot-scope="scope">
           <dict-tag :options="dict.type.sys_configuration_role" :value="scope.row.role"/>
         </template>
       </el-table-column>
       <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
         <template slot-scope="scope">
           <el-button
             size="mini"
             type="text"
             @click="handleUpdate(scope.row)"
             v-hasPermi="['system:configuration:edit']"
           >修改</el-button>
           <el-button
             size="mini"
             type="text"
             @click="handleDelete(scope.row)"
             v-hasPermi="['system:configuration:remove']"
           >删除</el-button>
         </template>
       </el-table-column>
     </el-table>

     <pagination
       v-show="total>0"
       :total="total"
       :page.sync="queryParams.pageNum"
       :limit.sync="queryParams.pageSize"
       @pagination="getList"
     />
   </div>




    <!-- 添加或修改补卡、请假流程配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>

    </el-dialog>
    <el-drawer
      title="信息填写"
      :visible.sync="drawer"
      :modal="false"
      class="buukage"
      >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row style="padding: 10px;">
          <el-col :span="24">
            <el-form-item label="人员姓名" prop="userName">
              <el-select   @change="nsle"  filterable style="width:100%;" v-model="form.userId" placeholder="请选择人员" clearable>
                <el-option
                  v-for="dict in userList"
                  :key="dict.userId"
                  :label="dict.nickName"
                  :value="dict.userId"
                  ref="mySelect"
                />
              </el-select>
              <!-- <el-input v-model="form.userName" placeholder="请输入人员姓名" /> -->
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="部门名称" prop="deptId">
              <treeselect v-model="form.deptId" @select="selectDepart" :options="deptOptions" :show-count="true" placeholder="请选择部门名称" />
              <!-- <el-input v-model="form.deptName" placeholder="请输入部门名称" /> -->
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="配置类型" prop="type">
                      <el-select style="width: 100%;" v-model="form.type" placeholder="请选择配置类型 1：补卡，2：请假">
                        <el-option
                          v-for="dict in dict.type.sys_configuration_type"
                          :key="dict.value"
                          :label="dict.label"
            :value="dict.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="角色" prop="role">
                      <el-select style="width: 100%;" v-model="form.role" placeholder="请选择角色 1：审批员 ，2：抄送员">
                        <el-option
                          v-for="dict in dict.type.sys_configuration_role"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>
          </el-col>
        </el-row>

        <!-- <el-form-item label="人员id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入人员id" />
        </el-form-item> -->

        <!-- <el-form-item label="审批管理部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入审批管理部门id" />
        </el-form-item> -->


     <div  class="dialog-footer" style="text-align: center;">
       <el-button type="primary" @click="submitForm">确 定</el-button>
       <el-button @click="cancel">取 消</el-button>
     </div>
      </el-form>

    </el-drawer>
  </div>
</template>

<script>
import { listConfiguration, getConfiguration, delConfiguration, addConfiguration, updateConfiguration } from "@/api/system/configuration";
import { listUser, getUser, delUser, addUser, updateUser, resetUserPwd, changeUserStatus } from "@/api/system/user";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Configuration",
  dicts: ['sys_configuration_role', 'sys_configuration_type'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      drawer:false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 补卡、请假流程配置表格数据
      configurationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        deptName: null,
        type: null,
        role: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "部门不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        role: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
      },
      userList:[],
      // 部门树选项
      deptOptions: undefined,
      tableMaxHeight:'200'
    };
  },
  created() {
    this.getList();
    this.getLists()
    this.getTreeselect()
    window.onresize = () => {
          this.changeTableMaxHeight()
        }
        this.changeTableMaxHeight()
  },
  mounted() {
  	window.onresize = () => {
  	          this.changeTableMaxHeight()
  	        }
  	        this.changeTableMaxHeight()
  },
  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 查询用户列表 */
    getLists() {
      this.loading = true;
      listUser().then(response => {
          this.userList = response.rows;
          this.loading = false;
        }
      );
    },
    /** 查询补卡、请假流程配置列表 */
    getList() {
      this.loading = true;
      listConfiguration(this.queryParams).then(response => {
        this.configurationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      // this.open = false;
      this.drawer = false
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        userName: null,
        deptId: null,
        deptName: null,
        type: null,
        role: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.open = true;
      this.drawer = true
      this.title = "添加补卡、请假流程配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getConfiguration(id).then(response => {
        this.form = response.data;
        // this.open = true;
        this.drawer = true
        this.title = "修改补卡、请假流程配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateConfiguration(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              // this.open = false;
              this.drawer = false
              this.getList();
            });
          } else {
            addConfiguration(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              // this.open = false;
              this.drawer = false
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除补卡、请假流程配置编号为"' + ids + '"的数据项？').then(function() {
        return delConfiguration(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('system/configuration/export', {
        ...this.queryParams
      }, `configuration_${new Date().getTime()}.xlsx`)
    },

    nsle(val){
      console.log(val)
      this.form.userName = this.userList.find(item => item.userId == val).nickName
      console.log(this.form.userName)
    },
    nsles(val){
      console.log(val)
      this.queryParams.userName = this.userList.find(item => item.userId == val).nickName
      console.log(this.queryParams.userName)
    },
    selectDepart(val){
      console.log(val)
      this.form.deptName =val.label
    },
    selectDeparts(val){
      console.log(val)
      this.queryParams.deptName =val.label
    },
    // 获取屏幕高度
    showFilterForm () {
          this.filterActive = !this.filterActive
          this.changeTableMaxHeight()
        },
        changeTableMaxHeight () {
          let height = document.body.offsetHeight // 网页可视区域高度
          // if (this.filterActive) {
          //   this.tableMaxHeight = height - 320
          // } else {
            this.tableMaxHeight = height - 250
          // }
          console.log(height)
        }
  }
};
</script>
<style lang="scss">
  .buukage{
    .el-drawer__open .el-drawer.rtl{
      width: 20% !important;
    }
  }
</style>
