<template>
  <div class="dialog-box" style="position: relative">
    <el-scrollbar class="custom-scroll content-box">
      <el-form
        ref="formEditTourOrderRef"
        :model="formData"
        :size="layoutStore.defaultFormItemSize"
        :rules="rules"
        label-width="120px"
        label-position="right"
        @submit.prevent
      >
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="最高返利金额" prop="tourOrder.rebateSumPrice">
              <el-input-number
                class="input-item"
                v-model="formData.tourOrder.rebateSumPrice"
                placeholder=""
                :clearable="true"
                :step="1"
                :controls="true"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="核销返利金额" prop="tourOrder.rebatePrice">
              <el-input-number
                class="input-item"
                v-model="formData.tourOrder.rebatePrice"
                placeholder=""
                :clearable="true"
                :step="1"
                :controls="true"
                :min="0"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-scrollbar>
    <el-row class="footer-box" type="flex" justify="end" align="middle">
      <el-button :size="layoutStore.defaultFormItemSize" @click="onCancel()">取消</el-button>
      <el-button :size="layoutStore.defaultFormItemSize" type="primary" @click="onRebate()">核销</el-button>
    </el-row>
  </div>
</template>

<script lang="ts">
export default {
  name: 'formEditTourOrderRebate',
};
</script>

<script setup lang="ts">
import { DialogProp } from '@/components/Dialog/types';
import { VxeColumn, VxeTable } from 'vxe-table';
import { ANY_OBJECT } from '@/types/generic';
import { DictData, DictionaryBase } from '@/common/staticDict/types';
import { ElMessage, ElMessageBox, UploadFile } from 'element-plus';
import { useRoute, useRouter } from 'vue-router';
import { useCommon } from '@/common/hooks/useCommon';
import { useLayoutStore, useStaticDictStore } from '@/store';
import { useDownload } from '@/common/hooks/useDownload';
import { useDropdown } from '@/common/hooks/useDropdown';
import { DropdownOptions, ListData } from '@/common/types/list';
import { useTable } from '@/common/hooks/useTable';
import { TableOptions } from '@/common/types/pagination';
import { useUpload } from '@/common/hooks/useUpload';
import { useUploadWidget } from '@/common/hooks/useUploadWidget';
import { DictionaryController } from '@/api/system';
import { treeDataTranslate, findItemFromList, findTreeNodePath, findTreeNode } from '@/common/utils';
import { TourismProjectData } from '@/api/generated/tourismProjectController';
import { TourOrderPassenageData } from '@/api/generated/tourOrderPassenageController';
import { TourOrderData } from '@/api/generated/tourOrderController';
import { TourismProjectController, TourOrderPassenageController, tourOrderController } from '@/api/generated';
import FormSelectTourProject from '@/pages/tourOrder/formSelectTourProject/index.vue';

const router = useRouter();
const route = useRoute();
const layoutStore = useLayoutStore();
const { downloadFile } = useDownload();
const { getUploadHeaders, getUploadActionUrl, fileListToJson, parseUploadData, getPictureList } = useUpload();
const { 
  Delete,
  Search,
  Edit,
  Plus,
  Refresh,
  Picture,
  Dialog,
  mainContextHeight,
  clientHeight,
  checkPermCodeExist,
  parseParams,
  parseArrayParams,
  formatDateByStatsType,
  getDateRangeFilter,
} = useCommon();
// 静态字典
const { staticDict: StaticDict } = useStaticDictStore();

const props = withDefaults(
  defineProps<{
    subPage?: number | string | boolean;
    id?: ANY_OBJECT;
    saveOnSubmit?: boolean;
    rowData?: ANY_OBJECT;
    // 当使用Dialog.show弹出组件时，须定义该prop属性，以便对dialog进行回调
    dialog?: DialogProp<ANY_OBJECT[]>;
  }>(),
  {
    subPage: 0,
    id: undefined,
    saveOnSubmit: true,
    rowData: undefined,
    dialog: undefined,
  },
);

const formEditTourOrderRef = ref();
// 表单数据定义
type FormEditTourOrderData = {
  tourOrder: TourOrderData;
};
// 表单数据
const formData = reactive<FormEditTourOrderData>({
  tourOrder: {
    // ID主键
    id: undefined,
    // 订单号
    orderNo: undefined,
    // 出发日期
    departureDate: undefined,
    // 结束日期
    endDate: undefined,
    // 成人数量
    adultNumber: undefined,
    // 儿童数量
    childrenNumber: undefined,
    // 总天数
    countTimes: undefined,
    // 币种
    currency: undefined,
    // 项目标题
    projectTitle: undefined,
    // 订单总金额
    totalAmount: undefined,
    // 儿童价格
    childrenPrice: undefined,
    // 成人价格
    adultPrice: undefined,
    // 房间数量
    roomNumber: undefined,
    // 车辆信息
    carId: undefined,
    // 车辆金额
    carAmount: undefined,
    // 预定日期
    orderDate: undefined,
    // 订单状态
    orderStatus: undefined,
    // 旅游项目
    projectId: undefined,
    // 项目简述
    projectResume: undefined,
    // 顾客姓名
    customerName: undefined,
    // 顾客手机号
    customerMobile: undefined,
    // 顾客微信号
    customerWechat: undefined,
    // 顾客备用手机号
    customerMobileStandby: undefined,
    // 创建用户
    createUserId: undefined,
    // 创建时间
    createTime: undefined,
    // 更新用户
    updateUserId: undefined,
    // 更新时间
    updateTime: undefined,
    // 删除标记(1: 正常 -1: 已删除)
    dataState: undefined,
    //核销金额
    rebatePrice: undefined,
    // 返利金额
    rebateSumPrice: undefined,
  },
},
);
// 表单验证规则
const rules = reactive({
  'tourOrder.rebatePrice': [
    {required: true, message: '请输入核销的金额', trigger: 'blur'}
  ],
});

const onCancel = () => {
  if (props.dialog) {
    props.dialog.cancel();
  }
};

// 初始化页面数据
const loadtourOrderData = () => {
  return new Promise<void>((resolve, reject) => {
    let params: ANY_OBJECT = {
      id: props.id
    };
    tourOrderController.view(params).then(res => {
      formData.tourOrder = { ...res.data };
      resolve();
    }).catch(e => {
      reject(e);
    });
  });
};
const refreshFormEditTourOrder = () => {
  // 刷新段落
};
/**
 * 重置过滤值
 */
const resetFormEditTourOrder = () => {
  refreshFormEditTourOrder();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetFormEditTourOrder();
};
/**
 * 核销
 */
const onRebate = () => {
  formEditTourOrderRef.value.validate((valid) => {
    if (!valid) return;
    // 级联操作
    if (!props.saveOnSubmit) {
      let formData = {
        ...formData.tourOrder
      };
  props.dialog?.submit(formData);
      return;
    }
    let params: ANY_OBJECT = {
      tourOrderDto: {
        id: formData.tourOrder.id,
        rebatePrice: formData.tourOrder.rebatePrice,
      }
    };

    tourOrderController.clearRebate(params).then(res => {
      ElMessage.success('核销成功');
      props.dialog?.submit();
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
  });
};
const formInit = () => {
  loadtourOrderData().then(res => {
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};

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