<template>
  <el-container>
    <el-main>
      <el-form :model="form.model" :rules="form.rules" ref="formRef" label-width="100px">
        <el-form-item label="用户ID" prop="user_id">
          <el-input v-model="form.model.user_id" placeholder="请填写用户ID" :disabled="isView"></el-input>
        </el-form-item>
        <el-form-item label="充值金额" prop="amount">
          <el-input-number v-model="form.model.amount" :min="0" :precision="2" placeholder="充值金额" :disabled="isView" />
          <span style="margin-left: 10px">৳</span>
        </el-form-item>
        <el-form-item label="支付方式" prop="payment_method">
          <el-select v-model="form.model.payment_method" placeholder="请选择支付方式" :disabled="isView">
            <el-option label="支付宝" value="alipay"></el-option>
            <el-option label="微信支付" value="wechat"></el-option>
            <el-option label="银行卡" value="bank"></el-option>
            <el-option label="余额" value="balance"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单号" prop="order_no">
          <el-input v-model="form.model.order_no" placeholder="请填写订单号" :disabled="isView"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.model.status" placeholder="请选择状态" :disabled="isView">
            <el-option label="待处理" value="pending"></el-option>
            <el-option label="已完成" value="completed"></el-option>
            <el-option label="已失败" value="failed"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.model.remark" type="textarea" :rows="3" placeholder="请填写备注"
            :disabled="isView"></el-input>
        </el-form-item>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit" v-if="!isView">
      <el-button v-if="modal.params.type == 'add'" type="primary" @click="confirm">{{ t('common.save') }}</el-button>
      <el-button v-if="modal.params.type == 'edit'" type="primary" @click="confirm">{{ t('common.update') }}</el-button>
    </el-footer>
  </el-container>
</template>
<script setup>
import { cloneDeep } from 'lodash';
import { onMounted, reactive, ref, unref, computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { api } from '../finance.service';

const { t } = useI18n();
const emit = defineEmits(['modalCallBack']);
const props = defineProps({
  modal: {
    type: Object,
  },
});

const isView = computed(() => props.modal.params.type === 'view');

// 添加 编辑 form
let formRef = ref(null);
const form = reactive({
  model: {
    user_id: '',
    amount: 0,
    payment_method: 'alipay',
    order_no: '',
    status: 'pending',
    remark: '',
  },
  rules: {
    user_id: [{ required: true, message: '请填写用户ID', trigger: 'blur' }],
    amount: [{ required: true, message: '请填写充值金额', trigger: 'blur' }],
    payment_method: [{ required: true, message: '请选择支付方式', trigger: 'change' }],
    order_no: [{ required: true, message: '请填写订单号', trigger: 'blur' }],
    status: [{ required: true, message: '请选择状态', trigger: 'change' }],
  },
});
const loading = ref(false);
// 获取详情
async function getDetail(id) {
  loading.value = true;
  const { code, data } = await api.recharge.detail(id);
  code == '200' && (form.model = data);
  loading.value = false;
}
// 表单关闭时提交
async function confirm() {
  unref(formRef).validate(async (valid) => {
    if (!valid) return;
    let submitForm = cloneDeep(form.model);
    const { code } =
      props.modal.params.type == 'add'
        ? await api.recharge.add(submitForm)
        : await api.recharge.edit(props.modal.params.id, submitForm);
    if (code == '200') {
      emit('modalCallBack', { event: 'confirm' });
    }
  });
}
async function init() {
  if (props.modal.params.id) {
    await getDetail(props.modal.params.id);
  }
}
onMounted(() => {
  init();
});
</script>
