<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" v-if="!isView">
          <el-input v-model="form.model.user_id" placeholder="请填写用户ID"></el-input>
        </el-form-item>
        <el-form-item label="用户信息" v-if="isView">
          <div>
            <p><strong>用户名：</strong>{{ form.model.username || '-' }}</p>
            <p><strong>手机号：</strong>{{ form.model.mobile || '-' }}</p>
          </div>
        </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="type">
          <el-select v-model="form.model.type" placeholder="请选择佣金类型" :disabled="isView">
            <el-option label="订单佣金" value="order"></el-option>
            <el-option label="推荐佣金" value="referral"></el-option>
            <el-option label="团队佣金" value="team"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="来源订单" prop="source_order_no">
          <el-input
            v-model="form.model.source_order_no"
            placeholder="请填写来源订单号"
            :disabled="isView"
          ></el-input>
        </el-form-item>
        <el-form-item label="佣金比例" prop="commission_rate">
          <el-input-number
            v-model="form.model.commission_rate"
            :min="0"
            :max="100"
            :precision="2"
            placeholder="佣金比例"
            :disabled="isView"
          />
          <span style="margin-left: 10px">%</span>
        </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="settled"></el-option>
            <el-option label="已冻结" value="frozen"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="获得时间" v-if="isView">
          <span>{{ form.model.create_time || '-' }}</span>
        </el-form-item>
        <el-form-item label="结算时间" v-if="isView && form.model.settle_time">
          <span>{{ form.model.settle_time || '-' }}</span>
        </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">保存</el-button>
      <el-button v-if="modal.params.type == 'edit'" type="primary" @click="confirm">更新</el-button>
    </el-footer>
  </el-container>
</template>
<script setup>
  import { cloneDeep } from 'lodash';
  import { onMounted, reactive, ref, unref, computed } from 'vue';
  import { api } from '../finance.service';
  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 { error, data } = await api.commission.detail(id);
    error === 0 && (form.model = data);
    loading.value = false;
  }
  // 表单关闭时提交
  async function confirm() {
    unref(formRef).validate(async (valid) => {
      if (!valid) return;
      let submitForm = cloneDeep(form.model);
      const { error } =
        props.modal.params.type == 'add'
          ? await api.commission.add(submitForm)
          : await api.commission.edit(props.modal.params.id, submitForm);
      if (error == 0) {
        emit('modalCallBack', { event: 'confirm' });
      }
    });
  }
  async function init() {
    if (props.modal.params.id) {
      await getDetail(props.modal.params.id);
    }
  }
  onMounted(() => {
    init();
  });
</script>
