<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryParams" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="分组名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="分组名称" clearable style="width: 240px" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索</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">新增</el-button>
        <el-button type="danger" plain icon="el-icon-delete" size="mini" @click="handleDelete()">批量删除</el-button>
      </el-col>

    </el-row>

    <el-table :data="roleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分组编号" prop="id" />
      <el-table-column label="分组名称" prop="name" />
      <el-table-column label="显示顺序" prop="sort" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-user" @click="handleAuthUser(scope.row.id)">
            分配用户
          </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" />

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="分组名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="显示顺序" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0" style="width: 100%;" />
        </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 {
  getUserList,//获取分组集合
  getUser,// 根据分类id获取人员信息
  addNotification,//新增数据
  updateNotification,//修改分组信息
  deleteNotification,//删除分组
  getXg
} from "@/api/smartSchool/informationPush/personnelGrouping";
export default {
  name: "index",
  data() {
    return {
      notificationId: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      selectedRoleId: "",
      // 是否显示弹出层
      open: false,
      form: {
        id: "",
        name: "",
        sort: "",
        remark: "",
      },

      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: "",

      },
      // 表单参数
      form: {},
      idList: [],
      // 表单校验
      rules: {

      }
    };
  },
  created() {
    this.getList();

  },
  methods: {

    /** 查询角色列表 */
    getList() {
      this.loading = true;
      getUserList(this.queryParams).then(response => {
        this.roleList = response.rows;
        console.log('this.roleList', this.roleList);
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        sort: undefined,


      };
      this.resetForm("form");
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("form");
      this.queryParams.name = "",
        this.getList();
    },

    // 更多操作触发
    // handleCommand(command, row) {
    //   switch (command) {
    //     case "handleDataScope":
    //       this.handleDataScope(row);
    //       break;
    //     case "handleAuthUser":
    //       this.handleAuthUser(row);
    //       break;
    //     default:
    //       break;
    //   }
    // },



    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加分组";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      const id = row.id || this.ids
      getXg(id).then(response => {
        this.form = response.data;
        console.log(this.form, this.form);
        this.open = true;

      });
      this.title = "修改分组";


    },

    //根据id获取分组数据
    groupList() {
      this.roleList.forEach(role => {
        const notificationId = role.id;
        getUser(notificationId).then(response => {
          this.idList = response.data;
          console.log('this.idList', this.idList);
        });
      });
    },

    /** 分配用户跳转页面操作 */
    handleAuthUser(id) {
      console.log(id, 'id');
      this.$router.push({
        path: "/personnelGrouping/authUser",
        query: {
          id
        }
      });
    },


    // handleAuthUser: function (row) {
    //   this.selectedRoleId = row.id;
    //   console.log(this.selectedRoleId, 'roleId');
    //   this.$router.push({
    //     path: "/personnelGrouping/authUser",
    //     query: {
    //       id: this.selectedRoleId // 使用保存的 roleId 值
    //     }
    //   })
    // },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            // this.form.menuIds = this.getMenuAllCheckedKeys();
            updateNotification(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // this.form.menuIds = this.getMenuAllCheckedKeys();
            addNotification(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      let idList = ''
      if (row) {
        let idArr = []
        idArr.push(row.id)
        idList = idArr.toString()
        console.log(idList);
      } else {
        idList = this.ids.toString()
        console.log(idList);
      }
      this.$modal.confirm('是否确认删除该数据项？').then(() => {
        return deleteNotification({ idList });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },





  }
};
</script>