<!-- 监控配置 -->
<template>
  <div class="app-container monitorConfig">
    <div class="page-container monitorConfigPage" v-if="pageType === 1">
      <div class="page-form-title-box">
        <el-form :model="queryParams" class="form-box" ref="queryForm"
                 :inline="true" style="width:100%;">
          <el-form-item label="" prop="searchValue">
            <el-input
              maxLength="30"
              v-model="queryParams.searchValue"
              placeholder="请输入名称"
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="" prop="deviceType">
            <el-select v-model="queryParams.deviceType" placeholder="请选择设备分类" style="width: 200px">
              <el-option
                v-for="dict in typeOptionList"
                :key="dict.typeKey"
                :label="dict.typeName"
                :value="dict.typeKey"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="state">
            <el-select v-model="queryParams.state" placeholder="请选择状态" style="width: 200px">
              <el-option
                v-for="dict in optionList"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          <p class="page-add-common-style-button"
             style="float: right;"
             @click="tableButton(1)"
             v-hasPermiRouter="['iot:monitorConfig:add']"
          >新增</p>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="dataList">
          <el-table-column label="名称" prop="monitorName"  show-overflow-tooltip/>
          <el-table-column label="设备分类" prop="deviceType" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-for="(item,index) in typeOptionList" v-show="item.typeKey === scope.row.deviceType">{{item.typeName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="监控类型" prop="monitorType" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.monitorType == 1?'设备上线':(scope.row.monitorType == 2?'设备离线':'')}}</span>
            </template>
          </el-table-column>
          <el-table-column label="告警类型" prop="alarmType" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.alarmType == 1?'立即':(scope.row.alarmType == 2?'累计':'')}}</span>
            </template>
          </el-table-column>
          <el-table-column label="次数" prop="count" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.count?scope.row.count:'N/A'}}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="state" width="100" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.state?'启用':'禁用'}}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createTime" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime,"{y}-{m}-{d} {h}:{i}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" show-overflow-tooltip v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <!--<p class="table-button-p"-->
                   <!--@click="tableButton(2,scope.row)"-->
                   <!--v-hasPermiRouter="['demo:demo:detail']"-->
                <!--&gt;详情</p>-->
                <p class="table-button-p"
                   @click="tableButton(3,scope.row)"
                   v-hasPermiRouter="['iot:monitorConfig:edit']"
                >编辑</p>
                <p class="table-button-p"
                   @click="tableButton(4,scope.row)"
                   v-hasPermiRouter="['iot:monitorConfig:del']"
                >删除</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination :page-sizes="[20, 30, 40, 50]"
                    v-show="total>0"
                    :total="total"
                    :page.sync="queryParams.page"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
    <add-page :propsData="propsData" v-if="pageType === 2"></add-page>
  </div>
</template>
<script>
  import { iotMonitorConfigList,iotMonitorConfigDelete,iotTypeGetAllTypes } from "@/api/iotDevice/index";
  import addPage from "./addPage.vue";
  export default {
    name: 'index',
    components: {
     addPage
    },
    data () {
      return {
        tableButtonType:this.hasPermiDom(['iot:monitorConfig:edit','iot:monitorConfig:del',]),
        //页面状态
        pageType:1,
        //页面遮罩
        loading:false,
        //下拉列表数据
        typeOptionList:[],
        optionList:[{value:true,label:'启用'},{value:false,label:'停用'}],
        //查询条件
        queryParams:{
          page:1,
          pageSize:20,
          searchValue:"",
          deviceTypeKey :null,
          state :null,
        },
        //列表数据
        dataList:[],
        //数据数量
        total:0,
        //组件传参
        propsData:{},
      }
    },
    created () {

    },
    mounted () {
      this.iotTypeGetAllTypes();
      this.getList();
    },
    methods: {
      //查询按钮
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      //重置按钮
      resetQuery(){
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          searchValue:"",
          deviceTypeKey :null,
          state :null,
        });
        this.getList();
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        iotMonitorConfigList(this.queryParams).then(response => {
          this.$set(this,'loading',false);
          this.$set(this,'dataList',response.data.records);
          this.$set(this,'total',response.data.total);
        });
      },
      //操作按钮
      tableButton(type,row){
        let self = this;
        if(type == 1){
          //新增
          this.$set(this,'pageType',2);
          this.$set(this,'propsData',{});
        }else if(type == 2){
          //详情
          this.$set(this,'pageType',2);
          let obj = JSON.parse(JSON.stringify(row))
          obj.showType = true;
          this.$set(this,'propsData',obj);
        }else if(type == 3){
          //编辑
          this.$set(this,'pageType',2);
          let obj = JSON.parse(JSON.stringify(row))
          this.$set(this,'propsData',obj);
        }else if(type == 4){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            iotMonitorConfigDelete({configId:row.configId}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }else if(type == 5){
          //启用&停用
          let text = row.state  ? "停用" : "启用";
          this.$confirm('是否确认' + text + '?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            // stateFunction({id:row.id,state:!row.state,}).then(response => {
            //   self.msgSuccess(response.message)
            //   self.getList();
            // });
          }).catch(() => {});
        }else if(type == 6){
          //返回并刷新
          this.$set(this,'pageType',1);
          this.getList();
        }
      },
      //获取设备分类列表
      iotTypeGetAllTypes(){
        iotTypeGetAllTypes({}).then(response => {
          this.$set(this,'typeOptionList',response.data);
        });
      },
    },
  }
</script>
<style scoped lang="scss">
  .monitorConfig{
    .monitorConfigPage{

    }
  }
</style>
