<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label=" 提供商" prop="provider">
        <el-input v-model="dataForm.provider" placeholder=" 提供商：阿里/腾讯/网易/百度/"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="续费方式" prop="renewalType">
        <el-select v-model="dataForm.renewalType" placeholder="请选择续费方式" style="width: 240px">
          <el-option v-for="item in renewalType" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="续费日" prop="renewalDate">
        <el-input-number v-model="dataForm.renewalDate" :min="1" :max="29" placeholder="具体续费日期 (1-29)" style="width: 100%" />
      </el-form-item>
      <el-form-item label="到期时间" prop="expireTime">
        <el-date-picker v-model="dataForm.expireTime" type="datetime" placeholder="选择到期时间" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
      </el-form-item>
      <el-form-item label="其他信息" prop="otherInfo">
        <el-input v-model="dataForm.otherInfo" placeholder="其他信息json形式"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, computed } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
import { useAppStore } from "@/store";
import { getDictDataList } from "@/utils/utils";

const store = useAppStore();
const renewalType = getDictDataList(store.state.dicts, "renewal_type");

const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  provider: "",
  name: "",
  remark: "",
  otherInfo: "",
  renewalType: "",
  renewalDate: null as number | null,
  expireTime: "",

  createDate: "",
  createUser: "",
  updateDate: "",
  updateUser: ""
});

const getRules = () => ({
  provider: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  name: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  remark: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  renewalType: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  renewalDate:
    dataForm.renewalType !== "1"
      ? [
          { required: true, message: "续费日不能为空", trigger: "blur" },
          { type: "number", min: 1, max: 29, message: "日期必须在1到29之间", trigger: "blur" }
        ]
      : [{ type: "number", min: 1, max: 29, message: "日期必须在1到29之间", trigger: "blur" }],
  expireTime: [{ required: true, message: "到期时间不能为空", trigger: "blur" }]
});

// 使用 computed 属性，使其响应式
const rules = computed(() => getRules());

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/baitutools/dlrenewalremind/" + id).then((res) => {
    const { data } = res; // 解构出 data 对象

    // --- 新增处理逻辑 ---
    if (data.hasOwnProperty("renewalDate")) {
      if (typeof data.renewalDate === "string") {
        const parsed = parseInt(data.renewalDate, 10);
        data.renewalDate = isNaN(parsed) ? null : parsed;
      } else if (typeof data.renewalDate === "number") {
        data.renewalDate = data.renewalDate
      } else {
        data.renewalDate = null;
      }
    }

    // 将处理后的 data 对象合并到 dataForm
    Object.assign(dataForm, data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/baitutools/dlrenewalremind", dataForm).then((res) => {
      ElMessage.success({
        message: "成功",
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
