<!--分类配置-->
<template>
  <div class="app-container classConfig">
    <div class="page-container classConfigPage" 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:param: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="label" />
          <el-table-column label="字段" align="left" prop="field" width="200" show-overflow-tooltip/>
          <el-table-column label="类型" align="left" prop="type" width="200" show-overflow-tooltip/>
          <!--<el-table-column label="数据类型" align="left" prop="dataType" show-overflow-tooltip/>-->
          <el-table-column label="排序" align="left" prop="sort" width="140" show-overflow-tooltip/>
          <el-table-column label="是否内置" align="left" prop="required" width="140" show-overflow-tooltip>
            <template slot-scope="scope">{{scope.row.required?'是':'否'}}</template>
          </el-table-column>
          <el-table-column label="状态" align="left" prop="state" width="140" show-overflow-tooltip>
            <template slot-scope="scope">{{scope.row.state?'启用':'停用'}}</template>
          </el-table-column>
          <el-table-column label="创建时间" align="left" 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="操作" align="left" width="260" 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="['iot:param:edit']"
                >编辑</p>
                <p class="table-button-p"
                   @click="tableButton(2,scope.row)"
                   v-hasPermiRouter="['iot:param:perms']"
                >配置</p>
                <p class="table-button-p"
                   @click="tableButton(3,scope.row)"
                   v-hasPermiRouter="['iot:param:del']"
                >删除</p>
                <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)"
                             v-hasPermiRouter="['iot:param:move']">
                  <p class="table-button-p">更多>></p>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="1">上移</el-dropdown-item>
                    <el-dropdown-item command="2">下移</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
                <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 v-if="pageType == 2" :propsData="propsData"></add-page>
    <!--新增弹窗-->
    <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="label">
        <el-input v-model="dialogForm.label" placeholder="请输入标题" maxLength="8" style="width:320px;"/>
      </el-form-item>
      <el-form-item label="字段" prop="field">
        <el-input v-model="dialogForm.field" placeholder="请输入字段" maxLength="20" style="width:320px;"/>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="dialogForm.type" placeholder="请选择类型" clearable style="width:320px;">
          <el-option
            v-for="dict in classOption"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <!--<el-form-item label="数据类型" prop="dataType">-->
      <!--<el-select v-model="dialogForm.dataType" placeholder="请选择数据类型" clearable style="width:320px;">-->
      <!--<el-option-->
      <!--v-for="dict in dataClassOption"-->
      <!--:key="dict.value"-->
      <!--:label="dict.label"-->
      <!--:value="dict.value"-->
      <!--&gt;</el-option>-->
      <!--</el-select>-->
      <!--</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="required">
        <el-radio-group v-model="dialogForm.required" style="width:320px;">
          <el-radio :label="true">必须</el-radio>
          <el-radio :label="false">非必须</el-radio>
        </el-radio-group>
      </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 { iotParamList,iotParamAdd,iotParamUpdate,iotParamDelete,iotParamDetail,iotParamMoveUp,iotParamMoveDown } from "@/api/iotDevice/index";
  import addPage from "./addPage.vue";
  export default {
    name: 'index',
    components: {
      addPage
    },
    data(){
      return{
        tableButtonType:this.hasPermiDom(['iot:param:edit','iot:param:perms','iot:param:del','iot:param:move']),
        pageType:1,
        loading:false,
        options:[{label:'启用',value:true},{label:'停用',value:false}],
        queryParams:{
          page:1,
          pageSize:20,
          searchValue :"",
          state :null,
        },
        dataList:[],
        total:0,
        dialogTitle:null,
        dialogType:false,
        dialogForm:{},
        dialogRules:{
          label: [
            { required: true, message: "请输入标题", trigger: "blur" },
            { required: true, message: "请输入标题", validator: this.spaceJudgment, trigger: "blur" }
          ],
          field: [
            { required: true, message: "请输入字段", trigger: "blur" },
            { required: true, message: "请输入字段", validator: this.spaceJudgment, trigger: "blur" }
          ],
          type: [
            { required: true, message: "请选择类型", trigger: "blur" },
          ],
          dataType: [
            { required: true, message: "请选择数据类型", trigger: "blur" },
          ],
          sort: [
            { required: true, message: "请输入", trigger: "blur" },
            { required: true, message: "请输入", validator: this.spaceJudgment, trigger: "blur" }
          ],
          required: [
            { required: true, message: "是否必须", trigger: "blur" },
          ],
          state: [
            { required: true, message: "请选择状态", trigger: "blur" },
          ],
        },
        classOption:[
          {label:'文本框',value:'text'},
          {label:'文本域',value:'textarea'},
          {label:'密码框',value:'password'},
          {label:'计数器',value:'number'},
          {label:'单选下拉框',value:'select'},
          {label:'多选下拉框',value:'selectMultiple'},
          // {label:'单选框',value:'radio'},
          // {label:'复选框',value:'checkbox'},
          // {label:'滑块',value:'slider'},
          // {label:'时间选择器',value:'timePicker'},
          // {label:'时间范围选择器',value:'timePickerRange'},
          // {label:'日期选择器',value:'datePicker'},
          // {label:'日期范围选择器',value:'datePickerRange'},
          // {label:'颜色选择器',value:'colorPicker'},
        ],
        dataClassOption:[
          {label:'字符型',value:'string'},
          {label:'布尔型',value:'boolean'},
          {label:'数字型',value:'int'},
          {label:'浮点型',value:'number'},
        ],
        //组件传参
        propsData:{},
      }
    },
    created(){

    },
    mounted(){
      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.id){
              let obj = {
                id:this.dialogForm.id,
                label:this.dialogForm.label,
                field:this.dialogForm.field,
                type:this.dialogForm.type,
                dataType:this.dialogForm.dataType,
                sort:this.dialogForm.sort,
                required:this.dialogForm.required,
                state:this.dialogForm.state,
              }
              iotParamUpdate(obj).then(response => {
                this.msgSuccess(response.message);
                this.getList();
                this.$set(this,'dialogType',false);
              });
            }else{
              iotParamAdd(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',{
          label:'',
          field:'',
          type:'',
          dataType:'',
          sort:'',
          required:true,
          state:true,
        });
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        iotParamList(this.queryParams).then(response => {
          this.$set(this,'loading',false);
          this.$set(this,'dataList',response.data.records);
          this.$set(this,'total',response.data.total);
        });
      },
      //更多按钮
      handleCommand(val,row){
        if(val == 1){
          //部门上移动
          iotParamMoveUp({id:row.id}).then(response => {
            this.msgSuccess(response.message)
            this.getList();
          });
        }else if(val == 2){
          //部门下移动
          iotParamMoveDown({id:row.id}).then(response => {
            this.msgSuccess(response.message)
            this.getList();
          });
        }
      },
      tableButton(type,row){
        let self = this;
        if(type == 1){
          //编辑
          iotParamDetail({id:row.id}).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 == 2){
          //配置
          this.$set(this,'propsData',row);
          this.$set(this,'pageType',2);
        }else if(type == 3){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            iotParamDelete({id:row.id}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }else if (type == 4){
          //返回并刷新
          this.$set(this,'pageType',1);
          self.getList();
        }
      },
    },
  }
</script>

<style scoped lang="scss">
  .classConfig{
    .classConfigPage{

    }
  }
</style>
