<template>
  <div class="pay">
    <div class="title">
      <span>{{ $t('curriculumVitae:pay:title') }}</span>
      <el-button
        type="text"
        class="editBtn"
        :disabled="!reviewerId"
        @click.stop="openEdit"
      >
        {{ $t('common:button:edit') }}
      </el-button>
    </div>
    <div class="message">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item
          :label="$t('curriculumVitae:pay:form:payCardId')"
          style="width: 45%"
        >
          {{ DATA.BankNum }}
        </el-form-item>
        <el-form-item
          :label="$t('curriculumVitae:pay:form:username')"
          style="width: 45%"
        >
          {{ DATA.BankName }}
        </el-form-item>
        <el-form-item
          :label="$t('curriculumVitae:pay:form:bank')"
          style="width: 45%"
        >
          {{ DATA.OpeningBank }}
        </el-form-item>
        <el-form-item
          :label="$t('curriculumVitae:pay:form:idCard')"
          style="width: 45%"
        >
          {{ DATA.IdCard }}
        </el-form-item>
        <el-form-item
          :label="$t('curriculumVitae:pay:form:phone')"
          style="width: 45%"
        >
          {{ DATA.BankPhoneNum }}
        </el-form-item>
      </el-form>
    </div>
    <base-model :config="model_cfg">
      <template slot="dialog-body">
        <el-form
          ref="payFrom"
          v-loading="loading"
          :model="form"
          :rules="rules"
          label-width="100px"
          size="small"
          :inline="true"
          class="demo-form-inline"
        >
          <el-form-item
            style="width: 45%"
            :label="$t('curriculumVitae:pay:form:payCardId')"
            prop="BankNum"
          >
            <el-input
              clearable
              placeholder=""
              v-model="form.BankNum"
              :maxlength="400"
            ></el-input>
          </el-form-item>
          <el-form-item
            style="width: 45%"
            :label="$t('curriculumVitae:pay:form:username')"
            prop="BankName"
          >
            <el-input
              clearable
              placeholder=""
              v-model="form.BankName"
              :maxlength="400"
            ></el-input>
          </el-form-item>
          <el-form-item
            style="width: 45%"
            :label="$t('curriculumVitae:pay:form:bank')"
            prop="OpeningBank"
          >
            <el-input
              clearable
              placeholder=""
              v-model="form.OpeningBank"
              :maxlength="400"
            ></el-input>
          </el-form-item>
          <el-form-item
            style="width: 45%"
            :label="$t('curriculumVitae:pay:form:idCard')"
            prop="IdCard"
          >
            <el-input
              clearable
              placeholder=""
              v-model="form.IdCard"
              :maxlength="400"
            ></el-input>
          </el-form-item>
          <el-form-item
            style="width: 45%"
            :label="$t('curriculumVitae:pay:form:phone')"
            prop="BankPhoneNum"
          >
            <el-input
              clearable
              placeholder=""
              v-model="form.BankPhoneNum"
              :maxlength="400"
            ></el-input>
          </el-form-item>
        </el-form>
      </template>
      <template slot="dialog-footer">
        <el-button size="small" type="primary" @click="handleCancle">
          {{ $t('common:button:cancel') }}
        </el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          @click="handleSave"
        >
          {{ $t('common:button:save') }}
        </el-button>
      </template>
    </base-model>
  </div>
</template>
<script>
import BaseModel from '@/components/BaseModel'
import { updatePaymentMode } from '@/api/reviewers'
const defaultForm = () => {
  return {
    BankNum: '',
    BankName: '',
    OpeningBank: '',
    IdCard: '',
    BankPhoneNum: '',
  }
}
export default {
  name: 'pay',
  components: { BaseModel },
  props: {
    DATA: {
      type: Object,
      required: true,
      default: () => {
        return {}
      },
    },
    reviewerId: {
      type: String,
      default: '',
    },
    isEN: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      model_cfg: {
        visible: false,
        showClose: true,
        width: '800px',
        title: this.$t('curriculumVitae:pay:form:title'),
        appendToBody: true,
      },
      form: defaultForm(),
      rules: {
        BankNum: [
          {
            validator: (rule, value, callback) => {
              let reg = new RegExp(/^[A-Za-z0-9]+$/, 'ig')
              if (value && !reg.test(value)) {
                callback(new Error(this.$t('common:ruleMessage:pattern')))
              } else {
                callback()
              }
            },
            trigger: 'blur',
          },
          {
            max: 400,
            message: this.$t('form:rules:maxLength:400'),
            trigger: 'blur',
          },
        ],
        BankName: [
          {
            max: 400,
            message: this.$t('form:rules:maxLength:400'),
            trigger: 'blur',
          },
        ],
        OpeningBank: [
          {
            max: 400,
            message: this.$t('form:rules:maxLength:400'),
            trigger: 'blur',
          },
        ],
        IdCard: [
          {
            validator: (rule, value, callback) => {
              let reg = new RegExp(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/, 'ig')
              if (value && !reg.test(value)) {
                callback(new Error(this.$t('common:ruleMessage:pattern')))
              } else {
                callback()
              }
            },
            trigger: 'blur',
          },
          {
            max: 400,
            message: this.$t('form:rules:maxLength:400'),
            trigger: 'blur',
          },
        ],
        BankPhoneNum: [
          {
            validator: (rule, value, callback) => {
              let reg = new RegExp(/^\d{11}$/, 'i')
              if (value && !reg.test(value)) {
                callback(new Error(this.$t('common:ruleMessage:pattern')))
              } else {
                callback()
              }
            },
            trigger: 'blur',
          },
          {
            max: 400,
            message: this.$t('form:rules:maxLength:400'),
            trigger: 'blur',
          },
        ],
      },
      loading: false,
    }
  },
  methods: {
    openEdit() {
      this.form = defaultForm()
      Object.keys(this.form).forEach((key) => {
        if (this.DATA[key]) {
          this.form[key] = this.DATA[key]
        }
      })
      this.model_cfg.visible = true
    },
    handleCancle() {
      this.form = defaultForm()
      this.model_cfg.visible = false
    },
    async handleSave() {
      try {
        let validate = await this.$refs.payFrom.validate()
        if (!validate) return false
        if (this.reviewerId) {
          this.form.Id = this.reviewerId
        }
        this.loading = true
        let res = await updatePaymentMode(this.form)
        this.loading = false
        if (res.IsSuccess) {
          this.$emit('getInfo')
          this.model_cfg.visible = false
        }
      } catch (err) {
        this.loading = false
        console.log(err)
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.pay {
  min-height: 100px;
  .title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
    font-weight: bold;
  }
  .message {
    margin: auto;
    min-height: 100px;
    background-color: #eee;
    padding: 10px;
    line-height: 30px;
    border-radius: 5px;
  }
  ::v-deep .el-form-item__label {
    color: #000;
  }
}
</style>