<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="1150" @ok="handleSubmit">
    <BasicForm @register="registerForm" name="SeparateAccountsForm">
      <template #SeparateAccounts="{ model, feild }">
        <FormItem>
          <InputNumber
            addonBefore="平台"
            addonAfter="%"
            v-model:value="model['ptSeparateAccounts']"
            class="mr-5"
            :precision="2"
            :max="ptSeparateAccountsMax"
            :min="1"
            @change="handleShChange"
            placeholder="请输入平台分账比例"
          ></InputNumber>
          <InputNumber
            addonBefore="商户"
            addonAfter="%"
            v-model:value="model['shSeparateAccounts']"
            class="mr-5"
            :precision="2"
            :max="shSeparateAccountsMax"
            :min="1"
            placeholder="请输入商户分账比例"
            @change="handleShChange"
          ></InputNumber>
          <InputNumber
            addonBefore="门店"
            disabled
            placeholder="自动计算"
            v-model:value="model['mdSeparateAccounts']"
            :precision="2"
            :min="0"
          ></InputNumber>
        </FormItem>
      </template>
    </BasicForm>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { InputNumber, FormItem } from 'ant-design-vue';
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema } from '../SeparateAccounts.data';
  import { saveOrUpdate } from '../SeparateAccounts.api';
  import { useMessage } from '/@/hooks/web/useMessage';
  const { createMessage } = useMessage();
  // Emits声明
  const emit = defineEmits(['register', 'success']);
  const isUpdate = ref(true);
  const isDetail = ref(false);
  const ptSeparateAccountsMax = ref(100);
  const shSeparateAccountsMax = ref(100);

  //表单配置
  const [registerForm, { setProps, resetFields, setFieldsValue, validate, scrollToField, getFieldsValue }] = useForm({
    labelWidth: 150,
    schemas: formSchema,
    showActionButtonGroup: false,
    baseColProps: { span: 24 },
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: !!data?.showFooter, showOkBtn: !!data?.showFooter });
    isUpdate.value = !!data?.isUpdate;
    isDetail.value = !!data?.showFooter;
    if (unref(isUpdate)) {
      //表单赋值
      await setFieldsValue({
        ...data.data,
      });
    }
    // 隐藏底部时禁用整个表单
    setProps({ disabled: !data?.showFooter });
  });
  //设置标题
  const title = computed(() => (!unref(isUpdate) ? '添加分账规则' : !unref(isDetail) ? '分账规则详情' : '编辑分账规则'));
  //表单提交事件
  async function handleSubmit(v) {
    try {
      let values = await validate();
      setModalProps({ confirmLoading: true });
      //提交表单
      await saveOrUpdate(values, isUpdate.value);
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success');
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
  function handleShChange() {
    const field = getFieldsValue();
    if (field.ptSeparateAccounts) {
      shSeparateAccountsMax.value = 100 - field.ptSeparateAccounts;
    }
    if (field.shSeparateAccounts) {
      ptSeparateAccountsMax.value = 100 - field.shSeparateAccounts;
    }
    if (field.shSeparateAccounts || field.ptSeparateAccounts) {
      const pt = Number(field.ptSeparateAccounts) || 0;
      const sh = Number(field.shSeparateAccounts) || 0;
      setFieldsValue({
        mdSeparateAccounts: 100 - pt - sh,
      });
    }
  }
</script>

<style lang="less" scoped>
  /** 时间和数字输入框样式 */
  :deep(.ant-input-number) {
    width: 100%;
  }

  :deep(.ant-calendar-picker) {
    width: 100%;
  }
</style>
