<template>
  <!-- 弹出框表单 -->
  <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row>

        <Field :span="20" label="姓名" prop="name" v-model="form.name" placeholder="请输入个人姓名"/>
        <Field :span="20" label="联系方式" prop="contact" v-model="form.contact" placeholder="请输入联系方式"/>
        <Field :span="20" label="证件类型" prop="idCardType" v-model="form.idCardType" type="radio" :enumData="dict.idCardType" placeholder="请输入证件类型"/>
        <Field :span="20" label="证件号码" prop="idCardNo" v-model="form.idCardNo" placeholder="请输入证件号码"/>

      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" v-if="pageInfo.type !== 'view'" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import form from "@/assets/mixins/formdialog";
import dialogShow from "./dialogshow";

export default {
  mixins: [form],
  components: {
    dialogShow,
  },
  data() {
    let validatorSheet = (rule, value, callback)=>{
        let Reg = /^([1-6][1-9]|50)\d{4}\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}$/
        let regt =  /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
        
        if( Reg.test( value ) || regt.test( value )  ) {
            callback()
        } else {
            callback(new Error('输入正确格式的身份证号'))
        }
    }
    let phoneReg = (rule, value, callback) =>{
        let regExp=/^(((\+86)|(\+86-))|((86)|(86\-))|((0086)|(0086\-)))?1[3|5|7|8]\d{9}$/;
        if(regExp.test(value)){
            callback()
        }else{
            callback(new Error('请输入正确格式的手机号码'))
        }
    }
    return {
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "预约自动签到服务约束",
      // 是否显示弹出层
      open: false,
      toString: [
        "createMethod",
        "constraint",
        "idCardType",
        "personId",
      ],
      // 表单校验
      rules: {
        
        idCardNo: [
            {required: true,message: "请输入身份证号码", trigger: "blur" },
            {max: 18,message: "最多只能录入18个字符",trigger: "blur",},
            {validator: validatorSheet,trigger: "blur"}
        ],
        name: [
            {required: true,message: "请输入姓名", trigger: "blur" },
        ],
        idCardType: [
            {required: true,message: "请输入证件类型", trigger: "blur" },
        ],
        contact:[
            {required: true,message: "请输入联系方式", trigger: "blur" },
            {validator:phoneReg, trigger: "blur" },
        ]
      }
    };
  },

  methods: {
    /** 编辑 */
    edit(row) {
      this.reset()
      this.query = {id: row.id};
      this.urls.currUrl = "appointment/constraint/edit";
      this.getData();
      this.pageInfo.type = "edit"
      this.title = "修改预约自动签到服务约束";
    },
    /** 新增 */
    add(row) {
      this.reset()
      this.urls.currUrl = "appointment/constraint/add";
      this.getData();
      this.pageInfo.type = "add"
      this.title = "新增预约自动签到服务约束";
    },
    /** 查看*/
    view(row) {
      this.reset()
      this.query = {id: row.id};
      this.urls.currUrl = "appointment/constraint/view";
      this.getData();
      this.pageInfo.type = "view"
      this.title = "预约自动签到服务约束详细";
    },
    /**取消按钮 */
    cancel() {
      this.open = false;
    },
    /**获取数据后弹框 */
    afterRender(data) {
      this.open = true;
    },

    // beforeSubmit(data) {
    //   // data.name = this.dict['personId'][data.personId];
    //   return data;
    // },

    afterSubmit(data) {
      this.open = false;
      this.$emit("ok");
    },

    // 表单重置
    reset() {
      this.form = {
        siteId: null,
        siteName: "",
        personId: null,
        name: "",
        contact: "",
        idCardType: "",
        idCardNo: "",
        accumulative: 0,
        constraintTime: null,
        createMethod: null,
        operator: "",
        constraint: 0,
      };
      this.resetForm("form");
    },
    resetForm(refName) {
      if (this.$refs[refName]) {
        this.$refs[refName].resetFields();
      }
    },
  },
};
</script>
