<!--物联分类-->
<template>
  <div class="app-container iotClassification">
    <div class="page-container iotClassification-page" 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="请输入名称/标识"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="" prop="state ">
            <el-select v-model="queryParams.state " clearable placeholder="请选择状态" style="width: 200px">
              <el-option
                v-for="dict in options"
                :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-submit-common-style-button"
             style="float: right;"
             @click="dialogOpen"
             v-hasPermiRouter="['iot:type: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="名称" align="left" prop="typeName" />
          <el-table-column label="标识" align="left" prop="typeKey" show-overflow-tooltip/>
          <el-table-column label="是否内置" align="left" prop="isInner" show-overflow-tooltip>
            <template slot-scope="scope">{{scope.row.isInner?'是':'否'}}</template>
          </el-table-column>
          <el-table-column label="状态" align="left" prop="state" show-overflow-tooltip>
            <template slot-scope="scope">{{scope.row.state?'启用':'停用'}}</template>
          </el-table-column>
          <el-table-column label="创建时间" align="left" prop="createTime" 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="操作" align="left" class-name="small-padding fixed-width" 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="['iot:type:detail']"
                >详情</p>
                <p class="table-button-p"
                   @click="tableButton(3,scope.row)"
                   v-hasPermiRouter="['iot:type:edit']"
                >编辑</p>
                <p class="table-button-p"
                   @click="tableButton(4,scope.row)"
                   v-hasPermiRouter="['iot:type: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>
    <infoPage v-if="pageType == 2" :propsData="propsData"></infoPage>
    <!--新增弹窗-->
    <el-dialog class="iotClassification-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">
      <el-form :model="dialogForm" ref="dialogForm" :inline="true" :rules="dialogRules" class="addCheckPage-min" label-width="80px">
        <el-form-item label="类型" prop="typeKey">
          <el-select v-model="dialogForm.typeKey" @change="typeSelectChange" placeholder="请选择类型" style="width: 320px">
            <el-option
              v-for="dict in iotTypeOptions"
              :key="dict.code"
              :label="dict.name"
              :value="dict.code"/>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="dialogForm.state">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            placeholder="请输入内容"
            v-model="dialogForm.remark"
            resize="none"
            maxLength="50"
            style="width:320px;"
            :rows="3"
            show-word-limit
          ></el-input>
        </el-form-item>
      </el-form>
      <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 { iotTypeList,iotTypeDetail,iotTypeAdd,iotTypeUpdate,iotTypeDelete,iotTypeGetTypeKey } from "@/api/iotDevice/index";
  import infoPage from "./infoPage.vue"
  export default {
    name: 'index',
    components: {
      infoPage
    },
    data(){
      return{
        tableButtonType:this.hasPermiDom(['iot:type:detail','iot:type:edit','iot:type:del']),
        loading:false,
        pageType:1,
        options:[{label:'启用',value:true},{label:'停用',value:false}],
        iotTypeOptions:[],
        queryParams:{
          page:1,
          pageSize:20,
          searchValue :"",
          jobGroup:null,
          state :null,
        },
        dataList:[],
        total:0,
        dialogTitle:null,
        dialogType:false,
        dialogForm:{
          typeName:"",
          typeKey:"",
          state:true,
          remark:"",
        },
        dialogRules:{
          typeName: [
            { 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" }
          ],
          state: [
            { required: true, message: "请选择状态", trigger: "blur" },
          ],
        },
        //组件传参
        propsData:{},
      }
    },
    created(){

    },
    mounted(){
      this.iotTypeGetTypeKey();
      this.getList();
    },
    methods:{
      //弹层关闭
      dialogOff(){
        this.$set(this,'dialogType',false);
      },
      //弹层开启
      dialogOpen(){
        this.dialogFormReset();
        this.$set(this,'dialogTitle','新增');
        this.$set(this,'dialogType',true);
      },
      //弹层确定
      dialogSubmit(){
        this.$refs["dialogForm"].validate(valid => {
          if (valid) {
            if(this.dialogForm.typeId){
              let obj = {
                typeId:this.dialogForm.typeId,
                typeName:this.dialogForm.typeName,
                typeKey:this.dialogForm.typeKey,
                state:this.dialogForm.state,
                remark:this.dialogForm.remark,
              }
              iotTypeUpdate(obj).then(response => {
                this.msgSuccess(response.message);
                this.getList();
                this.$set(this,'dialogType',false);
              });
            }else{
              iotTypeAdd(this.dialogForm).then(response => {
                this.msgSuccess(response.message);
                this.getList();
                this.$set(this,'dialogType',false);
              });
            }
          }
        })
      },
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      resetQuery(){
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          searchValue :"",
          state :null,
        });
        this.getList();
      },
      dialogFormReset(){
        this.$set(this,'dialogForm',{
          typeName:"",
          typeKey:"",
          state:true,
          remark:"",
        });
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        iotTypeList(this.queryParams).then(response => {
          this.$set(this,'dataList',response.data.records);
          this.$set(this,'total',response.data.total);
          this.$set(this,'loading',false);
        });
      },
      tableButton(type,row){
        let self = this;
        if(type == 2){
          //详情
          this.$set(this,'propsData',JSON.parse(JSON.stringify(row)));
          this.$set(this,'pageType',2);
        }else if(type == 3){
          //编辑
          iotTypeDetail({id:row.typeId}).then(response => {
            this.dialogFormReset();
            this.$set(this,'dialogTitle','编辑');
            this.$set(this,'dialogForm',JSON.parse(JSON.stringify(response.data)));
            this.$set(this,'dialogType',true);
          });
        }else if(type == 4){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            iotTypeDelete({typeId:row.typeId}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }else if(type == 'out'){
          this.$set(this,'pageType',1);
        }
      },
      iotTypeGetTypeKey(){
        iotTypeGetTypeKey({}).then(response => {
          this.$set(this,'iotTypeOptions',response.data);
        });
      },
      //选中类型
      typeSelectChange(val){
        let self = this;
        for(let i=0;i<self.iotTypeOptions.length;i++){
          if(val == self.iotTypeOptions[i].code){
            self.$set(self.dialogForm,'typeName',self.iotTypeOptions[i].name);
          }
        }
      },
    },
  }
</script>

<style scoped lang="scss">
.iotClassification{
  .iotClassification-page{

  }
}
</style>
