<template>
  <div class="body">
    <!-- 头部区域 -->

    <div class="head">
      <div class="head-img">
        <img src="../../../public/images/img-1.png">
      </div>
      <div class="head-info">用户与组绑定</div>
    </div>

    <!-- 搜索区域 -->
    <div class="form-head">
      <span>
        <el-input
          v-model="listQuery.key"
          size="mini"
          placeholder="请输入姓名,手机号或卡号"
          clearable
        />
      </span>

      <span style="margin-left: 15px">学院：</span>
      <el-select
        v-model="listQuery.college"
        clearable
        size="mini"
        placeholder="请选择学院"
      >
        <el-option
          v-for="(item, index) in collegeList"
          :key="index"
          :label="item.name"
          :value="item.name"
        />
      </el-select>

      <span style="margin-left: 15px">专业：</span>
      <el-select
        v-model="listQuery.professor"
        clearable
        size="mini"
        placeholder="请选择专业"
      >
        <el-option
          v-for="(item, index) in professorList"
          :key="index"
          :label="item.name"
          :value="item.name"
        />
      </el-select>

      <span style="margin-left: 15px">年级：</span>
      <el-select
        v-model="listQuery.nj"
        clearable
        size="mini"
        placeholder="请选择年级"
      >
        <el-option
          v-for="(item, index) in njList"
          :key="index"
          :label="item.name"
          :value="item.name"
        />
      </el-select>

      <span style="margin-left: 15px">班级：</span>
      <el-select
        v-model="listQuery.className"
        clearable
        size="mini"
        placeholder="请选择班级"
      >
        <el-option
          v-for="(item, index) in classNameList"
          :key="index"
          :label="item.name"
          :value="item.name"
        />
      </el-select>

      <el-button
        style="margin-left: 20px"
        type="primary"
        size="mini"
        class="button"
        @click="handleFilter"
      >
        查询
      </el-button>

      <!-- <el-button
        style="margin-left: 20px"
        type="primary"
        size="mini"
        class="button"
      >
        导出
      </el-button> -->
    </div>

    <!-- 表格区域 -->
    <div class="form">
      <el-table :data="list">
        <el-table-column label="姓名" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>

        <el-table-column label="手机号" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.mobile }}</span>
          </template>
        </el-table-column>

        <el-table-column label="卡号" align="center" show-overflow-tooltip>
          <template slot-scope="{ row }">
            <span>{{ row.cardNumber }}</span>
          </template>
        </el-table-column>

        <el-table-column label="学院" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.college }}</span>
          </template>
        </el-table-column>

        <el-table-column label="专业" align="center" show-overflow-tooltip>
          <template slot-scope="{ row }">
            <span>{{ row.professor }}</span>
          </template>
        </el-table-column>

        <el-table-column label="年级" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.nj }}</span>
          </template>
        </el-table-column>

        <el-table-column label="班级" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.classname }}</span>
          </template>
        </el-table-column>

        <el-table-column label="当前分组" align="center">
          <template slot-scope="{ row }">
            <span style="color: #1890ff">{{ row.group }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="300px" align="center">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              size="small"
              @click="checkGroup(row)"
            >查看当前分组</el-button>
            <el-button
              type="text"
              size="small"
              @click="bindGroup(row)"
            >绑定分组</el-button>
            <el-button
              type="text"
              size="small"
              @click="unbindGroup(row)"
            >解绑分组</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 底部分页区 -->

      <div class="block">
        <template>
          <pagination
            :total="total"
            :page.sync="listQuery.page"
            :limit.sync="listQuery.limit"
            @pagination="getUserList"
          />
        </template>
      </div>
    </div>

    <!-- 查看分组弹窗 -->
    <el-dialog
      title="查看当前分组"
      :visible.sync="dialogCheckGroups"
      width="30%"
      center
    >
      <el-form>
        <el-form-item label="已绑定分组：" class="switch-button-item">
          <span>
            <el-tag
              v-for="item in groupWithDeviceList"
              :key="item.idGroup"
              class="groupclass"
            >
              {{ item.groupName }}
            </el-tag>
          </span>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 绑定分组弹窗 -->

    <el-dialog
      title="绑定分组"
      :visible.sync="dialogBindGroup"
      width="30%"
      center
    >
      <el-form ref="dataForm" :model="temp" :rules="rules">
        <el-form-item
          style="white-space: pre"
          label="选择分组："
          class="switch-button-item"
          prop="groupsId"
        >
          <el-col :span="12">
            <el-select
              v-model="temp.groupsId"
              filterable
              multiple
              value-key="id"
              placeholder="请选择"
            >
              <el-option
                v-for="item in groupList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-col>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button type="primary" @click="updateData()">确 定</el-button>
        <el-button @click="dialogBindGroup = false">取 消</el-button>
      </span>
    </el-dialog>

    <!-- 解绑分组弹窗 -->
    <el-dialog
      title="解绑分组"
      :visible.sync="dialogUnbindGroup"
      width="30%"
      center
    >
      <el-table :data="groupWithDeviceList">
        <el-table-column label="分组" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.groupName }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="340px" align="center">
          <template slot-scope="{ row, $index }">
            <el-button
              type="text"
              size="small"
              @click="deleteGroup(row, $index)"
            >解绑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import {
  fetchGroupList,
  searchList,
  bindGroups,
  deleteGroups,
  checkGroups,
  getList
} from '@/api/userWithGroup'
import { getChooseList } from '@/api/usermanagement'
export default {
  components: {
    Pagination
  },
  data() {
    return {
      collegeList: [],
      njList: [],
      professorList: [],
      classNameList: [],
      list: [],
      groupList: [],
      groupWithDeviceList: [],
      temp: {
        userName: '',
        cardNumber: '',
        groupsId: []
      },
      total: 0, // 数据的总数//
      listQuery: {
        page: 1, // 当前在第几页//
        limit: 8, // 一页几条//
        key: '', // 关键字 姓名 手机号 卡号
        college: '', // 学院
        nj: '', // 年级
        professor: '', // 专业
        className: '' // 班级
      },
      dialogCheckGroups: false,
      dialogBindGroup: false,
      dialogUnbindGroup: false,
      rules: {
        groupsId: [
          {
            required: true,
            message: '请选择分组',
            trigger: 'change'
          }
        ]
      }
    }
  },
  created() {
    this.getUserList()
    this.getgroupList()
    // 获取学院
    this.getCollege(1)
    // 获取专业
    this.getProfessor(3)
    // 获取年级
    this.getNj(2)
    // 获取班级
    this.getClassName(4)
  },
  methods: {
    getUserList() {
      getList(this.listQuery).then((response) => {
        console.log(response)

        this.list = response.data
        this.total = response.total
      })
    },
    getgroupList() {
      fetchGroupList({ page: this.listQuery.page, limit: '999' }).then(
        (response) => {
          this.groupList = response.data
        }
      )
    },
    getCollege(name) {
      getChooseList({ name }).then((response) => {
        // console.log(response);
        this.collegeList = response.data
      })
    },
    getProfessor(name) {
      getChooseList({ name }).then((response) => {
        // console.log(response);
        this.professorList = response.data
      })
    },
    getNj(name) {
      getChooseList({ name }).then((response) => {
        // console.log(response);
        this.njList = response.data
      })
    },
    getClassName(name) {
      getChooseList({ name }).then((response) => {
        // console.log(response);
        this.classNameList = response.data
      })
    },
    checkGroup(row) {
      this.temp = Object.assign({}, row)
      checkGroups({ userId: row.id }).then((response) => {
        this.groupWithDeviceList = response.data
      })
      this.dialogCheckGroups = true
    },
    bindGroup(row) {
      this.temp = Object.assign({}, row)
      this.dialogBindGroup = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          bindGroups({
            id: tempData.id,
            groups: tempData.groupsId
          }).then(() => {
            this.dialogBindGroup = false
            this.$notify({
              title: '绑定分组成功',
              type: 'success',
              duration: 1500
            })
          })
        }
      })
    },
    unbindGroup(row) {
      this.temp = Object.assign({}, row)
      checkGroups({ userId: row.id }).then((response) => {
        this.groupWithDeviceList = response.data
      })
      this.dialogUnbindGroup = true
    },
    deleteGroup(row, index) {
      console.log(row, index)
      if (confirm('确定解绑吗？')) {
        deleteGroups({
          id: this.temp.id,
          groups: [row.idGroup]
        }).then(() => {
          this.$notify({
            title: '解绑成功',
            type: 'success',
            duration: 1500
          })
          this.groupWithDeviceList.splice(index, 1)
        })
      }
    },
    handleChangeFilter() {
      if (!this.listQuery.name && !this.listQuery.cardNumber) {
        this.getUserList()
      } else {
        searchList(this.listQuery).then((response) => {
          this.list = response.data
          this.total = response.total
        })
      }
    },
    handleFilter() {
      this.list = []
      this.total = 0
      this.listQuery.page = 1
      this.getUserList()
    }
  }
}
</script>

<style lang="scss" scoped>
.body {
  width: 1551px;
  margin: 29px 29px 0 31px;
  .head {
    display: flex;
    width: 1380px;
    height: 55px;
    margin-left: 40px;
    border-bottom: 1px solid #cccccc;

    .head-img {
      width: 30px;
      height: 30px;

      img {
        width: 30px;
      }
    }

    .head-info {
      height: 22px;
      margin-top: 5px;
      margin-left: 16px;
      font-weight: bold;
    }
    .input {
      width: 240px;
      margin-left: 21px;
    }

    .input-serch {
      margin-left: 32px;
    }
  }

  .form-head {
    display: flex;
    align-items: center;
    margin: 20px 40px;
    width: 1504px;
    font-size: 9px;
  }

  .form {
    margin-left: 40px;
    width: 1380px;

    .block {
      float: right;
    }
  }
  .switch-button-item {
    margin-left: 120px;
    .groupclass {
      float: left;
      margin-right: 10px;
      margin-bottom: 10px;
    }
  }
}
</style>
