<template>
  <div class="table-form">
    <Form :model="jdbc" :ref="action" :rules="rules" :label-width="labelWidth">
      <Row :gutter="10">
        <Col span="24">
          <Form-item label="名称" prop="name" v-validate:name.required.maxInputLength="rules">
            <Input v-model="jdbc.name" placeholder="请输入名称" :disabled="disabled"></Input>
          </Form-item>
        </Col>
      </Row>
      <Row :gutter="10">
        <Col span="24">
          <FormItem label="数据源" prop="code" v-validate:code.required.@change="rules">
            <Select v-model="jdbc.code" placeholder="选择数据源" :disabled="disabled">
              <Option v-for="item in dataOriginOption" v-model="item.code">{{item.code}}({{item.description}})</Option>
            </Select>
          </FormItem>
        </Col>
      </Row>
      <Row :gutter="10">
        <Col span="24">
          <FormItem label="执行sql语句" prop="sql" v-validate:sql.required.maxTextLength="rules">
            <Input
              v-model="jdbc.sql"
              type="textarea"
              :autosize="{minRows: 2,maxRows: 5}"
              placeholder="请输入sql语句" v-tip:code="sms_jdbc_sql" :disabled="disabled"
            ></Input>
          </FormItem>
        </Col>
      </Row>
      <Row :gutter="10">
        <Col span="24">
          <FormItem label="替换参数" prop="params"  v-validate:params.maxInputLength="rules">
            <Input
              v-model="jdbc.params"
              type="textarea"
              :autosize="{minRows: 2,maxRows: 5}"
              placeholder="请输入替换参数" v-tip:code="sms_jdbc_param" :disabled="disabled"
            ></Input>
          </FormItem>
        </Col>
      </Row>
      <Row :gutter="10">
        <Col span="24">
          <FormItem label="扩展信息" prop="extInfo" v-validate:extInfo.maxTextLength="rules">
            <Input
              v-model="jdbc.extInfo"
              type="textarea"
              :autosize="{minRows: 2,maxRows: 5}" :disabled="disabled"
              placeholder="请输入扩展信息"  
            ></Input>
          </FormItem>
        </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 JdbcForm extends Vue {
   

    @Prop() private jdbc!:any;
    @Prop(String) private action!:string;
    @Prop() private tableInfo!:any;
    @Prop() private tableVm!:any;

    loading : boolean = false;
    labelWidth:number =120;
    rules:any={};
    dataOriginOption : Array<any> = [];
    getDbCollector(){
        this.getApiData('get',this.$msPrefix.base+'/api/v1/data/all/DBCollector',{},res=>{
            if (res.rtnCode == 0) {
               this.dataOriginOption=res.data.content
            }
        })
    }
    mounted(){
        this.getDbCollector()
    }
    save(){
        this.getApiData('post',this.$msPrefix.sms+'/api/v1/data/save/jdbcProfile',this.jdbc,res=>{
           this.loading=!1;
            if (res.rtnCode == 0) {
                this.tableVm.getData();
                this.cancel();
                this.$tools.message('success',(this.action == 'update'?'修改':'新增')+'成功！')
            }
        },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',"请完善必填项!")
           
          }
        });
      }

    @Emit('closeModal') close(){}
    cancel() {  
      	this.close();
        (this.$refs[this.action] as any).resetFields();
      }

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

