<template>
  <div class="table-form">
    <Form :model="model" :ref="action" :rules="rules" :label-width="labelWidth">
      <Row :gutter="10">
        <Col span="24">
           <Form-item label="用户名" prop="username" v-validate:username.required.maxInputLength="rules">
            <Input v-model="model.username" placeholder="请输入用户登陆账号" :disabled="disabled"></Input>
            </Form-item>
        </Col>
      </Row>
     <Row :gutter="10">
                <Col span="24">
                <Form-item label="认证类型" prop="authType">
                    <Select v-model="model.authType" placeholder="请选择认证类型"  :disabled="disabled">
                        <Option :value="0">密码认证</Option>
                        <Option :value="1">LDAP认证</Option>
                    </Select>
                </Form-item>
                </Col>
        </Row>
        <Row :gutter="10" v-if="model.authType==0&&!model.id">
        <Col span="24">
            <FormItem label="密码" prop="password"  v-validate:password.required.maxInputLength="rules">
                <Input type="password" v-model="model.password"  placeholder="请输入密码"  :disabled="disabled"></Input>
            </FormItem>
        </Col>
      </Row>
        <Row :gutter="10" v-if="model.authType==0&&!model.id">
            <Col span="24">
            <FormItem label="确认密码" prop="confirmPassword"  v-validate:confirmPassword.required.maxInputLength="rules">
                <Input type="password" v-model="model.confirmPassword"  placeholder="请输入密码"  :disabled="disabled"></Input>
            </FormItem>
            </Col>
        </Row>
        <Row :gutter="10" >
            <Col span="24">
            <FormItem label="姓名" prop="name"  v-validate:name.required.maxInputLength="rules">
                <Input v-model="model.name" placeholder="请输入姓名" :disabled="disabled"></Input>
            </FormItem>
            </Col>
        </Row>
       <Row :gutter="10" >
        <Col span="24">
           <FormItem label="性别" prop="sex">
               <Select v-model="model.sex" placeholder="请选择性别"  :disabled="disabled">
                   <Option :value="0">男</Option>
                   <Option :value="1">女</Option>
               </Select>
            </FormItem>
        </Col>
      </Row>

      <Row :gutter="10">
        <Col span="24">
            <FormItem label="手机号码" prop="phone"  v-validate:phone.required.phone.maxInputLength="rules">
                <Input v-model="model.phone"  placeholder="请输入手机号码"  :disabled="disabled"></Input>
            </FormItem>
        </Col>
      </Row>
       <Row :gutter="10">
        <Col span="24">
           <FormItem label="电子邮件" prop="email"  v-validate:email.maxInputLength="rules">
                <Input v-model="model.email" placeholder="请输入电子邮件"  :disabled="disabled"></Input>
            </FormItem>
        </Col>
      </Row>
         <Row :gutter="10">
                <Col span="24">
                    <Form-item label="描述" prop="remark" v-validate:remark.maxTextLength="rules">
                        <Input v-model="model.remark" type="textarea" placeholder="请输入描述" :disabled="disabled"></Input>
                    </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 BaseUserForm extends Vue {
   
    loading : boolean = false;
    @Prop() private model!:any;
    @Prop(String) private action!:string;
    @Prop() private tableVm!:any;

    labelWidth:number =120;
    rules : any={};
    @Emit('close-modal') close(){}
    cancel () {
        this.close();
        
        (this.$refs[this.action] as any).resetFields();
    }

    save(){
        if(this.model.password!==this.model.confirmPassword){
            this.$tools.message('info',"两次密码输入不一致!")
            return;
        }
        this.getApiData('post',this.$msPrefix.base+'/api/v1/data/save/user',this.model,res=>{
            this.loading=!1;
            if (res.rtnCode == 0) {
                this.tableVm.getData();
                this.cancel();
                this.$tools.message('success',(this.action == 'update'?'修改':'新增')+'成功！')
            }else{
               this.$tools.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',"请完善必填项!")
          }
        });
      }

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

