<template>
  <div class="app-container">
    <addModal ref="addModel" :machine-list="snList" @success="modalSuccess" />
    <!--搜索区-->
    <el-select
      v-model="form.zoneId"
      filterable
      clearable
      placeholder="请选择区域"
      class="input"
      no-data-text="“选择区域”为空"
    >
      <el-option
        v-for="(item, index) in areaPreviewList"
        :key="index"
        :label="item.name"
        :value="item.id"
      />
    </el-select>
    <el-select
      v-model="form.sn"
      filterable
      clearable
      placeholder="请选择设备编号"
      class="margin-left input"
    >
      <el-option
        v-for="(item, index) in machineList"
        :key="index"
        :label="item.sn"
        :value="item.sn"
      />
    </el-select>
    <el-select
      v-model="form.sn"
      filterable
      clearable
      placeholder="请选择设备地点"
      class="margin-left input"
    >
      <el-option
        v-for="(item, index) in machineList"
        :key="index"
        :label="item.name"
        :value="item.sn"
      />
    </el-select>
    <el-button
      type="primary"
      icon="el-icon-search"
      class="margin-left"
      @click="query"
      >搜索</el-button
    >
    <!--主表格-->
    <div class="table-wrap">
      <div class="table-add" @click="handleCreate">
        <img src="@/icons/images/ic_plus.png" />
      </div>
      <div v-for="(item, index) in tableData" :key="index" class="table-item">
        <div class="table-item-head">
          <div class="flex">
            <div class="head-title">{{ item.name }}</div>
            <div class="tag">
              <el-tag v-if="item.spiPushMsg" type="success">{{
                item.spiPushMsg
              }}</el-tag>
              <!-- <el-tag v-if="item.zoneType" type="success">{{
                getAreaDescByType(item.zoneType)
              }}</el-tag> -->
            </div>
          </div>
          <div>
            <!-- 跳转到大屏 -->
            <el-button
              type="primary"
              size="small"
              round
              @click="goLargeScreeno(item)"
              ><i class="el-icon-s-platform"></i
            ></el-button>
            <el-button
              type="primary"
              size="small"
              round
              @click="handleUpdate(item)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              circle
              plain
              @click="handleAreaDelete(item)"
            />
          </div>
        </div>
        <div class="table-item-content">
          <div
            v-for="(itemS, indexS) in item.devices"
            :key="indexS"
            class="content-item"
          >
            <div class="content-item-id">{{ itemS.sn }}</div>
            <div class="content-item-name">{{ itemS.name }}</div>
            <div class="content-item-status">
              <el-tag v-if="itemS.online === 1" type="success">在线</el-tag>
              <el-tag v-else type="info">离线</el-tag>
              <el-button
                type="danger"
                icon="el-icon-close"
                size="mini"
                class="content-item-status-delete"
                circle
                @click="handleDeviceDelete(item.id, itemS)"
              />
            </div>
          </div>
        </div>
      </div>
    </div>
    <!--分页栏-->
    <pagination
      :total="pagination.total"
      :page.sync="pagination.current"
      :limit.sync="pagination.pageSize"
      :page-sizes="pagination.pageSizeOptions"
      :layout="pagination.layout"
      @pagination="handleSizeChange"
    />
  </div>
</template>

<script>
import {
  getAreaPageList,
  getAreaList,
  deleteArea,
  deleteDeviceOfArea,
  platformSnList,
} from "@/api/area";
import { getFaceDeviceList } from "@/api/old_api_pengwenbing";
import tableMixins from "@/mixins/tableMixins";
import Pagination from "@/components/Pagination";
import AddModal from "./modal/AddModal.vue";

export default {
  components: { Pagination, AddModal },
  mixins: [tableMixins],
  data() {
    return {
      form: {
        zoneId: null,
        adminId: null,
        sn: null,
      },
      areaPreviewList: [],
      // 设备列表_搜索
      machineList: [],
      showSearch: false,
      snList: [],
    };
  },
  methods: {
    initData() {
      this.form.adminId = this.$store.getters.adminId;
      this.getAreaList();
      this.getFaceDeviceList();
      this.getTableList();
    },
    getAreaDescByType(type) {
      if (!type) {
        return "";
      }
      const areaType = this.areaTypeList.find((item) => item.type === type);
      if (!areaType) {
        return "";
      }
      return areaType.desc || "";
    },
    // 返回所有的区域列表概览
    async getAreaList() {
      try {
        const result = await getAreaList(this.form);
        this.areaPreviewList = result.data;
      } catch (e) {
        console.log(e);
        this.areaPreviewList = [];
      }
    },
    // 获取设备列表-搜索
    getFaceDeviceList() {
      getFaceDeviceList().then((res) => {
        this.machineList = res.data;
      });
    },
    // 获取设备列表-添加/编辑
    getSnList(id) {
      if (id) {
        this.form.zoneId = id;
      }
      platformSnList(this.form).then((res) => {
        this.snList = res.data;
        this.form.zoneId = null;
      });
    },
    // 获取表格数据
    async getTableList() {
      this.tableLoading = true;
      try {
        const result = await getAreaPageList(this.getFilterParams());
        this.setDataList(result.data);
      } catch (e) {
        console.log(e);
      } finally {
        this.tableLoading = false;
      }
    },
    // 获取筛选信息
    query() {
      this.clearPageParams();
      this.getTableList();
    },
    // 增加筛选条件
    transformFilterForm() {
      return this.form;
    },
    modalSuccess(isEdit) {
      if (!isEdit) {
        this.clearPageParams();
      }
      this.getTableList();
    },
    handleCreate() {
      this.getSnList();
      this.$refs.addModel.open();
    },
    handleUpdate(row) {
      this.$refs.addModel.open(Object.assign({}, row));
      this.getSnList(row.id);
    },
    handleAreaDelete(row) {
      const that = this;
      this.$confirm("确定要删除此区域吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        showClose: false,
        type: "warning",
        beforeClose: async (action, instance, done) => {
          if (action === "confirm") {
            instance.confirmButtonLoading = true;
            instance.cancelButtonLoading = true;
            instance.confirmButtonText = "提交中...";
            try {
              await deleteArea(row.id);
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              that.getAreaList();
              that.getTableList();
            } catch (e) {
              // this.$message({
              //   type: 'error',
              //   message: '删除失败!'
              // })
            } finally {
              done();
              setTimeout(() => {
                instance.confirmButtonLoading = false;
                instance.cancelButtonLoading = false;
              }, 300);
            }
          } else {
            done();
          }
        },
      })
        .then(() => {})
        .catch(() => {});
    },
    handleDeviceDelete(areaId, row) {
      const that = this;
      this.$confirm("确定要解绑此设备吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        showClose: false,
        type: "warning",
        beforeClose: async (action, instance, done) => {
          if (action === "confirm") {
            instance.confirmButtonLoading = true;
            instance.cancelButtonLoading = true;
            instance.confirmButtonText = "提交中...";
            try {
              await deleteDeviceOfArea(areaId, row.sn);
              this.$message({
                type: "success",
                message: "解绑成功!",
              });
              that.getTableList();
            } catch (e) {
              // this.$message({
              //   type: 'error',
              //   message: '解绑失败!'
              // })
            } finally {
              done();
              setTimeout(() => {
                instance.confirmButtonLoading = false;
                instance.cancelButtonLoading = false;
              }, 300);
            }
          } else {
            done();
          }
        },
      })
        .then(() => {})
        .catch(() => {});
    },
    goLargeScreeno(item) {
      // 创建一个 base-64 编码的字符串
      let id = window.btoa(item.id);
      window.location.href =
        "https://face.ldxk.edu.cn:8901/ywjl-fyzd/dormitory/index.html?zoneDeviceRId=" +
        id;
    },
  },
};
</script>
<style lang="scss" scoped>
.table {
  margin-top: 15px;
}
.flex {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.margin-left {
  margin: 10px 0 0 10px;
}
.input {
  width: 200px;
}
.input1 {
  width: 160px;
}
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
  background-color: #f1f2f5;
}

::-webkit-scrollbar-thumb {
  background-color: #bfc6d4;
  border-radius: 10px;
}
::-webkit-scrollbar-track {
  background-color: transparent;
}
.table-wrap {
  display: flex;
  flex-wrap: wrap;
  & > div {
    margin-top: 28px;
    margin-left: 1%;
    min-width: 375px;
    width: 32%;
    height: 256px;
    flex: none;
    overflow: hidden;
    border: 1px solid #e3e9f8;
    border-radius: 8px;
  }
  & > div:nth-of-type(3n + 1) {
    margin-left: 0;
  }
  .table-add {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f8faff;
    cursor: pointer;
    & img {
      width: 80px;
      height: 80px;
    }
  }
  .table-item {
    display: flex;
    flex-direction: column;
    .table-item-head {
      width: 100%;
      height: 62px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 14px 0 20px;
      background-color: #e3ebff57;
      .head-title {
        font-size: 20px;
        color: #1b55ab;
      }
    }
    .tag {
      flex: left;
      text-align: center;
      margin-left: 10px;
    }
    .table-item-content {
      width: 100%;
      padding: 0 12px;
      height: calc(256px - 62px);
      overflow: auto;
      .content-item {
        margin-top: 12px;
        padding: 4px 6px;
        display: flex;
        align-items: center;
        font-size: 14xp;
        color: #606266;
        &:last-of-type {
          margin-bottom: 12px;
        }
        &:hover {
          background-color: #f5f8ff;
          border-radius: 5px;
          .content-item-status .content-item-status-delete {
            visibility: visible;
          }
        }
        .content-item-id {
          flex: none;
        }
        .content-item-name {
          flex: auto;
          margin-left: 20px;
        }
        .content-item-status {
          flex: none;
          display: flex;
          align-items: center;
          .content-item-status-delete {
            margin-left: 15px;
            visibility: hidden;
            padding: 3px !important;
          }
        }
      }
    }
  }
}
.el-icon-s-platform,
.el-icon-search {
  font-weight: 600;
}
</style>
