<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" :page="page" :permission="permissionList"
               :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate" @row-save="rowSave" @row-del="rowDel"
               @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange" @current-change="currentChange"
               @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">

    </avue-crud>
  </basic-container>
</template>

<script>
  import{
    getDetailByDeviceId
  } from "../../api/device/iotdevice.js";
  import {
    getList,
    getDetail,
    add,
    update,
    remove
  } from "../../api/device/iotalarm.js";
  import {
    mapGetters
  } from "vuex";


  export default {
    props:{
      imei: "",
    },
    data() {

      return {
        deviceDetail:{},
        alarmDetail: {},
        alarmDetailVisible: false,
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          menuWidth: 200,
          height: 'auto',
          calcHeight: 30,
          tip: false,
          menu: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          addBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "设备sn号",
              prop: "deviceId",
              // search: true,
              width: 130,
              rules: [{
                required: false,
                message: "请输入告警设备sn号",
                trigger: "blur"
              }]
            },
            {
              label: "设备名称",
              prop: "deviceName",
              // search: true,
              width: 200,
              overHidden: true,
              rules: [{
                required: false,
                message: "请输入设备名称",
                trigger: "blur"
              }]
            },
            {
              label: "设备类型",
              prop: "deviceType",
              type: "select",
              // search: true,
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=device_type",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              dataType: "number",
              rules: [{
                required: false,
                message: "请输入设备类型",
                trigger: "blur"
              }]
            },

            // {
            //   label: "告警次数",
            //   prop: "alarmTimes",
            //   rules: [{
            //     required: false,
            //     message: "请输入告警次数",
            //     trigger: "blur"
            //   }]
            // },
            // {
            //   label: "告警等级",
            //   prop: "level",
            //   type: "select",
            //   dicUrl: "/api/blade-system/dict-biz/dictionary?code=alarm_level",
            //   props: {
            //     label: "dictValue",
            //     value: "dictKey"
            //   },
            //   dataType: "nubmer",
            //   rules: [{
            //     required: false,
            //     message: "请输入告警等级 1:严重告警,2:警告告警,3:提示告警",
            //     trigger: "blur"
            //   }]
            // },
            // {
            //   label: "数据",
            //   prop: "name",
            //   width: 100,
            //   rules: [{
            //     required: false,
            //     message: "请输入告警原因",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "监测值",
              prop: "monitorValue",
              width: 100,
              rules: [{
                required: false,
                message: "请输入监测值",
                trigger: "blur"
              }]
            },

            {
              label: "告警时间",
              prop: "recordTime",
              rules: [{
                required: false,
                message: "请输入告警时间",
                trigger: "blur"
              }]
            },
            {
              label: "最新告警时间",
              prop: "updateTime",
              hide: true,
              rules: [{
                required: false,
                message: "请输入最新告警时间",
                trigger: "blur"
              }]
            },


            // {
            //   label: "告警状态",
            //   prop: "status",
            //   type: "select",
            //   dicUrl: "/api/blade-system/dict-biz/dictionary?code=alarm_status",
            //   props: {
            //     label: "dictValue",
            //     value: "dictKey"
            //   },
            //   dataType: "number",
            //   rules: [{
            //     required: false,
            //     message: "请输入告警状态，0 实时告警，1-历史告警",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "告警类型",
              prop: "alarmType",
              type: "select",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=alarm_type",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              dataType: "number",
              rules: [{
                required: false,
                message: "请输入告警类型, 0-业务告警；1-设备告警",
                trigger: "blur"
              }]
            },

          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.alarmlist_add, false),
          viewBtn: this.vaildData(this.permission.alarmlist_view, false),
          delBtn: this.vaildData(this.permission.alarmlist_delete, false),
          editBtn: this.vaildData(this.permission.alarmlist_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },

    methods: {
      init(){
        // this.query.deviceId = this.imei;
        this.page.currentPage =1;
        this.page.pageSize =10;
        this.onLoad(this.page)
      },
      ViewAlarmDetail(row) {
        getDetailByDeviceId(row.deviceId).then(res=>{
          this.deviceDetail = res.data.data;
          console.log(res)
        });
        this.alarmDetail = row;
        this.alarmDetailVisible = true;
      },

      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      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) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      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();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        this.query.deviceId = this.imei;
        console.log("imei:"+this.imei)
        // this.query.deviceName = "阅海万家"
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
