<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>姓名：</span>
      <span class="input">
        <el-input
          v-model="listQuery.name"
          placeholder="请输入姓名"
          clearable
          @clear="handleChangeFilter"
          @keyup.enter.native="handleFilter"
        >
        </el-input>
      </span>

      <span>卡号：</span>
      <span class="input">
        <el-input
          v-model="listQuery.cardNumber"
          placeholder="请输入卡号"
          clearable
          @clear="handleChangeFilter"
          @keyup.enter.native="handleFilter"
        >
        </el-input>
      </span>

      <el-button type="primary" size="mini" class="button" @click="handleFilter"
        >查询</el-button
      >
    </div>

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

        <el-table-column label="卡号" align="center" width="440px">
          <template slot-scope="{ row }">
            <span>{{ row.cardNumber }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="440px" 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
              class="groupclass"
              v-for="item in groupWithDeviceList"
              :key="item.idGroup"
            >
              {{ 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-option>
            </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 {
  fetchUserList,
  fetchGroupList,
  editGroup,
  searchList,
  fetchList,
  bindGroups,
  deleteGroups,
  checkGroups,
} from "@/api/userWithGroup";
export default {
  components: {
    Pagination,
  },
  data() {
    return {
      list: [],
      groupList: [],
      groupWithDeviceList: [],
      temp: {
        userName: "",
        cardNumber: "",
        groupsId: [],
      },
      total: 0, //数据的总数//
      listQuery: {
        page: 1, //当前在第几页//
        limit: 8, //一页几条//
        name: "",
        cardNumber: "",
      },
      dialogCheckGroups: false,
      dialogBindGroup: false,
      dialogUnbindGroup: false,
      rules: {
        groupsId: [
          {
            required: true,
            message: "请选择分组",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getUserList();
    this.getgroupList();
  },
  methods: {
    getUserList() {
      if (this.listQuery.name || this.listQuery.cardNumber) {
        searchList(this.listQuery).then((response) => {
          this.list = response.data;
          this.total = response.total;
        });
      } else {
        fetchUserList({
          page: this.listQuery.page,
          limit: this.listQuery.limit,
        }).then((response) => {
          this.list = response.data;
          this.total = response.total;
        });
      }
    },
    getgroupList() {
      fetchGroupList({ page: this.listQuery.page, limit: "999" }).then(
        (response) => {
          this.groupList = 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() {
      if (!this.listQuery.name && !this.listQuery.cardNumber) {
        alert("请输入搜索条件");
      } else {
        let temp = Object.assign({}, this.listQuery);
        temp.page = 1;
        searchList(temp).then((response) => {
          this.list = response.data;
          this.total = response.total;
        });
        this.listQuery.page = 1;
      }
    },
  },
};
</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 {
    margin: 0 40px;
    width: 1504px;
    line-height: 118px;
    font-size: 9px;

    .input {
      display: inline-block;
      margin-right: 30px;
      width: 210px;
    }

    .button {
      margin-left: 41px;
    }
  }

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