<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.serialNumber"
          placeholder="请输入序列号"
          clearable
          @clear="handleChangeFilter"
          @keyup.enter.native="handleFilter"
        >
        </el-input>
      </span>

      <span>设备名称：</span>
      <span class="input">
        <el-input
          v-model="listQuery.name"
          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.serialNumber }}</span>
          </template>
        </el-table-column>

        <el-table-column label="名称" align="center" width="440px">
          <template slot-scope="{ row }">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="440px" align="center">
          <template slot-scope="{ row }">
            <el-button type="text" size="small" @click="checkPower(row)"
              >查看当前身份</el-button
            >
            <el-button type="text" size="small" @click="bindPower(row)"
              >添加身份</el-button
            >
            <el-button type="text" size="small" @click="unbindPower(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="getList"
          />
        </template>
      </div>
    </div>

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

    <!-- 添加身份弹窗 -->

    <el-dialog
      title="添加身份"
      :visible.sync="dialogBindPower"
      width="30%"
      center
    >
      <el-form ref="dataForm" :model="temp" :rules="rules">
        <el-form-item
          style="white-space: pre"
          label="选择身份："
          class="switch-button-item"
          prop="groups"
        >
          <el-col :span="12">
            <el-select
              v-model="temp.groups"
              filterable
              multiple
              placeholder="请选择"
            >
              <el-option
                v-for="item in groupList"
                :key="item.idIdentity"
                :label="item.identityName"
                :value="item.idIdentity"
              >
              </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="dialogBindPower = false">取 消</el-button>
      </span>
    </el-dialog>

    <!-- 删除身份弹窗 -->
    <el-dialog
      title="删除身份"
      :visible.sync="dialogUnbindPower"
      width="30%"
      center
    >
      <el-table :data="deviceWithPowerList">
        <el-table-column label="身份类型" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.identityName }}</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="deletePower(row, $index)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import {
  fetchList,
  bindPowers,
  searchList,
  deletePowers,
  checkPowers,
} from "@/api/deviceWithStatus";
export default {
  components: {
    Pagination,
  },
  data() {
    return {
      list: [],
      groupList: [
        { idIdentity: 0, identityName: "其它" },
        { idIdentity: 1, identityName: "学生" },
        { idIdentity: 4, identityName: "教职工" },
        { idIdentity: 7, identityName: "临时人员" },
      ],
      deviceWithPowerList: [],
      temp: {
        serialNumber: "",
        name: "",
        groups: [],
        identities: "",
      },
      total: 0, //数据的总数//
      listQuery: {
        page: 1, //当前在第几页//
        limit: 8, //一页几条//
        name: "",
        serialNumber: "",
      },
      dialogBindPower: false,
      dialogUnbindPower: false,
      dialogCheckPowers: false,
      rules: {
        groups: [
          {
            required: true,
            message: "请选择分组",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      if (this.listQuery.name || this.listQuery.serialNumber) {
        searchList(this.listQuery).then((response) => {
          this.list = response.data;
          this.total = response.total;
        });
      } else {
        fetchList({
          page: this.listQuery.page,
          limit: this.listQuery.limit,
        }).then((response) => {
          this.list = response.data;
          this.total = response.total;
        });
      }
    },
    checkPower(row) {
      this.temp = Object.assign({}, row);
      checkPowers({ deviceId: row.id }).then((response) => {
        this.deviceWithPowerList = response.data;
      });
      this.dialogCheckPowers = true;
    },
    bindPower(row) {
      this.temp = Object.assign({}, row);
      this.dialogBindPower = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    updateData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp);
          bindPowers({
            id: tempData.id,
            serialNumber: tempData.serialNumber,
            identities: tempData.groups,
          }).then(() => {
            this.dialogBindPower = false;
            this.$notify({
              title: "添加身份成功",
              type: "success",
              duration: 1500,
            });
          });
        }
      });
    },
    unbindPower(row) {
      this.temp = Object.assign({}, row);
      checkPowers({ deviceId: row.id }).then((response) => {
        this.deviceWithPowerList = response.data;
      });
      this.dialogUnbindPower = true;
    },
    deletePower(row, index) {
      if (confirm("确定删除身份吗？")) {
        deletePowers({
          id: this.temp.id,
          serialNumber: this.temp.serialNumber,
          identities: [row.idIdentity],
        }).then(() => {
          this.$notify({
            title: "删除身份成功",
            type: "success",
            duration: 1500,
          });
          this.deviceWithPowerList.splice(index, 1);
        });
      }
    },
    handleChangeFilter() {
      if (!this.listQuery.name && !this.listQuery.serialNumber) {
        this.getList();
      } else {
        searchList(this.listQuery).then((response) => {
          this.list = response.data;
          this.total = response.total;
        });
      }
    },
    handleFilter() {
      if (!this.listQuery.name && !this.listQuery.serialNumber) {
        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>
