<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入部门名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="tree" default-expand-all highlight-current
            @node-click="handleNodeClick" />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
          label-width="68px">
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入员工姓名" clearable style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="班次" prop="schedulingId">
            <el-select v-model="queryParams.schedulingId" placeholder="班次" clearable style="width: 240px">
              <el-option v-for="dict in schedulList" :key="dict.id" :label="dict.shiftName"
                :value="dict.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="手机号" prop="phoneNumber">
            <el-input v-model="queryParams.phoneNumber" placeholder="请输入手机号码" clearable style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" size="mini" :disabled="multiple" @click="handleAdd"
              v-hasPermi="['system:user:create']">设置排班</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" type="index" width="50"align="center"/>
          <!-- <el-table-column label="员工Id" align="center" rop="employeeId"/> -->
          <el-table-column label="员工姓名" align="center" prop="name"/>
          <el-table-column label="班次信息" align="center" prop="shiftName"/>
          <el-table-column label="部门" align="center" key="deptName" prop="deptName"/>
          <el-table-column label="手机号" align="center" prop="phoneNumber"/>
          <el-table-column label="邮箱" align="center" prop="email"/>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </el-col>
    </el-row>

    <!-- 设置排班对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="排班选择" prop="schedulingId">
          <el-select v-model="form.schedulingId" placeholder="班次" clearable style="width: 100%;">
              <el-option v-for="dict in schedulList" :key="dict.id" :label="dict.shiftName"
                :value="dict.id" />
            </el-select>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSimpleSchedulings } from "@/api/personnel/attendance/scheduling/index";
import {listUser,updateUser} from "@/api/personnel/attendance/employee/index";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listSimpleDepts } from "@/api/system/dept";
export default {
  name: "SystemUser",
  components: { Treeselect },
  data() {
    return {
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: undefined,
      schedulList:[],
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "name"
      },
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: undefined,
        phoneNumber: undefined,
        schedulingId: undefined,
        deptId: undefined,
      },
      // 表单校验
      rules: {
        schedulingId: [
          { required: true, message: "请选择排班", trigger: "blur" }
        ],
      },
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  mounted() {
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      }
      );
    },
    /** 查询部门下拉树结构 + 岗位下拉 */
    getTreeselect() {
      listSimpleDepts().then(response => {
        // 处理 deptOptions 参数
        this.deptOptions = [];
        this.deptOptions.push(...this.handleTree(response.data, "id"));
      });
      listSimpleSchedulings().then(response => {
        // 处理 postOptions 参数
        this.schedulList = [];
        this.schedulList.push(...response.data);
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.files = [],
      this.form = {
        employeeIds: "",
        schedulingId: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.deptId = undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection);
      this.form.employeeIds = selection.map(item => item.employeeId)
      console.log(this.form.employeeIds);
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "设置排班";
    },
   
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateUser(this.form).then(response => {
              this.$modal.msgSuccess("设置成功");
              this.reset();
              this.open = false;
              this.getList();
            });
        }
      });
    },
  }
};
</script>
<style lang="scss" scoped>
.public-link-operate {
    border-bottom: 1px solid #e1e3e5;
    padding: 20px 0;
    .operate-title {
        color: #141e31;
        font-size: 14px;
        font-weight: 500;
        margin-bottom: 12px;
.link-tip {
    color: #838892;
    font-weight: 400;
    margin-left: 10px;
.link-tip-mark {
    font-weight: 600;
}
}
    }
}
.public-link-address {
    color: #141e31;
    font-weight: 500;
    padding: 20px 0;
.link-input {
    font-weight: 400;
    margin-top: 10px;
    width: 100%;
}
}
</style>