<template>
  <el-container>
    <el-main>
      <el-form :model="form.model" :rules="form.rules" ref="formRef" :label-width="formLabelWidth">
        <el-form-item :label="t('modules.commission.userId')" prop="user_id" v-if="!isView">
          <el-input v-model="form.model.user_id" :placeholder="t('modules.commission.enterUserId')"></el-input>
        </el-form-item>
        <el-form-item :label="t('modules.commission.userInfo')" v-if="isView">
          <div>
            <p><strong>{{ t('modules.commission.userName') }}：</strong>{{ form.model.username || '-' }}</p>
            <p><strong>{{ t('modules.commission.phoneNo') }}：</strong>{{ form.model.mobile || '-' }}</p>
          </div>
        </el-form-item>
        <el-form-item :label="t('modules.commission.commissionAmount')" prop="amount">
          <el-input-number v-model="form.model.amount" :min="0" :precision="2"
            :placeholder="t('modules.commission.commissionAmount')" :disabled="isView" />
          <span style="margin-left: 10px">৳</span>
        </el-form-item>
        <el-form-item :label="t('modules.commission.commissionType')" prop="type">
          <el-select v-model="form.model.type" :placeholder="t('modules.commission.selectCommissionType')"
            :disabled="isView">
            <el-option :label="t('modules.commission.orderCommission')" value="order"></el-option>
            <el-option :label="t('modules.commission.referralCommission')" value="referral"></el-option>
            <el-option :label="t('modules.commission.teamCommission')" value="team"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="t('modules.commission.sourceOrder')" prop="source_order_no">
          <el-input v-model="form.model.source_order_no" :placeholder="t('modules.commission.enterSourceOrderNo')"
            :disabled="isView"></el-input>
        </el-form-item>
        <el-form-item :label="t('modules.commission.commissionRate')" prop="commission_rate">
          <el-input-number v-model="form.model.commission_rate" :min="0" :max="100" :precision="2"
            :placeholder="t('modules.commission.commissionRate')" :disabled="isView" />
          <span style="margin-left: 10px">%</span>
        </el-form-item>
        <el-form-item :label="t('common.status')" prop="status">
          <el-select v-model="form.model.status" :placeholder="t('modules.commission.selectStatus')" :disabled="isView">
            <el-option :label="t('modules.commission.unsettled')" value="pending"></el-option>
            <el-option :label="t('modules.commission.settled')" value="settled"></el-option>
            <el-option :label="t('modules.commission.frozen')" value="frozen"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="t('modules.commission.createTime')" v-if="isView">
          <span>{{ form.model.create_time || '-' }}</span>
        </el-form-item>
        <el-form-item :label="t('modules.commission.settleTime')" v-if="isView && form.model.settle_time">
          <span>{{ form.model.settle_time || '-' }}</span>
        </el-form-item>
        <el-form-item :label="t('modules.commission.remark')" prop="remark">
          <el-input v-model="form.model.remark" type="textarea" :rows="3"
            :placeholder="t('modules.commission.enterRemark')" :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 { useFormConfig } from '@/hooks/useFormConfig';
import { api } from '../finance.service';

const { t } = useI18n();

// 使用表单配置hooks
const { formLabelWidth } = useFormConfig({ enWidth: '140px' });
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: '',
    username: '',
    mobile: '',
    amount: 0,
    type: 'order',
    source_order_no: '',
    commission_rate: 0,
    status: 'pending',
    create_time: '',
    settle_time: '',
    remark: '',
  },
  rules: {
    user_id: [{ required: true, message: '请填写用户ID', trigger: 'blur' }],
    amount: [{ required: true, message: '请填写佣金金额', trigger: 'blur' }],
    type: [{ required: true, message: '请选择佣金类型', trigger: 'change' }],
    commission_rate: [{ 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.commission.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.commission.add(submitForm)
        : await api.commission.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>
