<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    :width="500"
    :visible="visible"
    :confirm-loading="loading"
    :maskClosable="false"
    :maxable="maxable"
    :title="isUpdate ? '发货' : '发货'"
    :body-style="{ paddingBottom: '24px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form>
      <a-form-item label="审核状态">
        <a-radio-group
          v-model:value="form.auditStatus"
        >
          <a-radio :value="20">审核通过</a-radio>
          <a-radio :value="30">驳回</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="返还押金" v-if="form.auditStatus === 20" >
          <a-input-number :min="0" :max="9999999" v-model:value="form.refundMoney"/>
          <span style="marginleft: 10px"> 元</span>
      </a-form-item>
      <a-alert v-if="form.auditStatus === 20" message="审核通过后，订单将变更为已退租状态，并且电池设备自动解绑。" type="warning" />
      <a-form-item label="驳回原因" v-if="form.auditStatus === 30">
        <a-textarea
          :rows="4"
          :maxlength="200"
          placeholder="仅在驳回时填写"
          v-model:value="form.refuseDesc"
        />
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
import {ref, reactive, watch, computed} from 'vue';
  import { Form, message } from 'ant-design-vue';
  import { useUserStore } from '@/store/modules/user';
import { OrderRefund } from "@/api/order/refund/model";
import { updateOrderRefund } from "@/api/order/refund";
import { assignObject } from "ele-admin-pro";

  const userStore = useUserStore();
  // 当前用户信息
  const loginUser = computed(() => userStore.info ?? {});
  // 是否是修改
  const isUpdate = ref(false);
  const useForm = Form.useForm;
  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 修改回显的数据
    data?: OrderRefund;
  }>();

  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 提交状态
  const loading = ref(false);
  // 是否显示最大化切换按钮
  const maxable = ref(true);

  // 用户信息
  const form = reactive<OrderRefund>({
    orderRefundId: undefined,
    auditStatus: 20,
    orderId: undefined,
    refuseDesc: '',
    tenantId: undefined
  });

  // 已上传数据, 可赋初始值用于回显
  const images = ref(<any>[]);

  /* 更新visible */
  const updateVisible = (value: boolean) => {
    emit('update:visible', value);
  };

  // 表单验证规则
  const rules = reactive({
    equipmentCode: [
      {
        required: true,
        type: 'string',
        message: '请输入设备编码',
        trigger: 'blur'
      }
    ]
  });

  const { resetFields, validate, validateInfos } = useForm(form, rules);

  /* 保存编辑 */
  const save = () => {
    validate()
      .then(() => {
        loading.value = true;
        // 去除空格
        const data = {
          ...form,
          orderRefundId: props.data?.orderRefundId,
          orderId: props.data?.orderId,
          tenantId: props.data?.tenantId
        };
        // 转字符串
        updateOrderRefund(data)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            emit('done');
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        if (props.data) {
          loading.value = false;
          if(props.data.auditStatus != 10){
            assignObject(form, props.data);
          }
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }
    }
  );
</script>
<style lang="less"></style>
