<template>
  <div class="dialog-box" style="position: relative">
    <el-scrollbar class="custom-scroll content-box">
      <el-form
        ref="editFormProjectGroupPurchaseDetailRef"
        :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="TourProjectGroupPurchaseDetail.adultPrice">
              <el-input-number
                class="input-item"
                v-model="formData.TourProjectGroupPurchaseDetail.adultPrice"
                placeholder=""
                :clearable="true"
                :min="0"
                :step="1"
                :precision="2"
                :controls="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="儿童价格" prop="TourProjectGroupPurchaseDetail.childrenPrice">
              <el-input-number
                class="input-item"
                v-model="formData.TourProjectGroupPurchaseDetail.childrenPrice"
                placeholder=""
                :clearable="true"
                :min="0"
                :step="1"
                :precision="2"
                :controls="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="人数(下限包含)" prop="TourProjectGroupPurchaseDetail.minCount" width="180px">
              <el-input-number
                class="input-item"
                v-model="formData.TourProjectGroupPurchaseDetail.minCount"
                placeholder=""
                :clearable="true"
                :min="0"
                :step="1"
                :precision="0"
                :controls="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="人数(上限包含)" prop="TourProjectGroupPurchaseDetail.maxCount">
              <el-input-number
                class="input-item"
                v-model="formData.TourProjectGroupPurchaseDetail.maxCount"
                placeholder=""
                :clearable="true"
                :min="0"
                :step="1"
                :precision="0"
                :controls="true"
              />
            </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="onSaveClick()">保存</el-button>
    </el-row>
  </div>
</template>

<script lang="ts">
export default {
  name: 'editFormProjectGroupPurchaseDetail',
};
</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, stringCase } from '@/common/utils';
import { TourProjectGroupPurchaseData } from '@/api/generated/tourProjectGroupPurchaseController';
import { TourProjectGroupPurchaseDetailData } from '@/api/generated/tourProjectGroupPurchaseDetailController';
import { TourProjectGroupPurchaseController, TourProjectGroupPurchaseDetailController } from '@/api/generated';

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;
    groupPurchaseCode?: ANY_OBJECT;
    id?: ANY_OBJECT;
    saveOnSubmit?: boolean;
    rowData?: ANY_OBJECT;
    // 当使用Dialog.show弹出组件时，须定义该prop属性，以便对dialog进行回调
    dialog?: DialogProp<ANY_OBJECT[]>;
  }>(),
  {
    subPage: 0,
    groupPurchaseCode: undefined,
    id: undefined,
    saveOnSubmit: true,
    rowData: undefined,
    dialog: undefined,
  },
);

const editFormProjectGroupPurchaseDetailRef = ref();
// 表单数据定义
type EditFormProjectGroupPurchaseDetailData = {
  TourProjectGroupPurchaseDetail: TourProjectGroupPurchaseDetailData;
};
// 表单数据
const formData = reactive<EditFormProjectGroupPurchaseDetailData>({
  TourProjectGroupPurchaseDetail: {
    // 主键id
    id: undefined,
    // 关联 拼团设置的id
    groupPurchaseCode: undefined,
    // 创建用户
    createUserId: undefined,
    // 创建时间
    createTime: undefined,
    // 更新用户
    updateUserId: undefined,
    // 更新时间
    updateTime: undefined,
    // 删除标记(1: 正常 -1: 已删除)
    dataState: undefined,
    // 成人价格
    adultPrice: undefined,
    // 儿童价格
    childrenPrice: undefined,
    // 阶梯的人数(下限)
    minCount: undefined,
    // 阶梯的人数(上限)
    maxCount: undefined,
  },
},
);
// 表单验证规则
const rules = reactive({
  'TourProjectGroupPurchaseDetail.maxCount': [
    {required: true, message: '请输入阶梯的人数(上限)', trigger: 'blur'}
  ],
  'TourProjectGroupPurchaseDetail.minCount': [
    {required: true, message: '请输入阶梯的人数(下限)', trigger: 'blur'}
  ],
  'TourProjectGroupPurchaseDetail.childrenPrice': [
  {required: true, message: '请输入儿童价格', trigger: 'blur'}
  ],
  'TourProjectGroupPurchaseDetail.adultPrice': [
    {required: true, message: '请输入成人价格', trigger: 'blur'}
  ],
});

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

const isEdit = computed(() => {
  return props.saveOnSubmit ? props.id != null : props.rowData != null;
});

// 初始化页面数据
const loadTourProjectGroupPurchaseDetailData = () => {
  return new Promise<void>((resolve, reject) => {
    if (!isEdit.value) {
      resolve();
      return;
    }
    if (!props.saveOnSubmit && props.rowData != null) {
      formData.TourProjectGroupPurchaseDetail = JSON.parse(JSON.stringify(props.rowData));
      resolve();
      return;
    }
    let params: ANY_OBJECT = {
      id: props.id
    };
    TourProjectGroupPurchaseDetailController.view(params).then(res => {
      formData.TourProjectGroupPurchaseDetail = { ...res.data };
      resolve();
    }).catch(e => {
      reject(e);
    });
  });
};
const onUploadError = () => {
  ElMessage.error('文件上传失败');
};
const onUploadLimit = () => {
  ElMessage.error('已经超出最大上传个数限制');
};
const refreshEditFormProjectGroupPurchaseDetail = () => {
  // 刷新段落
};
/**
 * 重置过滤值
 */
const resetEditFormProjectGroupPurchaseDetail = () => {
  refreshEditFormProjectGroupPurchaseDetail();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetEditFormProjectGroupPurchaseDetail();
};
/**
 * 保存
 */
const onSaveClick = () => {
  editFormProjectGroupPurchaseDetailRef.value.validate((valid) => {
    if (!valid) return;
    //判断人数上下限是否合理
    if (formData.TourProjectGroupPurchaseDetail.maxCount < formData.TourProjectGroupPurchaseDetail.minCount) {
      ElMessage.error('阶梯的人数(上限)不能小于阶梯的人数(下限)');
      return;
    }
    // 级联操作
    if (!props.saveOnSubmit) {
      let retFormData = {
        ...formData.TourProjectGroupPurchaseDetail
      };
  props.dialog?.submit(retFormData);
      return;
    }
    let params: ANY_OBJECT = {
      tourProjectGroupPurchaseDetailDto: {
        id: formData.TourProjectGroupPurchaseDetail.id,
        groupPurchaseCode: props.groupPurchaseCode,
        createUserId: formData.TourProjectGroupPurchaseDetail.createUserId,
        createTime: formData.TourProjectGroupPurchaseDetail.createTime,
        updateUserId: formData.TourProjectGroupPurchaseDetail.updateUserId,
        updateTime: formData.TourProjectGroupPurchaseDetail.updateTime,
        adultPrice: formData.TourProjectGroupPurchaseDetail.adultPrice,
        childrenPrice: formData.TourProjectGroupPurchaseDetail.childrenPrice,
        minCount: formData.TourProjectGroupPurchaseDetail.minCount,
        maxCount: formData.TourProjectGroupPurchaseDetail.maxCount,
      }
    };

    let httpCall = isEdit.value ? TourProjectGroupPurchaseDetailController.update : TourProjectGroupPurchaseDetailController.add;
    httpCall(params).then(res => {
      ElMessage.success('保存成功');
      props.dialog?.submit();
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
  });
};
const formInit = () => {
  loadTourProjectGroupPurchaseDetailData().then(res => {
    if (isEdit.value) refreshEditFormProjectGroupPurchaseDetail();
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};

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