<template>
  <div class="table-form">
    <Form :model="model" :ref="action" :rules="rules" :label-width="labelWidth">
      <Row :gutter="10">
        <Col span="24">
           <Form-item label="设备类型标识(name)" prop="name" v-validate:name.required.maxInputLength="rules">
            <Input v-model="model.name" placeholder="请输入设备类型标识(name)" :disabled="disabled"></Input>
            </Form-item>
        </Col>
      </Row>
      <Row :gutter="10" >
        <Col span="24">
           <Form-item label="设备类型键(uniqueKey)" prop="uniqueKey" v-validate:uniqueKey.required.maxInputLength="rules">
                <Input v-model="model.uniqueKey" placeholder="请输入设备类型键" :disabled="disabled"></Input>
            </Form-item>
        </Col>
      </Row>
      
      <Row :gutter="10">
        <Col span="24">
            <FormItem label="资源前缀(uri)" prop="uri"  v-validate:uri.required.maxInputLength="rules">
                <Input v-model="model.uri"  placeholder="请输入资源前缀uri"  :disabled="disabled"></Input>
            </FormItem>
        </Col>
      </Row>
      <Row :gutter="10" >
        <Col span="24">
            <FormItem label="是否展示(show)" prop="show" >
                <i-switch v-model="model.show"  :disabled="disabled"/>
            </FormItem>
        </Col>
      </Row>
        <Row :gutter="10">
            <Col span="24">
            <Form-item label="设备类型名称" prop="nameZh" v-validate:nameZh.required.maxInputLength="rules">
                <Input v-model="model.nameZh" placeholder="请输入设备类型名称" :disabled="disabled"></Input>
            </Form-item>
            </Col>
        </Row>
       <Row :gutter="10">
            <Col span="24">
              <Form-item label="动环配置" prop="profileId" v-validate:profileId.required.@change.$number="rules">
                  <Select v-model="model.profileId" placeholder="请选择动环配置"  :disabled="disabled">
                      <Option :value="profile.id" v-for="profile in profiles">{{profile.name}}</Option>
                  </Select>
              </Form-item>
            </Col>
        </Row>
        <Row :gutter="10" >
          <Col span="24">
            <Form-item label="设备类型传参" prop="params" v-validate:params.maxInputLength="rules">
                  <Input v-model="model.params" placeholder="请输入设备类型传参" :disabled="disabled" v-tip:code="psem_device_type_params"></Input>
              </Form-item>
          </Col>
        </Row>
         <Row :gutter="10">
                <Col span="24">
                    <Form-item label="指标值获取方式" prop="accessType" v-validate:accessType.required.@change.$number="rules">
                        <RadioGroup v-model="model.accessType" v-tip:code="psem_device_type_access_type" :disabled="disabled">
                            <Radio :label=0 :disabled="disabled">标准批量获取</Radio>
                            <Radio :label=1 :disabled="disabled">标准遍历获取</Radio>
                            <Radio :label=2 :disabled="disabled">遍历设备实例获取</Radio>
                        </RadioGroup>
                    </Form-item>
                </Col>
        </Row>
      <Row class="buttonDiv">
        <Button :loading="loading"
          @click="submitForm(action)"
          type="primary"
          v-if="!(action == 'detail')"
        >确&nbsp;&nbsp;定</Button>
        <Button @click="cancel">关&nbsp;&nbsp;闭</Button>
      </Row>
    </Form>
  </div>
</template>
<script lang="ts">
import { Component, Prop, Vue, Emit } from "vue-property-decorator";


@Component
export default class DeviceTypeForm extends Vue {
   
    loading : boolean = false;
    @Prop() private model!:any;
    @Prop(String) private action!:string;
    @Prop() private tableVm!:any;

    labelWidth:number =140;
    rules : any={};
    profiles : Array<any> =[];
    @Emit('close-modal') close(){}
    cancel () {
        this.close();
        this.model.show=false;
        (this.$refs[this.action] as any).resetFields();
    }

    save(){
      this.model.psemProfile=this.profiles.find((item)=>{return item.id==this.model.profileId});
      this.getApiData('post',this.$msPrefix.psem+'/api/v1/data/save/PSEMDeviceType',this.model,res=>{
           this.loading=!1;
            if (res.rtnCode == 0) {
                this.tableVm.getData();
                this.cancel();
                this.$tools.message('success',(this.action == 'update'?'修改':'新增')+'成功！')
            }else{
               this.$Message.error(res.msg);
            }
        },err=>{
          this.loading=!1;
        })
    }
    /*提交*/
      submitForm (action) {
      	
        (this.$refs[action] as any).validate((valid) => {
          if (valid) {
            if(!this.loading){
                this.loading=!0;
                this.save()
            }
          } else {
           this.$tools.message('info',"请完善必填项!")
          }
        });
      }

      getAllSettings(){
         let url =  this.$msPrefix.psem+"/api/v1/data/all/PSEMProfile";
          this.getApiData("get", url + "?size=1000", {}, res => {
              if (res.data.content.length > 0) {
                  this.profiles = res.data.content;
              }
          })
      }

      mounted(){
        this.getAllSettings();
      }

       get disabled(){
        return this.action=='detail'
      }
}
</script>

