<template>
  <div class="organizationalStructure-addPage">
    <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"
        >新增</p>
        <p class="page-out-common-style-button"
           style="float: right;margin-right:20px;"
           @click="backPage"
        >返回</p>
      </el-form>
    </div>
    <div class="content-box">
      <el-table class="table-box" v-loading="loading" border :data="dataList">
        <el-table-column label="序号" type="index" align="center" width="50"/>
        <el-table-column label="名称" align="left" prop="dictKey"/>
        <el-table-column label="关联值" align="left" prop="dictValue" show-overflow-tooltip/>
        <el-table-column label="排序" align="left" prop="sort" width="80"  show-overflow-tooltip/>
        <el-table-column label="状态" align="left" prop="state" width="120"  show-overflow-tooltip>
          <template slot-scope="scope">
            {{scope.row.state?'启用':'停用'}}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="left" prop="createTime" width="180"  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" width="260">
          <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)"
              >编辑</p>
              <p class="table-button-p"
                 @click="tableButton(2,scope.row)"
              >删除</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>
    <el-dialog class="classConfig-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="dictKey">
          <el-input v-model="dialogForm.dictKey" placeholder="请输入名称" maxLength="10" style="width:320px;"/>
        </el-form-item>
        <el-form-item label="关联值" prop="dictValue">
          <el-input v-model="dialogForm.dictValue" placeholder="请输入标识" maxLength="20" style="width:320px;"/>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="dialogForm.sort" controls-position="right" :min="1" :max="9999" style="width:320px;"></el-input-number>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="dialogForm.state" style="width:320px;">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">禁用</el-radio>
          </el-radio-group>
        </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 { iotParamValueList,iotParamValueAdd,
    iotParamValueUpdate,iotParamValueDelete,iotParamValueDetail } from "@/api/iotDevice/index";
  export default {
    name: 'addPage',
    props:{
      propsData:{},
    },
    data(){
      return{
        loading:false,
        showType:false,
        newData:{},
        options:[{label:'启用',value:true},{label:'停用',value:false}],
        queryParams:{
          page:1,
          pageSize:20,
          searchValue:"",
          state :null,
        },
        dataList:[],
        total:0,
        dialogTitle:null,
        dialogType:false,
        dialogForm:{},
        dialogRules:{
          dictKey: [
            { required: true, message: "请输入名称", trigger: "blur" },
            { required: true, message: "请输入名称", validator: this.spaceJudgment, trigger: "blur" }
          ],
          dictValue: [
            { required: true, message: "请输入关联值", trigger: "blur" },
            { required: true, message: "请输入关联值", validator: this.spaceJudgment, trigger: "blur" }
          ],
          sort: [
            { required: true, message: "请输入排序", trigger: "blur" },
            { required: true, message: "请输入排序", validator: this.spaceJudgment, trigger: "blur" }
          ],
          state: [
            { required: true, message: "请选择状态", trigger: "blur" },
          ],
        },
      }
    },
    created(){

    },
    mounted(){
      this.getList();
    },
    methods:{
      // 返回按钮
      backPage(){
        this.$parent.tableButton(4);
      },
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      resetQuery(){
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          searchValue:"",
          state :null,
        });
        this.getList();
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        obj.paramId = this.propsData.id;
        iotParamValueList(obj).then(response => {
          this.$set(this,'loading',false);
          this.$set(this,'dataList',response.data.records);
          this.$set(this,'total',response.data.total);
        });
      },
      dialogFormReset(){
        this.$set(this,'dialogForm',{
          dictKey:'',
          dictValue:'',
          sort:1,
          state:true,
        });
      },
      //弹层关闭
      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.id){
              let obj = {
                paramId:this.propsData.id,
                id:this.dialogForm.id,
                dictKey:this.dialogForm.dictKey,
                dictValue:this.dialogForm.dictValue,
                sort:this.dialogForm.sort,
                state:this.dialogForm.state,
              }
              iotParamValueUpdate(obj).then(response => {
                this.msgSuccess(response.message);
                this.getList();
                this.$set(this,'dialogType',false);
              });
            }else{
              let obj = {
                paramId:this.propsData.id,
                dictKey:this.dialogForm.dictKey,
                dictValue:this.dialogForm.dictValue,
                sort:this.dialogForm.sort,
                state:this.dialogForm.state,
              }
              iotParamValueAdd(obj).then(response => {
                this.msgSuccess(response.message);
                this.getList();
                this.$set(this,'dialogType',false);
              });
            }
          }
        })
      },
      tableButton(type,row){
        let self = this;
        if(type == 1){
          //编辑
          iotParamValueDetail({id:row.id}).then(response => {
            this.dialogFormReset();
            this.$set(this,'dialogTitle','编辑');
            this.$set(this,'dialogForm',{
              id:response.data.id,
              dictKey:response.data.dictKey,
              dictValue:response.data.dictValue,
              sort:response.data.sort,
              state:response.data.state,
            });
            this.$set(this,'dialogType',true);
          });
        }else if(type == 2){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            iotParamValueDelete({id:row.id}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }
      },
    },
  }
</script>

<style scoped lang="scss">
  .organizationalStructure-addPage{
    flex:1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    .title-box{
      padding-top:20px;
      border-bottom:1px solid #dedede;
      display: flex;
    }
    .content-box{
      flex: 1;
      display: flex;
      flex-direction: column;
      padding:20px;
      overflow: hidden;
    }
  }
</style>
