<template>
  <div class="add-other-recharge-form">
    <el-dialog
      :title="$t('pages.addOtherRecharge.dialog.title', { origin: sysOrigin })"
      :visible="true"
      width="450px"
      :before-close="handleClose"
      :close-on-click-modal="false"
      :modal-append-to-body="true"
      :append-to-body="true"
    >
      <el-alert
        type="info"
        :closable="false"
        style="margin:0px 0px 20px 0px;"
        :description="$t('pages.addOtherRecharge.description.warning')"
      />
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="formData"
        label-width="50px"
        style="width: 350px; margin:auto;"
      >
        <el-form-item :label="$t('pages.addOtherRecharge.form.type')" prop="paymentType">
          <el-select v-model="formData.paymentType" style="width: 100%;">
            <el-option
              v-for="(item, index) in otherPaymentTypes"
              :key="index"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('pages.addOtherRecharge.form.amount')" prop="amount">
          <el-input
            v-model.trim="formData.amount"
            :placeholder="$t('pages.addOtherRecharge.placeholder.amount')"
          />
        </el-form-item>
        <el-form-item :label="$t('pages.addOtherRecharge.form.time')" prop="paymentTime">
          <el-date-picker
            v-model="formData.paymentDateNumber"
            style="width: 100%;"
            value-format="yyyyMMdd"
            type="date"
            :placeholder="$t('pages.addOtherRecharge.placeholder.paymentDate')"
          />
        </el-form-item>
        <el-form-item :label="$t('pages.addOtherRecharge.form.remarks')" prop="remarks">
          <el-input
            v-model.trim="formData.remarks"
            type="textarea"
            :rows="3"
            resize="none"
            maxlength="150"
            :placeholder="$t('pages.addOtherRecharge.placeholder.remarks')"
          />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose()">
          {{ $t('pages.addOtherRecharge.action.cancel') }}
        </el-button>
        <el-button
          v-loading="listLoading"
          type="primary"
          @click="handleSubmit()"
        >
          {{ $t('pages.addOtherRecharge.action.submit') }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { orderOtherRechargeProcess } from '@/api/purchase'
import { otherPaymentTypes } from '@/constant/type'
export default {
  name: 'AddOtherRecharge',
  props: {
    userId: {
      type: String,
      required: true
    },
    sysOrigin: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      otherPaymentTypes,
      listLoading: false,
      formData: {
        paymentType: '',
        userId: '',
        amount: '',
        paymentTime: '',
        remarks: ''
      }
    }
  },
  computed: {
    rules() {
      return {
        paymentType: [{
          required: true,
          message: this.$t('pages.addOtherRecharge.validation.requiredField'),
          trigger: 'blur'
        }],
        amount: [
          {
            required: true,
            message: this.$t('pages.addOtherRecharge.validation.requiredField'),
            trigger: 'blur'
          },
          {
            pattern: /^\d{1,7}(\.\d{0,2})?$/,
            message: this.$t('pages.addOtherRecharge.validation.amountRange'),
            trigger: 'blur'
          }
        ],
        paymentDateNumber: [
          {
            required: true,
            message: this.$t('pages.addOtherRecharge.validation.requiredField'),
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    handleSubmit() {
      const that = this
      that.$refs.dataForm.validate((valid) => {
        if (!valid) {
          console.error('error submit!!')
          return false
        }
        if (!that.userId) {
          that.$opsMessage.fail(that.$t('pages.addOtherRecharge.message.invalidUser'))
          return
        }

        if (!that.sysOrigin) {
          that.$opsMessage.fail(that.$t('pages.addOtherRecharge.message.invalidSystem'))
          return
        }
        that.formData.userId = that.userId
        that.formData.sysOrigin = that.sysOrigin
        that.listLoading = true
        orderOtherRechargeProcess(that.formData).then(res => {
          that.$emit('success', Object.assign({}, that.formData))
          that.handleClose()
        }).catch(err => {
          that.listLoading = false
          that.$emit('fial', err)
          that.handleClose()
        })
      })
    },
    handleClose() {
      this.$emit('close')
    }
  }
}
</script>
<style lang="scss">
.add-other-recharge-form {
  .el-dialog__body {
    padding: 20px;
  }
}
</style>
