<template>
  <div>
    <el-dialog
      title="编辑基本信息"
      custom-class="customer-dialog"
      :visible.sync="visible"
      :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-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 label="女" :value="2" />
          </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 label="农历" :value="0" />
          </el-select>
          <el-date-picker
            :clearable="false"
            value-format="timestamp"
            style="width: 136px;"
            v-model="form.memberBirthday"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="手机号码：">
          <el-select
            style="width: 106px;margin-right:6px;"
            v-model="form.nationCode"
            :disabled="phoneNumber&&!isEditPhone"
          >
            <el-option
              v-for="item in nationCodeList"
              :key="item.dictCode"
              :value="item.dictCode"
              :label="item.dictName"
            />
          </el-select>
          <el-form-item
            v-if="phoneNumber"
            prop="phoneNumber"
            style="display:inline-block"
            class="gray-phone-error"
          >
            <el-input
              v-model="form.phoneNumber"
              :style="{width: isEditPhone?'170px':'215px'}"
              :disabled="phoneNumber&&!isEditPhone"
            />
            <el-button
              type="text"
              v-if="!isEditPhone"
              style="margin-left:20px;"
              @click="isEditPhone=true"
            >
              修改
            </el-button>
            <template v-else>
              <el-button type="text" style="margin-left:20px;" @click="cancelPhoneEdit">
                取消
              </el-button>
              <el-button type="text" :disabled="!form.phoneNumber" @click="savePhoneEdit">
                保存
              </el-button>
            </template>
            <div v-if="showError" class="el-form-item__error" style="color:#c0c4cc;">
              请输入正确的手机号码
            </div>
          </el-form-item>
          <el-form-item
            v-else
            prop="phoneNumber"
            style="display:inline-block"
            class="gray-phone-error"
          >
            <el-input v-model="form.phoneNumber" style="width: 270px" />
          </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-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" :loading="btnLoading" @click="submit" :disabled="isEditPhone">确定</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 { 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 {
      visible: false,
      form: {
        memberId: '',
        memberName: '',
        memberGender: null,
        memberBirthday: '',
        birthType: 1,
        remark: '',
        phoneNumber: '',
        nationCode: '86',
      },
      phoneNumber: '',
      showError: false,
      isEditPhone: false,
      rules: {
        remark: [{ required: true, message: '请输入原因备注', trigger: 'blur' }],
      },
      btnLoading: false,
    };
  },
  computed: {
    ...mapState(['showEditClique']),
  },
  watch: {
    dialogVisible (n, o) {
      this.visible = n;
      if (n) {
        const {
          memberId,
          memberName,
          memberGender,
          memberBirthday,
          birthType,
          remark,
          phoneNumber,
          nationCode,
        } = this.baseInfo;
        this.form = Object.assign({}, this.form, {
          memberId,
          memberName,
          memberGender: memberGender || '',
          memberBirthday,
          birthType,
          remark,
          phoneNumber,
          nationCode: nationCode || '86',
        });
        this.phoneNumber = phoneNumber;
        this.$refs.form.clearValidate();
      }
    },
  },
  methods: {
    cancel () {
      this.$refs.form.resetFields();
      this.$emit('update:dialogVisible', false);
    },
    cancelPhoneEdit () {
      this.form.phoneNumber = this.phoneNumber;
      this.isEditPhone = false;
    },
    savePhoneEdit () {
      if (this.phoneNumber === this.form.phoneNumber || this.form.phoneNumber == Number(this.form.phoneNumber)) {
        this.isEditPhone = false;
        this.showError = false;
      } else {
        this.showError = true;
      }
    },
    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.message);
                this.btnLoading = false;
              }
            })
            .catch(err => {
              this.btnLoading = false;
              checkStatus(err);
            });
        } else {
          return false;
        }
      });
    },
  },
};

</script>
<style lang="less">
// 去除dailog-footer上边框
.customer-dialog .el-dialog__footer {
  padding-top: 0;
  padding-bottom: 20px;
  border-top: none !important;
}
.customer-dialog .el-dialog__body {
  padding-bottom: 0;
}
</style>
