<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" ref="crud"
      v-model="form" :before-open="beforeOpen"
      @search-change="searchChange" @search-reset="searchReset"
      @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">
    </avue-crud>
  </basic-container>
</template>

<script>
import { bizLogin, bizDeviceList, bizDeviceStatus, bizNodeType } from "@/api/wirelessintrusion/wirelessintrusion";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 30,
        dialogWidth: 950,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: true,
        editBtn: false,
        delBtn: false,
        addBtn: false,
        menu: true,
        menuWidth: 100,
        dialogClickModal: false,
        column: [
          {
            label: "设备名称",
            prop: "name",
            span: 12,
            search: true,
            formatter: (val, value, label) => {
              return val.name + '/' + val.remark
            },
            labelWidth: 120,
            viewDisplay: false,
          },
          {
            label: "设备名称",
            prop: "name",
            span: 12,
            search: true,
            labelWidth: 120,
            hide: true
          },
          {
            label: "备注名称",
            prop: "remark",
            span: 12,
            hide: true,
            labelWidth: 120
          },
          {
            label: "设备所属产品",
            prop: "productName",
            span: 12,
            labelWidth: 120
          },
          {
            label: "节点类型",
            type: "select",
            dicData: [],
            props: {
              label: "dictLabel",
              value: "dictValue"
            },
            search: true,
            prop: "node",
            labelWidth: 120,
            viewDisplay: false
          },
          {
            label: "节点类型",
            prop: "nodeLabel",
            labelWidth: 120,
            hide: true,
          },
          {
            label: "网关信息",
            prop: "gateway",
            span: 12,
            formatter: (val, value, label) => {
              return val.gateway || '-'
            },
            labelWidth: 120
          },
          {
            label: "设备状态",
            type: "select",
            dicData: [],
            props: {
              label: "dictLabel",
              value: "dictValue"
            },
            search: true,
            prop: "status",
            labelWidth: 120,
            viewDisplay: false,
          }, 
          {
            label: "节点类型",
            prop: "statusLabel",
            span: 12,
            labelWidth: 120,
            hide: true,
          },
          {
            label: "创建时间",
            prop: "createTime",
            span: 12,
            labelWidth: 120,
            hide: true,
          }, {
            label: "最后上线时间",
            span: 12,
            prop: "lastOnline",
            labelWidth: 120
          },
        ]
      },
      data: [],
      nodeType: [],//节点类型
    };
  },
  mounted() {
    this.loadDict();
  },
  methods: {
    //字典加载
    loadDict() {
      let token = window.sessionStorage.getItem('bizToken');
      if (token == 'undefined' || !token) {
        bizLogin({ appKey: 'Arf7bd4f26', appSecret: 'kb207044c8' }).then(res => {
          window.sessionStorage.setItem('bizToken', res.data.data.token);
          bizDeviceStatus(res.data.data.token).then(res => {
            const column = this.findObject(this.option.column, "status");
            column.dicData = res.data.data;
          })
          bizNodeType(res.data.data.token).then(res => {
            const column = this.findObject(this.option.column, "node");
            column.dicData = res.data.data;
          })
        })
      } else {
        bizDeviceStatus(token).then(res => {
          if(res.data.code == 401){
            window.sessionStorage.removeItem('bizToken');
            this.loadDict();
          }
          else if(res.data.code == 200){
            const column = this.findObject(this.option.column, "status");
            column.dicData = res.data.data;
          }
        })
        bizNodeType(token).then(res => {
          const column = this.findObject(this.option.column, "node");
          column.dicData = res.data.data;
        })
      }
    },
    //节点类型转换
    renderLabel(node,col) {
      const column = this.findObject(this.option.column, col);
      let idx = column.dicData.findIndex(item => item.dictValue == node);
      return idx > -1 ? column.dicData[idx].dictLabel : '';
    },
    // rowSave(row, done, loading) {
    //   add(row).then(() => {
    //     this.onLoad(this.page);
    //     this.$message({
    //       type: "success",
    //       message: "操作成功!"
    //     });
    //     done();
    //   }, error => {
    //     window.console.log(error);
    //     loading();
    //   });
    // },
    // rowUpdate(row, index, done, loading) {
    //   update(row).then(() => {
    //     this.onLoad(this.page);
    //     this.$message({
    //       type: "success",
    //       message: "操作成功!"
    //     });
    //     done();
    //   }, error => {
    //     window.console.log(error);
    //     loading();
    //   });
    // },
    // rowDel(row) {
    //   this.$confirm("确定将选择数据删除?", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning"
    //   })
    //     .then(() => {
    //       return remove(row.id);
    //     })
    //     .then(() => {
    //       this.onLoad(this.page);
    //       this.$message({
    //         type: "success",
    //         message: "操作成功!"
    //       });
    //     });
    // },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    // handleDelete() {
    //   if (this.selectionList.length === 0) {
    //     this.$message.warning("请选择至少一条数据");
    //     return;
    //   }
    //   this.$confirm("确定将选择数据删除?", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning"
    //   })
    //     .then(() => {
    //       return remove(this.ids);
    //     })
    //     .then(() => {
    //       this.onLoad(this.page);
    //       this.$message({
    //         type: "success",
    //         message: "操作成功!"
    //       });
    //       this.$refs.crud.toggleSelection();
    //     });
    // },
    beforeOpen(done, type) {
        console.log(this.form)
      if (["edit", "view"].includes(type)) {
        // let token = window.sessionStorage.getItem('bizToken');
        // if (token == 'undefined') {
        //   bizDeviceDetail(this.form.id).then(res => {
        //     this.form = res.data.data;
        //   });
        // }
      }
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      let values = {
        ...params,
        ...this.query,
        pid: 100
      };
      let token = window.sessionStorage.getItem('bizToken');
      if (token == 'undefined' || !token) {
        bizLogin({ appKey: 'Arf7bd4f26', appSecret: 'kb207044c8' }).then(res => {
          window.sessionStorage.setItem('bizToken', res.data.data.token);
          this.loading = true;
          bizDeviceList(res.data.data.token, { ...values, pageSize: page.pageSize, pageNum: page.currentPage }).then(res2 => {
            const data = res2.data;
            this.page.total = data.total;
            data.rows.map(item => {
              item.node = item.bizProduct.node;
              item.nodeLabel = this.renderLabel(item.node,'node');
              item.statusLabel = this.renderLabel(item.status,'status')
              item.productName = item.bizProduct.name;
            })
            this.data = data.records;
            this.loading = false;
          });
        })
      } else {
        this.loading = true;
        bizDeviceList(token, { ...values, pageSize: page.pageSize, pageNum: page.currentPage, pid: 100 }).then(res2 => {
          this.loading = false;
          this.selectionClear();
          if(res2.data.code == 401){
            console.log(res2.data.code)
            window.sessionStorage.removeItem('bizToken');
            this.onLoad();
          }
          else if(res2.data.code == 200){
            const data = res2.data;
            this.page.total = data.total;
            data.rows.map(item => {
              item.node = item.bizProduct.node;
              item.nodeLabel = this.renderLabel(item.node,'node');
              item.statusLabel = this.renderLabel(item.status,'status')
              item.productName = item.bizProduct.name;
            })
            this.data = data.rows;
          }
        }, err => {
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  }
};
</script>

<style></style>
