<template>
  <div class="app-container">
    <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" />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input v-model="queryParams.phonenumber" placeholder="请输入手机号码" clearable style="width: 240px" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getListss">搜索</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="openSelectUser">添加用户
        </el-button>
        <el-button type="danger" plain icon="el-icon-delete" size="mini" @click="handleDelete()">批量删除用户</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-close" size="mini" @click="handleClose">关闭
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :data="userList" @selection-change="handleSelectionChanges">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户名称" prop="userName" :show-overflow-tooltip="true" />
      <el-table-column label="手机" prop="phonenumber" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </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" icon="el-icon-circle-close" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增弹窗 -->
    <el-dialog title="选择用户" :visible.sync="visible" width="800px" top="5vh" append-to-body>
      <el-form :model="queryParam" ref="queryForm" size="small" :inline="true">
        <el-form-item label="用户名称" prop="userName">
          <el-input v-model="queryParam.userName" placeholder="请输入用户名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input v-model="queryParam.phonenumber" placeholder="请输入手机号码" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getListcy">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuerys">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row>
        <el-table @row-click="clickRow" ref="table" :data="userListcy" @selection-change="handleSelectionChange"
          height="260px">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column label="用户名称" prop="userName" :show-overflow-tooltip="true" />
          <el-table-column label="用户昵称" prop="nickName" :show-overflow-tooltip="true" />
          <el-table-column label="邮箱" prop="email" :show-overflow-tooltip="true" />
          <el-table-column label="手机" prop="phonenumber" :show-overflow-tooltip="true" />
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="totalcy > 0" :total="total" :page.sync="queryParam.pageNum" :limit.sync="queryParam.pageSize"
          @pagination="getListcy" />
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSelectUser">确 定</el-button>
        <el-button @click="visible = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  getUser,// 根据分类id获取人员信息
  addUser,//分组新增人员;
  deleteUser,//删除人员,

} from "@/api/smartSchool/informationPush/personnelGrouping";
// import selectUser from "./selectUser";
import { unallocatedUserList } from "@/api/system/role";
export default {
  name: "AuthUser",
  dicts: ['sys_normal_disable'],

  data() {
    return {

      visible: false,
      // 遮罩层
      loading: true,
      // 选中用户组
      userIds: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalcy: 0,
      // 用户表格数据
      userList: [],
      userListcy: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleId: undefined,
        userName: undefined,
        phonenumber: undefined
      },
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        roleId: undefined,
        userName: undefined,
        phonenumber: undefined
      }
    };
  },
  watch: {
    $route: {
      handler(to, from) {
        console.log('object', from);
        if (from.path == '/schoolManage/schoolNotice/personnelGrouping') {
          this.getList()
        }

      }
    }
    // '$route.query.id': function () {
    //   console.log("djuedhuiwhdeui");
    //   this.getList()
    // }
  },
  created() {
    this.getList();
    this.getListcy();
  },

  mounted() {
  },
  methods: {
    /** 根据页面跳转所传id查询用户列表 */
    getList() {
      this.loading = true;
      this.notificationId = this.$route.query.id;
      console.log('this..notificationId', this.notificationId);
      getUser(this.notificationId).then(response => {
        this.userList = response.data;
        console.log('this.userList', this.userList);
        this.loading = false;
      });
    },
    //添加用户 
    openSelectUser() {
      this.userIds = []; // 清空数组内容
      this.visible = true;
    },
    // 查询可分组表数据
    getListcy() {
      unallocatedUserList(this.queryParam).then(res => {
        this.userListcy = res.rows;
        this.totalcy = res.total;
        console.log('this.userListcy', this.userListcy);
        this.total = res.total;
      });
    },
    getListss() {
      unallocatedUserList(this.queryParams).then(res => {
        this.userList = res.rows;
        this.total = res.total;
        console.log('this.userListcy', this.userListcy);
        this.total = res.total;
      });
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log('selection', selection);
      this.userIds = selection.map(item => item.userId);
    },
    // 多选框选中数据
    handleSelectionChanges(selection) {
      this.userIds = selection.map(item => item.userId)
      this.multiple = !selection.length
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.getList();

    },
    resetQuerys() {
      this.resetForm("queryForm");
      this.getListcy();

    },
    /** 选择成员操作 */
    handleSelectUser() {
      const notificationId = this.$route.query.id;
      const userIds = this.userIds;
      if (userIds == "") {
        this.$modal.msgError("请选择要分配的用户");
        return;
      }
      addUser({ notificationId: notificationId, userId: userIds }).then(res => {
        this.$modal.msgSuccess(res.msg);
        if (res.code === 200) {
          this.visible = false;
          this.getList();
          this.$emit("ok");
        }
      });
    },


    // 返回按钮
    handleClose() {
      const obj = { path: "/schoolManage/schoolNotice/personnelGrouping" };
      this.$tab.closeOpenPage(obj);
    },

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

    /** 删除操作 */
    handleDelete(row) {
      console.log(row, 'row');
      const notificationId = this.$route.query.id;
      console.log(notificationId);
      let userIds;
      if (row) {
        // 单个删除
        userIds = [row.userId];
        console.log(userIds);
      } else {
        // 批量删除
        userIds = this.userIds;
        console.log(userIds);
      }
      this.$modal.confirm('确认要取消该用户的分组吗？').then(() => {
        return deleteUser({ notificationId: notificationId, userId: userIds });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch((error) => {
      });
    }




  }
};
</script>
