<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="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="getList"
          />
        </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="groups"
        >
          <el-col :span="12">
            <el-select
              v-model="temp.groups"
              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 {
  fetchList,
  fetchGroupList,
  bindGroups,
  searchList,
  deleteGroups,
  checkGroups,
} from "@/api/devicewithgroup";
export default {
  components: {
    Pagination,
  },
  data() {
    return {
      list: [],
      groupList: [],
      groupWithDeviceList: [],
      temp: {
        serialNumber: "",
        name: "",
        groups: [],
        groupIds: [],
      },
      total: 0, //数据的总数//
      listQuery: {
        page: 1, //当前在第几页//
        limit: 8, //一页几条//
        name: "",
        serialNumber: "",
      },
      dialogBindGroup: false,
      dialogUnbindGroup: false,
      dialogCheckGroups: false,
      rules: {
        groups: [
          {
            required: true,
            message: "请选择分组",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getgroupList();
  },
  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;
        });
      }
    },
    getgroupList() {
      fetchGroupList({ page: this.listQuery.page, limit: "999" }).then(
        (response) => {
          this.groupList = response.data;
        }
      );
    },
    checkGroup(row) {
      this.temp = Object.assign({}, row);
      checkGroups({ deviceId: 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,
            serialNumber: tempData.serialNumber,
            groups: tempData.groups,
          }).then(() => {
            this.dialogBindGroup = false;
            this.$notify({
              title: "绑定分组成功",
              type: "success",
              duration: 1500,
            });
          });
        }
      });
    },
    unbindGroup(row) {
      this.temp = Object.assign({}, row);
      checkGroups({ deviceId: row.id }).then((response) => {
        this.groupWithDeviceList = response.data;
      });
      this.dialogUnbindGroup = true;
    },
    deleteGroup(row, index) {
      if (confirm("确定解绑吗？")) {
        deleteGroups({
          id: this.temp.id,
          serialNumber: this.temp.serialNumber,
          groups: [row.idGroup],
        }).then(() => {
          this.$notify({
            title: "解绑成功",
            type: "success",
            duration: 1500,
          });
          this.groupWithDeviceList.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>
