<template>
  <el-container>
    <el-main v-loading="loading" :element-loading-text="t('common.loading')">
      <el-form :model="form.model" :rules="form.rules" ref="formRef" :label-width="formLabelWidth">
        <el-form-item :label="t('modules.payment.channelName')" prop="channelName">
          <el-input v-model="form.model.channelName" :placeholder="t('modules.payment.enterChannelName')"
            maxlength="100" show-word-limit />
        </el-form-item>

        <el-form-item :label="t('modules.payment.merchantId')" prop="merchantNum">
          <el-input v-model="form.model.merchantNum" :placeholder="t('modules.payment.enterMerchantId')" maxlength="100"
            show-word-limit />
        </el-form-item>

        <el-form-item label="APPID" prop="appid">
          <el-input v-model="form.model.appid" :placeholder="t('modules.payment.enterAppId')" maxlength="200"
            show-word-limit />
        </el-form-item>

        <el-form-item :label="t('modules.payment.secretKey')" prop="secretKey">
          <el-input v-model="form.model.secretKey" type="textarea" :rows="4"
            :placeholder="t('modules.payment.enterSecretKey')" maxlength="1000" show-word-limit />
        </el-form-item>
        <el-form-item label="异步通知地址" prop="notifyUrl">
          <el-input v-model="form.model.notifyUrl" type="textarea" :rows="2" placeholder="请输入异步通知地址" maxlength="500"
            show-word-limit />
        </el-form-item>

        <el-divider content-position="left">Bkash通道代码</el-divider>

        <el-form-item label="Bkash支付编码" prop="bkashPayCode">
          <el-input v-model="form.model.bkashPayCode" placeholder="请输入Bkash支付编码" maxlength="100" show-word-limit />
        </el-form-item>

        <el-form-item label="Bkash提现编码" prop="bkashWithdrawCode">
          <el-input v-model="form.model.bkashWithdrawCode" placeholder="请输入Bkash提现编码" maxlength="100" show-word-limit />
        </el-form-item>

        <el-divider content-position="left">Nagad通道代码</el-divider>

        <el-form-item label="Nagad支付编码" prop="nagadPayCode">
          <el-input v-model="form.model.nagadPayCode" placeholder="请输入Nagad支付编码" maxlength="100" show-word-limit />
        </el-form-item>

        <el-form-item label="Nagad提现编码" prop="nagadWithdrawCode">
          <el-input v-model="form.model.nagadWithdrawCode" placeholder="请输入Nagad提现编码" maxlength="100" show-word-limit />
        </el-form-item>

        <el-divider content-position="left">Rocket通道代码</el-divider>

        <el-form-item label="Rocket支付编码" prop="rocketPayCode">
          <el-input v-model="form.model.rocketPayCode" placeholder="请输入Rocket支付编码" maxlength="100" show-word-limit />
        </el-form-item>

        <el-form-item label="Rocket提现编码" prop="rocketWithdrawCode">
          <el-input v-model="form.model.rocketWithdrawCode" placeholder="请输入Rocket提现编码" maxlength="100"
            show-word-limit />
        </el-form-item>

      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <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 { onMounted, reactive, ref, unref } from 'vue';
import { ElMessage } from 'element-plus';
import { useI18n } from 'vue-i18n';
import { useFormConfig } from '@/hooks/useFormConfig';
import api from '../../api';

const { t } = useI18n();

// 使用表单配置hooks
const { formLabelWidth } = useFormConfig({ enWidth: '140px' });

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

// 添加 编辑 form
let formRef = ref(null);
const loading = ref(false);

const form = reactive({
  model: {
    channelName: '',
    merchantNum: '',
    appid: '',
    secretKey: '',
    bkashPayCode: '',
    nagadPayCode: '',
    rocketPayCode: '',
    bkashWithdrawCode: '',
    nagadWithdrawCode: '',
    rocketWithdrawCode: '',
    notifyUrl: '',
  },
  rules: {
    channelName: [{ required: true, message: '请输入通道名称', trigger: 'blur' }],
    merchantNum: [{ required: true, message: '请输入商户号', trigger: 'blur' }],
    appid: [{ required: true, message: '请输入应用ID', trigger: 'blur' }],
    secretKey: [{ required: true, message: '请输入密钥', trigger: 'blur' }],
    // 新增字段都是可选的，不需要验证规则
  },
});

// 获取详情
async function getDetail(data) {
  loading.value = true;
  try {
    form.model.channelName = data.channelName || '';
    form.model.merchantNum = data.merchantNum || '';
    form.model.appid = data.appid || '';
    form.model.secretKey = data.secretKey || '';
    form.model.bkashPayCode = data.bkashPayCode || '';
    form.model.nagadPayCode = data.nagadPayCode || '';
    form.model.rocketPayCode = data.rocketPayCode || '';
    form.model.bkashWithdrawCode = data.bkashWithdrawCode || '';
    form.model.nagadWithdrawCode = data.nagadWithdrawCode || '';
    form.model.rocketWithdrawCode = data.rocketWithdrawCode || '';
    form.model.notifyUrl = data.notifyUrl || '';
  } finally {
    loading.value = false;
  }
}

// 表单关闭时提交
async function confirm() {
  unref(formRef).validate(async (valid) => {
    if (!valid) return;

    let submitForm = { ...form.model };

    if (props.modal.params.type == 'edit') {
      submitForm.id = props.modal.params.id;
    }

    const { code } =
      props.modal.params.type == 'add'
        ? await api.payment.channel.add(submitForm)
        : await api.payment.channel.update(submitForm);

    if (code == 200) {
      ElMessage.success(t('message.saveSuccess'));
      emit('modalCallBack', { event: 'confirm' });
    }
  });
}

async function init() {
  if (props.modal.params.type === 'edit' && props.modal.params.data) {
    await getDetail(props.modal.params.data);
  }
}

onMounted(() => {
  init();
});
</script>

<style lang="scss" scoped>
// .el-textarea {
//   :deep(.el-textarea__inner) {
//     font-family: 'Courier New', monospace;
//     font-size: 14px;
//   }
// }</style>
