<template>
  <div class="p-2">
    <el-card shadow="never" style="margin-top: 0">
      <approvalButton
        @submitForm="submitForm"
        @approvalVerifyOpen="approvalVerifyOpen"
        @handleApprovalRecord="handleApprovalRecord"
        :buttonLoading="buttonLoading"
        :id="form.acceptanceId"
        :status="form.flowStatus"
        :pageType="routeParams.type"
        :mode="false"
      />
    </el-card>

    <el-card shadow="never" style="margin-top: 0">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="验收确认编号" prop="acceptanceCode">
              <el-input v-model="form.acceptanceCode" placeholder="由系统自动生成" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目编号" prop="projectCode">
              <el-input v-model="form.projectCode" placeholder="自动带出" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="form.projectName" placeholder="请选择项目" readonly :disabled="isFormDisabled">
              <template #suffix>
                <el-icon style="cursor: pointer" @click="openProjectSelect">
                  <Search />
                </el-icon>
              </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目经理" prop="managerId">
              <el-select v-model="form.managerId" placeholder="由项目自动带出" disabled style="width: 100%">
                <el-option v-for="user in userList" :key="user.userId" :label="user.nickName" :value="user.userId" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="验收日期" prop="acceptanceDate">
              <el-date-picker
                v-model="form.acceptanceDate"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择验收日期"
                :disabled="isFormDisabled"
                :clearable="false"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="验收单附件" prop="ossId">
              <FileUpload
                v-model="ossIdString"
                :limit="5"
                :fileSize="20"
                :fileType="['doc', 'docx', 'pdf', 'xls', 'xlsx', 'png', 'jpg', 'jpeg']"
                :disabled="isFormDisabled"
                :isShowTip="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部门负责人" prop="chargeId">
              <el-select v-model="form.chargeId" placeholder="由项目自动带出" disabled style="width: 100%">
                <el-option v-for="user in userList" :key="user.userId" :label="user.nickName" :value="user.userId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分管副总" prop="deputyId">
              <el-select v-model="form.deputyId" placeholder="由项目自动带出" disabled style="width: 100%">
                <el-option v-for="user in userList" :key="user.userId" :label="user.nickName" :value="user.userId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                :rows="3"
                placeholder="确认该项目达到客户验收节点，可以进行验收。"
                :disabled="isFormDisabled"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <ApprovalRecord ref="approvalRecordRef" />
    <SubmitVerify ref="submitVerifyRef" @submit-callback="submitCallback" />
    <!-- 项目选择弹窗 -->
    <ProjectSelect ref="projectSelectRef" :multiple="false" @confirm-call-back="projectInfoSelectCallBack" />
  </div>
</template>

<script setup lang="ts" name="ProjectAcceptanceEdit">
import ApprovalButton from '@/components/Process/approvalButton.vue';
import ApprovalRecord from '@/components/Process/approvalRecord.vue';
import FileUpload from '@/components/FileUpload/index.vue';
import ProjectSelect from '@/components/ProjectSelect/index.vue';
import { useRoute, useRouter } from 'vue-router';
import { useUserStore } from '@/store/modules/user';
import type { ProjectInfoVO } from '@/api/oa/erp/projectInfo/types';
import {
  addProjectAcceptance,
  getProjectAcceptance,
  prepareProjectAcceptanceByProjectId,
  submitProjectAcceptanceAndFlowStart,
  updateProjectAcceptance
} from '@/api/oa/erp/projectAcceptance';
import type { ProjectAcceptanceForm } from '@/api/oa/erp/projectAcceptance/types';
import { FlowCodeEnum } from '@/enums/OAEnum';
import SubmitVerify from '@/components/Process/submitVerify.vue';
import { getUserList } from '@/api/system/user';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const route = useRoute();
const router = useRouter();

// 路由参数（与项目信息保持一致）
const routeParams = ref<Record<string, any>>({});

const userStore = useUserStore();
const refreshFlagKey = 'projectAcceptanceListShouldRefresh';

/** 通知列表页刷新 */
const notifyListRefresh = () => {
  sessionStorage.setItem(refreshFlagKey, Date.now().toString());
};
const isSuperAdmin = computed(() => {
  const roles = userStore.roles || [];
  return roles.includes('admin') || roles.includes('superadmin');
});

const buttonLoading = ref(false);
const formRef = ref<ElFormInstance>();
const approvalRecordRef = ref<InstanceType<typeof ApprovalRecord>>();
const submitVerifyRef = ref<InstanceType<typeof SubmitVerify>>();

const projectSelectRef = ref<InstanceType<typeof ProjectSelect>>();

const initFormData: ProjectAcceptanceForm = {
  acceptanceId: undefined,
  projectId: undefined,
  projectCode: undefined,
  projectName: undefined,
  managerId: undefined,
  projectManagerName: undefined,
  acceptanceDate: undefined,
  ossId: undefined,
  chargeId: undefined,
  chargeName: undefined as any,
  deputyId: undefined,
  deputyName: undefined as any,
  remark: undefined,
  acceptanceStatus: '1',
  flowStatus: 'draft' as any,
  flowCode: FlowCodeEnum.PROJECT_ACCEPTANCE_CODE,
  variables: {},
  bizExt: {}
};

const form = ref<ProjectAcceptanceForm>({ ...initFormData });

/** 用户下拉（与项目信息 devEdit 一致） */
const userList = ref<any[]>([]);
const getUserSelectList = async () => {
  const res = await getUserList({} as any);
  userList.value = res.data || [];
};

const rules = {
  projectName: [{ required: true, message: '项目名称不能为空', trigger: 'blur' }],
  projectCode: [{ required: true, message: '项目号不能为空', trigger: 'blur' }],
  acceptanceDate: [{ required: true, message: '验收日期不能为空', trigger: 'change' }],
  ossId: [
    {
      required: true,
      validator: (_rule: any, _value: any, callback: (e?: Error) => void) => {
        const v = form.value.ossId;
        if (v === undefined || v === null || String(v).trim() === '') {
          callback(new Error('请上传验收单附件'));
        } else {
          callback();
        }
      },
      trigger: 'change'
    }
  ]
} as any;

/** 验收日期设为当天（yyyy-MM-dd） */
const setAcceptanceDateToday = () => {
  const d = new Date();
  form.value.acceptanceDate = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}` as any;
};

const ossIdString = computed({
  get() {
    const v = form.value.ossId as any;
    return v === undefined || v === null ? '' : String(v);
  },
  set(val: string) {
    form.value.ossId = val || (undefined as any);
  }
});

/** 打开项目选择弹窗 */
const openProjectSelect = () => {
  projectSelectRef.value?.open();
};

/** 项目选择回调，自动填充项目相关信息 */
const projectInfoSelectCallBack = async (data: ProjectInfoVO[]) => {
  if (data && data.length > 0) {
    const project = data[0];
    form.value.projectId = project.projectId;
    form.value.projectCode = project.projectCode;
    form.value.projectName = project.projectName;
    form.value.managerId = project.managerId;
    form.value.chargeId = project.chargeId;
    form.value.deputyId = project.deputyId;
  }
};

const onProjectChange = async (val: any) => {
  if (!val) return;
  const { data } = await prepareProjectAcceptanceByProjectId(val);
  Object.assign(form.value, data);
  form.value.projectId = val;
};

const loadDetail = async () => {
  if (!routeParams.value.id) return;
  const res = await getProjectAcceptance(routeParams.value.id);
  Object.assign(form.value, res.data);
};

const submitForm = (status: string, mode: boolean) => {
  formRef.value?.validate(async (valid: boolean) => {
    if (!valid) return;
    // 前端权限校验：只有项目经理才能暂存或提交（超级管理员除外）
    if (!form.value.managerId) {
      proxy?.$modal.msgError('请先选择项目');
      buttonLoading.value = false;
      return;
    }
    if (!isSuperAdmin.value && userStore.userId !== form.value.managerId) {
      proxy?.$modal.msgError('只有项目经理才能提交或暂存项目验收确认');
      buttonLoading.value = false;
      return;
    }
    buttonLoading.value = true;
    try {
      // 提交审批（与项目信息保持一致）
      if (status !== 'draft') {
        // 提交流程：设置业务状态为审批中，流程状态为 waiting
        form.value.acceptanceStatus = '2';
        form.value.flowStatus = 'waiting' as any;
        form.value.flowCode = FlowCodeEnum.PROJECT_ACCEPTANCE_CODE; //OAPA
        form.value.variables = {
          projectId: form.value.projectId,
          projectCode: form.value.projectCode,
          projectName: form.value.projectName,
          managerId: form.value.managerId,
          managerName: form.value.projectManagerName,
          chargeId: form.value.chargeId,
          deputyId: form.value.deputyId
        };
        form.value.bizExt = {
          businessCode: form.value.acceptanceCode,
          businessTitle: form.value.projectName + '验收确认'
        };
        await submitProjectAcceptanceAndFlowStart(form.value);
        proxy?.$modal.msgSuccess('操作成功');
        notifyListRefresh();
        proxy?.$tab.closePage();
        router.go(-1);
      } else {
        // 暂存：显式设置流程状态为 draft
        form.value.flowStatus = 'draft' as any;
        form.value.acceptanceStatus = '1'; // 业务状态：暂存
        if (form.value.acceptanceId) {
          await updateProjectAcceptance(form.value);
        } else {
          await addProjectAcceptance(form.value);
        }
        proxy?.$modal.msgSuccess('暂存成功');
        notifyListRefresh();
        proxy?.$tab.closePage();
        router.go(-1);
      }
    } finally {
      buttonLoading.value = false;
    }
  });
};

const submitCallback = async () => {
  await proxy?.$tab.closePage(route);
  router.go(-1);
};

const approvalVerifyOpen = async () => {
  await submitVerifyRef.value?.openDialog(routeParams.value.taskId);
};

const handleApprovalRecord = () => {
  if (form.value.acceptanceId) {
    approvalRecordRef.value?.init(form.value.acceptanceId);
  }
};

/** 表单是否禁用：查看/审批模式，或非暂存业务状态 */
const isFormDisabled = computed(() => {
  if (routeParams.value.type === 'view' || routeParams.value.type === 'approval') {
    return true;
  }
  // 仅暂存（acceptanceStatus === '1'）可编辑，其余只读
  if (!form.value.acceptanceStatus) {
    return false;
  }
  return form.value.acceptanceStatus !== '1';
});

const loadFormData = async () => {
  // 获取路由参数（与项目信息保持一致）
  routeParams.value = route.query;
  await getUserSelectList();
  // 重置表单
  form.value = { ...initFormData };

  await loadDetail();

  if (routeParams.value.type === 'add') {
    setAcceptanceDateToday();
    // 如果传递了projectId参数，自动加载项目信息
    const projectId = routeParams.value.projectId;
    if (projectId) {
      await onProjectChange(projectId);
      setAcceptanceDateToday();
    }
  }
};

watch(
  () => route.fullPath,
  () => {
    loadFormData();
  }
);

onMounted(async () => {
  proxy?.$modal.loading('正在加载数据，请稍后...');
  try {
    await loadFormData();
  } finally {
    proxy?.$modal.closeLoading();
  }
});
</script>
