<!-- 设备功能 -->
<template>
  <div class="app-container deviceFunction">
    <div class="page-container deviceFunctionPage" 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="typeSource">
            <el-select v-model="queryParams.typeSource" placeholder="请选择设备分类" @change="queryChange" style="width: 200px">
              <el-option label="硬件设备" :value="1"/>
              <el-option label="物联设备" :value="2"/>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="typeKey" v-if="queryParams.typeSource">
            <el-select v-model="queryParams.typeKey" placeholder="请选择设备类型" style="width: 200px">
              <el-option
                v-for="dict in queryOptionList"
                :key="dict.code"
                :label="dict.name"
                :value="dict.code"
              ></el-option>
            </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="dialogOpen"
             v-hasPermiRouter="['lab:riskDeviceType: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="riskDeviceTypeName"  show-overflow-tooltip/>
          <el-table-column label="分类" prop="typeSource" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.typeSource == 1 ? '硬件设备' : (scope.row.typeSource == 2 ? '物联设备' : '')}}</span>
            </template>
          </el-table-column>
          <el-table-column label="类型" prop="typeKey" width="100" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="scope.row.typeSource == 1">
                <span v-for="item in iotHardware" v-show="item.code == scope.row.typeKey">{{item.name}}</span>
              </span>
              <span v-if="scope.row.typeSource == 2">
                <span v-for="item in iotTypeList" v-show="item.code == scope.row.typeKey">{{item.name}}</span>
              </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(1,scope.row)"
                   v-hasPermiRouter="['lab:riskDeviceType:edit']"
                >编辑</p>
                <p class="table-button-p"
                   @click="tableButton(2,scope.row)"
                   v-hasPermiRouter="['lab:riskDeviceType: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>
    <!--新增弹窗-->
    <el-dialog class="planMusicPage-dialog" :title='dialogTitle' width="540px" append-to-body
               :visible.sync="dialogType" v-if="dialogType" @close="dialogOff()"
               :close-on-click-modal="false" :close-on-press-escape="false">
      <div class="planMusicPage-dialog-box">
        <el-form :model="dialogForm" class="dialogForm" ref="dialogForm"
                 :rules="rules" style="width:100%;" label-width="90px">
          <el-form-item label="名称：" prop="riskDeviceTypeName">
            <el-input
              maxLength="30"
              v-model="dialogForm.riskDeviceTypeName"
              placeholder="请输入名称"
              clearable
              style="width: 360px"
            />
          </el-form-item>
          <el-form-item label="分类：" prop="typeSource">
            <el-radio-group v-model="dialogForm.typeSource" @change="dialogChange">
              <el-radio :label="1">硬件设备</el-radio>
              <el-radio :label="2">物联设备</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="类型：" prop="typeKey">
            <el-select v-model="dialogForm.typeKey"
                       placeholder="请选择设备类型" style="width: 360px">
              <el-option
                v-for="dict in dialogOptionList"
                :key="dict.code"
                :label="dict.name"
                :value="dict.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="说明：" prop="remark">
            <el-input
              v-model="dialogForm.remark"
              maxLength="200"
              placeholder="请输入说明"
              :show-word-limit="true"
              style="width:360px;"
              type="textarea"
              resize="none"
              :rows="2"
            />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer dialog-footer-box">
        <p class="dialog-footer-button-null"></p>
        <p class="dialog-footer-button-info" @click="dialogOff()">取消</p>
        <p class="dialog-footer-button-primary" @click="dialogSubmit">提交</p>
        <p class="dialog-footer-button-null"></p>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import { laboratoryRiskDeviceTypeList,laboratoryRiskDeviceTypeAdd,laboratoryRiskDeviceTypeUpdate,
    laboratoryRiskDeviceTypeDelete,laboratoryRiskDeviceTypeDetail } from "@/api/emergencyManagement/index";
  import { iotTypeGetTypeKey,iotHardwareTypeGetTypeKey } from "@/api/iotDevice/index";
  export default {
    name: 'index',
    //components: {
    //  addPage
    //},
    data () {
      return {
        tableButtonType:this.hasPermiDom(['lab:riskDeviceType:edit','lab:riskDeviceType:del',]),
        //页面状态
        pageType:1,
        //页面遮罩
        loading:false,
        iotTypeList:[],
        iotHardware:[],
        //下拉列表数据
        queryOptionList:[],
        //查询条件
        queryParams:{
          page:1,
          pageSize:20,
          searchValue:"",
          typeSource :null,
          typeKey:null,
        },
        //列表数据
        dataList:[],
        //数据数量
        total:0,
        //弹窗相关
        dialogTitle:null,
        dialogType:false,
        dialogForm:{},
        dialogOptionList:[],
        rules:{
          riskDeviceTypeName:[
            { required: true, message: '请输入名称', trigger: 'blur' },
            { required: true, message: "请输入名称", validator: this.spaceJudgment, trigger: "blur" }
          ],
          typeSource:[
            { required: true, message: '请选择分类', trigger: 'blur' },
            { required: true, message: "请选择分类", validator: this.spaceJudgment, trigger: "blur" }
          ],
          typeKey:[
            { required: true, message: '请选择类型', trigger: 'blur' },
            { required: true, message: "请选择类型", validator: this.spaceJudgment, trigger: "blur" }
          ],
        },
      }
    },
    created () {

    },
    mounted () {
      this.iotTypeGetTypeKey();
      this.iotHardwareTypeGetTypeKey();
      this.getList();
    },
    methods: {
      //弹层选中
      dialogChange(val){
        this.$set(this.dialogForm,'typeKey',null);
        if(val == 1){
          this.$set(this,'dialogOptionList',this.iotHardware);
        }else if(val == 2){
          this.$set(this,'dialogOptionList',this.iotTypeList);
        }
      },
      //弹层关闭
      dialogOff(){
        this.$set(this,'dialogType',false);
      },
      //弹层开启
      dialogOpen(){
        this.dialogFormReset();
        this.$set(this,'dialogTitle','新增');
        this.$set(this,'dialogType',true);
      },
      //弹层确定
      dialogSubmit(){
        // let obj = JSON.parse(JSON.stringify(this.dialogForm))
        let obj = {
          riskDeviceTypeName:this.dialogForm.riskDeviceTypeName,
          typeSource:this.dialogForm.typeSource,
          typeKey:this.dialogForm.typeKey,
          remark:this.dialogForm.remark,
        }
        if(this.dialogForm.id){
          obj.id = this.dialogForm.id;
          laboratoryRiskDeviceTypeUpdate(obj).then(response => {
            this.msgSuccess(response.message);
            this.getList();
          });
        }else{
          laboratoryRiskDeviceTypeAdd(obj).then(response => {
            this.msgSuccess(response.message);
            this.getList();
          });
        }
        this.$set(this,'dialogType',false);
      },
      dialogFormReset(){
        this.$set(this,'dialogForm',{
          riskDeviceTypeName:'',
          typeSource:'',
          typeKey:'',
          remark:'',
        });
      },
      //查询按钮
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      //重置按钮
      resetQuery(){
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          searchValue:"",
          typeSource:null,
          typeKey:null,
        });
        this.$set(this,'queryOptionList',[])
        this.getList();
      },
      //搜索栏选中分类
      queryChange(val){
        this.$set(this.queryParams,'typeKey',null);
        if(val == 1){
          this.$set(this,'queryOptionList',this.iotHardware);
        }else if(val == 2){
          this.$set(this,'queryOptionList',this.iotTypeList);
        }
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        laboratoryRiskDeviceTypeList(obj).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,'dialogTitle','编辑');
          this.$set(this,'dialogForm',JSON.parse(JSON.stringify(row)));
          if(row.typeSource == 1){
            this.$set(this,'dialogOptionList',this.iotHardware);
          }else if(row.typeSource == 2){
            this.$set(this,'dialogOptionList',this.iotTypeList);
          }
          this.$set(this,'dialogType',true);
        }else if(type == 2){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            laboratoryRiskDeviceTypeDelete({id:row.id}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }
      },
      //获取物联分类下拉列表
      iotTypeGetTypeKey(){
        iotTypeGetTypeKey({}).then(response => {
          this.$set(this,'iotTypeList',response.data);
        });
      },
      //获取硬件分类下拉列表
      iotHardwareTypeGetTypeKey(){
        iotHardwareTypeGetTypeKey({}).then(response => {
          this.$set(this,'iotHardware',response.data);
        });
      },
    },
  }
</script>
<style scoped lang="scss">
  .deviceFunction{
    .deviceFunctionPage{

    }
  }
</style>
