<template>
  <div class="app-container" id="userInfo">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入姓名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          maxlength="11"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <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-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:user:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" >
      <el-table-column label="序号" align="center" width="80" type="index">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="变动前部门" align="center" prop="oldOrgName" :show-overflow-tooltip="true" />
      <el-table-column label="变动前考勤规则" align="center" prop="oldAttName" :show-overflow-tooltip="true"/>
      <el-table-column label="变动后部门" align="center" prop="afterOrgName" :show-overflow-tooltip="true" />
      <el-table-column label="变动后考勤规则" align="center" prop="afterAttName" :show-overflow-tooltip="true" />
      <el-table-column label="变动人" align="center" prop="createName" :show-overflow-tooltip="true" />
      <el-table-column label="变动时间" align="center" prop="createTime" :show-overflow-tooltip="true" />
    </el-table>

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

    <!-- 添加参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body @close="handleClose">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="userIds">
              <el-select v-model="form.userIds" filterable placeholder="请选择人员姓名" clearable style="width: 100%;"  @change="handlePersonChange">
                <el-option
                  v-for="item in personOptions"
                  :key="item.userIds"
                  :label="item.userName"
                  :value="item.userIds"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" >
              <el-input v-model="form.phoneNumber" disabled />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="变动前部门" >
              <el-input v-model="form.oldOrgName" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="变动前考勤规则" >
              <el-input v-model="form.oldAttName" disabled />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="变动后部门" prop="orgIds">
              <treeselect v-model="form.orgIds" :multiple="false" :flat="true" :options="deptOptions" :normalizer="normalizer" placeholder="选择所属部门"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="变动后考勤规则" prop="afterAttId">
              <el-select v-model="form.afterAttId" filterable  placeholder="请选择考勤规则" clearable style="width: 100%;">
                <el-option
                  v-for="item in attOptions"
                  :key="item.afterAttId"
                  :label="item.afterAttName"
                  :value="item.afterAttId"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>



      </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 {
  listChange,
  addAtt,
  listDeptTree,
  getPersonSelect,
  getAttGroup,
  getOldData
} from "@/api/setting/change";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Change",
  components: { Treeselect },
  data() {
    return {
      isEditing: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      opens: false,
      openView: false,
      deptOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phone: undefined,
      },
      personOptions:[],
      attOptions:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userIds: [
          { required: true, message: "人员姓名不能为空", trigger: "blur" }
        ],
        orgIds: [
          { required: true, message: "所属部门不能为空", trigger: "blur" }
        ],
        afterAttId:[
          { required: true, message: "考勤组不能为空", trigger: "blur" }
        ]
      },
    };
  },
  created() {
    this.getList();
    this.getDeptList();
    this.getPersonSelect();
    this.getAttGroup();
  },
  methods: {
    /** 人员选择下拉 */
    getPersonSelect() {
      getPersonSelect().then(response => {
        this.personOptions=response.data
      });
    },

    /**
     * 考勤班组
     */
    getAttGroup() {
      getAttGroup().then(response => {
        this.attOptions=response.data
      });
    },

    /**
     * 部门
     */
    getDeptList(){
      listDeptTree().then(response => {
        this.deptOptions = this.handleTree(response.data, "id");
      });
    },

    /**
     * 去查询之前的数据
     * @param selectedValues
     */
    handlePersonChange(selectedValues) {
      console.log('选择的人员ID:', selectedValues);
      this.form.userId = selectedValues;
      if (selectedValues) {
        getOldData(this.form).then(response => {
          this.form.phoneNumber = response.data.phoneNumber || '';
          this.form.oldOrgId = response.data.oldOrgId || '';
          this.form.oldOrgName = response.data.oldOrgName || '';
          this.form.oldAttId = response.data.oldAttId || '';
          this.form.oldAttName = response.data.oldAttName || '';
          // alert(1);
        });
      }else {
        // 当取消选择时，清空之前的数据
        this.form.phoneNumber = '';
        this.form.oldOrgName = '';
        this.form.oldAttName = '';
      }

    },

    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.orgName,
        children: node.children
      };
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listChange(this.queryParams).then(response => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.opens = false;
      this.openView = false;
      this.isEditing = false;
      this.reset();
    },
    // 处理对话框关闭
    handleClose() {
      this.open = false;
      this.isEditing = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userIds: undefined,
        userId: undefined,
        phoneNumber: '',
        oldOrgName: '',
        oldAttName: '',
        orgIds: null,
        afterAttId: [],
        roleIds:[],
        oldAttId: null,
        oldOrgId: null
      };
      this.resetForm("form");
    },

    // 表单重置
    resets() {
      this.form = {
      };
      this.resetForm("forms");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 变动按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "变动人员";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.userId = this.form.userIds;
          this.form.afterOrgId = this.form.orgIds;
          console.log("变动方法",this.form);
          addAtt(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.isEditing = false;
            this.getList();
          });
        }
      });
    },
  }
};
</script>
