<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-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="手机号码：" prop="phoneNumber">
          <el-select style="width: 75px;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-input v-model="form.phoneNumber" style="width: 301px"></el-input>
        </el-form-item>
        <el-form-item label="原因备注：" prop="remark">
          <el-input type="text" maxlength="20" show-word-limit v-model="form.remark"
            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() {
    return {
      form: {
        memberId: '',
        memberName: '',
        memberGender: null,
        memberBirthday: '',
        birthType: 1,
        remark: '',
        phoneNumber: '',
        nationCode: ''
      },
      rules: {
        remark: [
          { required: true, message: "请输入原因备注", 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 {
                checkFalse();
                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,
          nationCode } = this.baseInfo;
        this.form = Object.assign({}, this.form, {
          memberId,
          memberName,
          memberGender,
          memberBirthday,
          birthType,
          remark,
          phoneNumber,
          nationCode
        });
        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;
}
</style>