<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" ref="crud" :page.sync="page"
      :permission="permissionList" @search-change="searchChange" @search-reset="searchReset"
      @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">
      <template #menu="{ row, index }">
        <el-button type="text" icon="el-icon-view" size="small" @click="rowView(row, index)">详情
        </el-button>
        <el-button type="text" icon="el-icon-map-location" size="small"
          @click="rowMap(row, index)">地图定位
        </el-button>
      </template>
    </avue-crud>
    <!-- 地图定位 -->
    <el-dialog v-if="dialogVisible" :visible.sync="dialogVisible" modal="false" append-to-body fullscreen>
      <MapView :floorNo="floorNo" :sn="sn" :x="x" :y="y"></MapView>
    </el-dialog>
  </basic-container>
</template>

<script>
import { bizLogin, terminalList, selectPatientPage } from "@/api/wirelessintrusion/wirelessintrusion";
import { mapGetters } from "vuex";
import { dateFormat } from "../../../util/date";
import MapView from "../../map/index.vue";

export default {
  components: {
    MapView
  },
  data() {
    return {
      query: {},
      loading: true,
      page: {
        size: 10,
        current: 1,
        total: 0
      },
      option: {
        height: 'auto',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        selection: false,
        viewBtn: true,
        delBtn: false,
        dialogClickModal: false,
        menuWidth: 180,
        column: [
          {
            label: "sn编号",
            prop: "sn",
          },
          {
            label: "姓名",
            prop: "patientName",
            search: true,
          },
          {
            label: "性别",
            prop: "sex",
            type: "select",
            dicData: [
              { value: "1", label: "男" }, { value: '0', label: "女" }
            ],
          },
          {
            label: "科室",
            prop: "departmentName",
          },
          {
            label: "状态",
            type: "select",
            dicData: [
              { value: 'alarm', label: "报警" }, { value: 'offLine', label: "离线" }, { value: 'onLine', label: "在线" }
            ],
            prop: "status",
            search: true,
          },
          {
            label: "年龄",
            prop: "age",
          },
          {
            label: "手机号",
            prop: "phone",
            span: 24,
          },
        ]
      },
      data: [],
      submitLoading: false,
      // buildingId: 202861, 
      buildingId: 206885, 
      //地图定位查看
      dialogVisible: false,
      sn: '',
      x: 0,
      y: 0,
      floorNo: 1
    };
  },
  computed: {
    ...mapGetters(["userInfo", "permission"]),
    permissionList() {
      return {
        addBtn: false,
        viewBtn: false,
        delBtn: true,
        editBtn: false
      };
    },
  },
  methods: {
    //查看详情
    rowView(row, index) {
      this.$refs.crud.rowView(row, index);
    },
    //地图定位查看
    rowMap(row, index) {
      let token = window.sessionStorage.getItem('bizToken');
      if (token == 'undefined' || !token) {
        bizLogin({ appKey: 'Arf7bd4f26', appSecret: 'kb207044c8' }).then(res => {
          if(res.status == 200){
            if(res.data.code == 0){
              window.sessionStorage.setItem('bizToken', res.data.data.token);
              terminalList(res.data.data.token, { page: 1, size: 1, buildingIds: this.buildingId, sn: row.sn }).then(res2 => {
                if (res2.data.code == 401) {
                  window.sessionStorage.removeItem('bizToken');
                  this.rowMap(row, index)
                }
                else if (res2.data.code == 0) {
                  const data = res2.data.data.content;
                  if (data.length > 0 && data[0].x && data[0].y) {
                    this.sn = row.sn;
                    this.x = data[0].x;
                    this.y = data[0].y;
                    this.floorNo = data[0].floorNo;
                    console.log(this.sn, this.x, this.y)
                    this.dialogVisible = true;
                  }else{
                    this.$message.warning("未查询到该人员定位信息");
                  }
                }else{
                  this.$message.error('物联网平台访问超时！')
                }
              });
            }else{
              this.$message.error('物联网平台访问超时！')
            }
          }else{
            this.$message.error('物联网平台访问超时！')
          }
          
        })
      }
      else {
        terminalList(token, { page: 1, size: 1, buildingIds: this.buildingId, sn: row.sn }).then(res2 => {
          if (res2.data.code == 401) {
            window.sessionStorage.removeItem('bizToken');
            this.rowMap(row, index);
          }
          else if (res2.data.code == 0) {
            const data = res2.data.data.content;
            if (data.length > 0 && data[0].x && data[0].y) {
              this.sn = row.sn;
              this.x = data[0].x;
              this.y = data[0].y;
              this.floorNo = data[0].floorNo;
              console.log(this.sn, this.x, this.y)
              this.dialogVisible = true;
            }else{
              this.$message.warning("未查询到该人员定位信息");
            }
          }else{
            this.$message.error('物联网平台访问超时！')
          }
        });
      }
      // if(row.x && row.y) {
      //   this.sn = row.sn;
      //   this.x = row.x;
      //   this.y = row.y;
      //   this.floorNo = row.floorNo;
      //   console.log(this.sn,this.x,this.y)
      //   this.dialogVisible = true;
      // }
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.current = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(current) {
      this.page.current = current;
    },
    sizeChange(size) {
      this.page.size = size;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      let token = window.sessionStorage.getItem('bizToken');
      if (token == 'undefined' || !token) {
        bizLogin({ appKey: 'Arf7bd4f26', appSecret: 'kb207044c8' }).then(res => {
          if(res.status == 200){
            if(res.data.code == 0){
              window.sessionStorage.setItem('bizToken', res.data.data.token);
              this.loading = true;
              selectPatientPage(res.data.data.token, Object.assign(params, this.query, { page: page.currentPage, size: page.pageSize, buildingIds: this.buildingId })).then(res2 => {
                if (res2.data.code == 401) {
                  window.sessionStorage.removeItem('bizToken');
                  this.onLoad(this.page);
                }
                else if (res2.data.code == 0) {
                  this.loading = false;
                  const data = res2.data.data;
                  this.page.total = data.total;
                  this.data = data.records;
                }else{
                  this.loading = false;
                  this.$message.error('物联网平台访问超时！')
                }
              }, err => {
                this.loading = false;
                this.$message.error('物联网平台访问超时！')
              });
            }else{
              this.$message.error('物联网平台访问超时！')
            }
          }else{
            this.$message.error('物联网平台访问超时！')
          }
          
        })
      }
      else {
        this.loading = true;
        selectPatientPage(token, Object.assign(params, this.query, { page: page.currentPage, size: page.pageSize, buildingIds: this.buildingId })).then(res2 => {
          // this.loading = true;
          if (res2.data.code == 401) {
            window.sessionStorage.removeItem('bizToken');
            this.onLoad(this.page);
          }
          else if (res2.data.code == 0) {
            this.loading = false
            const data = res2.data.data;
            this.page.total = data.total;
            this.data = data.records;
          }else{
            this.loading = false;
            this.$message.error('物联网平台访问超时！')
          }
        }, err => {
          this.$message.error('物联网平台访问超时！')
          this.loading = false;
        });
      }
    },
  }
};
</script>

<style lang="scss" scoped>
.el-table {
  width: 100% !important;
  border: 1px solid #D6D8DF;
}

.el-table th.el-table__cell {
  background: #F7F8FA;
}
</style>
