<template>
  <div>
    <el-dialog title="编辑基本信息" custom-class="customer-dailog" :visible.sync="dialogVisible"
      :close-on-click-modal="false" @close="cancel" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="133px">
        <el-form-item label="姓名：" prop="memberName">
          <el-input type="text" maxlength="10" show-word-limit placeholder="请输姓名"
            v-model="form.memberName" style="width: 252px;"></el-input>
        </el-form-item>
        <el-form-item label="性别：" prop="memberGender">
          <el-select v-model="form.memberGender" placeholder="请选择" style="width: 252px;">
            <el-option label="男" :value="1"></el-option>
            <el-option label="女" :value="2"></el-option>
            <el-option label="未知" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期：" prop="memberBirthday">
          <el-select v-model="form.birthType" placeholder="请选择"
            style="width: 106px;margin-right:6px;">
            <el-option label="阳历" :value="1"></el-option>
            <el-option label="农历" :value="0"></el-option>
          </el-select>
          <el-date-picker :clearable="false" value-format="timestamp" style="width: 136px;"
            v-model="form.memberBirthday" type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="手机号码：">
          <el-select style="width: 106px;margin-right:6px;" v-model="form.nationCode">
            <el-option v-for="item in nationCodeList" :key="item.dictCode" :value="item.dictCode"
              :label="item.dictName"></el-option>
          </el-select>
          <el-form-item prop="phoneNumber" style="display:inline-block" class="gray-phone-error">
            <el-input v-model="form.phoneNumber" style="width: 270px"></el-input>
          </el-form-item>
        </el-form-item>
        <el-form-item label="原因备注：" prop="remark">
          <el-input type="text" maxlength="20" show-word-limit v-model="form.remark"
            placeholder="请输入内容" style="width: 386px;"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" :loading="btnLoading" @click="submit">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { fomatBirthday } from "../../../utils/fomatBirthday";
import { doFetch } from "../../axios/api";
import url from "../../axios/url";
import { mapState } from "vuex";
import { checkFalse, checkStatus, checkSuccess } from "../../../../static/js/checkStatus";
export default {
  name: "baseinfoDialog",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    baseInfo: {
      type: Object,
      default: () => {
        return {};
      }
    },
    nationCodeList: {
      type: Array,
      default: () => {
        return [];
      }
    },
  },
  data() {
    var validateTel = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入号码"));
        // return callback();
      } else if (value.indexOf('*') == -1) {
        if (!this.form.nationCode) {
          this.form.nationCode = "86";
          if (!/^0?1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(value)) {
            callback(new Error("请输入正确号码"));
          }
        }
        if (this.form.nationCode) {
          if (this.form.nationCode == "86") {
            if (!/^0?1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(value)) {
              callback(new Error("请输入正确号码"));
            }
          } else {
            if (!/^(\d{1,11})$/.test(value)) {
              callback(new Error("请输入正确号码"));
            }
          }
        }
        callback();
      } else {
        if (!this.form.nationCode) {
          this.form.nationCode = "86";
          if (!/^0?1[3|4|5|6|7|8|9][0-9]\*{4}\d{4}$/.test(value)) {
            callback(new Error("请输入正确号码"));
          }
        }
        if (this.form.nationCode) {
          if (this.form.nationCode == "86") {
            if (!/^0?1[3|4|5|6|7|8|9][0-9]\*{4}\d{4}$/.test(value)) {
              callback(new Error("请输入正确号码"));
            }
          } else {
            if (!/^\d{3}\*{4}\d{4}$/.test(value)) {
              callback(new Error("请输入正确号码"));
            }
          }
        }
        callback();
      }
    };
    return {
      form: {
        memberId: '',
        memberName: '',
        memberGender: null,
        memberBirthday: '',
        birthType: 1,
        remark: '',
        phoneNumber: '',
        nationCode: '86'
      },
      rules: {
        remark: [ { required: true, message: "请输入原因备注", trigger: "blur" } ],
        phoneNumber: [ { validator: validateTel, trigger: "blur" } ]
      },
      btnLoading: false,
    };
  },
  computed: {
    ...mapState([ "showEditClique" ])
  },
  methods: {
    cancel() {
      this.$refs[ 'form' ].resetFields();
      this.$emit('update:dialogVisible', false);
    },
    submit() {
      this.$refs[ 'form' ].validate(valid => {
        if (valid) {
          this.btnLoading = true;
          doFetch(url.updateBaseDetail, Object.assign({}, this.form, { memberBirthday: fomatBirthday(this.form.memberBirthday) }))
            .then(res => {
              if (res.data.errorCode === 0) {
                setTimeout(() => {
                  this.btnLoading = false;
                  checkSuccess();
                  this.$refs[ 'form' ].resetFields();
                  this.$emit('update:dialogVisible', false);
                  this.$emit('refresh');
                }, 2000);
              } else {
                this.$message.error(res.data.response.message);
                this.btnLoading = false;
              }
            })
            .catch(err => {
              this.btnLoading = false;
              checkStatus(err);
            });
        } else {
          return false;
        }
      });
    },
  },
  watch: {
    dialogVisible(n, o) {
      if (n) {
        const { memberId,
          memberName,
          memberGender,
          memberBirthday,
          birthType,
          remark,
          phoneNumber
        } = this.baseInfo;
        this.form = Object.assign({}, this.form, {
          memberId,
          memberName,
          memberGender,
          memberBirthday,
          birthType,
          remark,
          phoneNumber,
          nationCode: this.baseInfo.nationCode || '86'
        });
        this.$refs[ 'form' ].clearValidate();
      }
    }
  }
};

</script>
<style lang="less">
// 去除dailog-footer上边框
.customer-dailog .el-dialog__footer {
  padding-top: 0;
  padding-bottom: 20px;
  border-top: none !important;
}
.customer-dailog .el-dialog__body {
  padding-bottom: 0;
  .gray-phone-error.is-error .el-form-item__error {
    color: #c0c4cc;
  }
}
</style>