<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="120px">
            <el-row :gutter="10" class="form-row">
              <el-col :span="12">
                <el-form-item label="项目名称" prop="projectName">
                  <el-input v-model="queryParams.projectName" placeholder="请点击右侧图标选择项目">
                    <template #suffix>
                      <el-icon style="cursor: pointer; margin-right: 4px; font-size: 14px" @click="openProjectSelect">
                        <Search />
                      </el-icon>
                    </template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="项目编号" prop="projectCode">
                  <el-input v-model="queryParams.projectCode" placeholder="请输入项目编号" clearable />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="10" class="form-row">
              <el-col :span="12">
                <el-form-item label="项目经理" prop="managerName">
                  <el-input v-model="queryParams.managerName" placeholder="请输入项目经理" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="当前里程碑" prop="milestonePlan">
                  <el-input v-model="queryParams.milestonePlan" placeholder="请输入当前里程碑" clearable />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="10" class="form-row">
              <el-col :span="12">
                <el-form-item label="部门名称" prop="deptName">
                  <el-input v-model="queryParams.deptName" placeholder="请输入部门名称" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="部门负责人" prop="chargeName">
                  <el-input v-model="queryParams.chargeName" placeholder="请输入部门负责人" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="12" v-show="false">
                <el-form-item label="项目ID" prop="projectId">
                  <el-input v-model="queryParams.projectId" placeholder="请点击右侧图标选择项目" />
                </el-form-item>
              </el-col>
              <el-col :span="12" v-show="false">
                <el-form-item label="项目经理ID" prop="managerId">
                  <el-input v-model="queryParams.managerId" placeholder="请点击右侧图标选择项目" />
                </el-form-item>
              </el-col>
              <el-col :span="12" v-show="false">
                <el-form-item label="部门ID" prop="deptId">
                  <el-input v-model="queryParams.deptId" placeholder="请点击右侧图标选择项目" />
                </el-form-item>
              </el-col>
              <el-col :span="12" v-show="false">
                <el-form-item label="部门负责人ID" prop="chargeId">
                  <el-input v-model="queryParams.chargeId" placeholder="请点击右侧图标选择项目" />
                </el-form-item>
              </el-col>
              <el-col :span="12" v-show="false">
                <el-form-item label="分管副总ID" prop="deputyId">
                  <el-input v-model="queryParams.deputyId" placeholder="请点击右侧图标选择项目" />
                </el-form-item>
              </el-col>
              <el-col :span="12" v-show="false">
                <el-form-item label="分管副总" prop="deputyName">
                  <el-input v-model="queryParams.deputyName" placeholder="请点击右侧图标选择项目" />
                </el-form-item>
              </el-col>
              <el-col :span="12" v-show="false">
                <el-form-item label="附件ID" prop="ossId">
                  <el-input v-model="queryParams.ossId" placeholder="请点击右侧图标选择项目" />
                </el-form-item>
              </el-col>
              <el-col :span="12" v-show="false">
                <el-form-item label="排序号" prop="sortOrder">
                  <el-input v-model="queryParams.sortOrder" placeholder="请点击右侧图标选择项目" />
                </el-form-item>
              </el-col>
              <el-col :span="12" v-show="false">
                <el-form-item label="激活标识" prop="activeFlag">
                  <el-input v-model="queryParams.activeFlag" placeholder="请点击右侧图标选择项目" />
                </el-form-item>
              </el-col>
              <el-col :span="12" v-show="false">
                <el-form-item label="周报说明" prop="informationNote">
                  <el-input v-model="queryParams.informationNote" placeholder="请点击右侧图标选择项目" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="10" class="form-row">
              <el-col :span="24">
                <el-form-item>
                  <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                  <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </div>
    </transition>

    <ProjectSelect ref="projectSelectRef" :multiple="false" @confirm-call-back="projectInfoSelectCallBack"></ProjectSelect>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['oa/erp:projectReport:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['oa/erp:projectReport:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['oa/erp:projectReport:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['oa/erp:projectReport:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" :columns="columns" :search="true" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>
      <!-- 项目周报信息表格展示 -->
      <el-table v-loading="loading" border :data="projectReportList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="项目周报ID" align="center" prop="reportId" v-if="columns[0].visible" />
        <el-table-column label="项目ID" align="center" prop="projectId" v-if="columns[2].visible" />
        <el-table-column label="项目名称" align="center" prop="projectName" v-if="columns[3].visible" />
        <el-table-column label="项目编码" align="center" prop="projectCode" v-if="columns[20].visible" />
        <el-table-column label="当前里程碑" align="center" prop="milestonePlan" v-if="columns[4].visible" />
        <el-table-column label="项目经理" align="center" prop="managerName" v-if="columns[5].visible" />
        <el-table-column label="部门ID" align="center" prop="deptName" v-if="columns[6].visible" />
        <el-table-column label="部门负责人" align="center" prop="chargeName" v-if="columns[7].visible" />
        <el-table-column label="分管副总" align="center" prop="deputyName" v-if="columns[8].visible" />
        <el-table-column label="周报情况说明" align="center" prop="informationNote" v-if="columns[9].visible" />
        <el-table-column label="排序号" align="center" prop="sortOrder" v-if="columns[10].visible" />
        <el-table-column label="附件ID" align="center" prop="ossId" v-if="columns[11].visible" />
        <el-table-column label="备注" align="center" prop="remark" v-if="columns[12].visible" />
        <el-table-column label="激活标识" align="center" prop="activeFlag" v-if="columns[13].visible" />
        <!-- <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[20].visible" /> -->
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['oa:projectReport:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['oa:projectReport:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改项目周报信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="projectReportFormRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目编号" prop="projectCode">
          <el-input v-model="form.projectCode" placeholder="请输入项目编号" />
        </el-form-item>
        <el-form-item label="当前里程碑" prop="milestonePlan">
          <el-input v-model="form.milestonePlan" placeholder="请输入当前里程碑" />
        </el-form-item>
        <el-form-item label="项目经理" prop="managerId">
          <el-input v-model="form.managerId" placeholder="请输入项目经理" />
        </el-form-item>
        <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门ID" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="chargeId">
          <el-input v-model="form.chargeId" placeholder="请输入部门负责人" />
        </el-form-item>
        <el-form-item label="分管副总" prop="deputyId">
          <el-input v-model="form.deputyId" placeholder="请输入分管副总" />
        </el-form-item>
        <el-form-item label="周报情况说明" prop="informationNote">
          <el-input v-model="form.informationNote" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="附件ID" prop="ossId">
          <el-input v-model="form.ossId" placeholder="请输入附件ID" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ProjectReport" lang="ts">
import { ProjectReportVO, ProjectReportQuery, ProjectReportForm } from '@/api/oa/erp/projectReport/types';
import { listProjectReport, getProjectReport, delProjectReport, addProjectReport, updateProjectReport } from '@/api/oa/erp/projectReport';
import ProjectSelect from '@/components/ProjectSelect/index.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const projectReportList = ref<ProjectReportVO[]>([]);
const route = useRoute();
const router = useRouter();
const buttonLoading = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
// 表单引用
const queryFormRef = ref<ElFormInstance>();
// 对话框引用
const projectReportFormRef = ref<ElFormInstance>();
// 根据项目名称选择项目组件引用
const projectSelectRef = ref<InstanceType<typeof ProjectSelect>>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
// 列显隐信息
const columns = ref<FieldOption[]>([
  { key: 0, label: `项目周报ID`, visible: false },
  { key: 1, label: `租户编号`, visible: false },
  { key: 2, label: `项目ID`, visible: false },
  { key: 3, label: `项目名称`, visible: true },
  { key: 4, label: `当前里程碑`, visible: true },
  { key: 5, label: `项目经理`, visible: true },
  { key: 6, label: `部门ID`, visible: true },
  { key: 7, label: `部门负责人`, visible: true },
  { key: 8, label: `分管副总`, visible: true },
  { key: 9, label: `周报情况说明`, visible: true },
  { key: 10, label: `排序号`, visible: true },
  { key: 11, label: `附件ID`, visible: true },
  { key: 12, label: `备注`, visible: true },
  { key: 13, label: `激活标识`, visible: true },
  { key: 14, label: `删除标志`, visible: true },
  { key: 15, label: `创建部门`, visible: true },
  { key: 16, label: `创建人`, visible: true },
  { key: 17, label: `创建时间`, visible: true },
  { key: 18, label: `更新人`, visible: true },
  { key: 19, label: `更新时间`, visible: true },
  { key: 20, label: `项目编码`, visible: true }
]);
const initFormData: ProjectReportForm = {
  reportId: undefined,
  projectId: undefined,
  projectName: undefined,
  projectCode: undefined,
  milestonePlan: undefined,
  managerId: undefined,
  deptId: undefined,
  chargeId: undefined,
  deputyId: undefined,
  informationNote: undefined,
  sortOrder: undefined,
  ossId: undefined,
  remark: undefined,
  activeFlag: undefined,
  deptName: undefined,
  managerName: undefined,
  chargeName: undefined,
  deputyName: undefined
};
const data = reactive<PageData<ProjectReportForm, ProjectReportQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: undefined,
    projectName: undefined,
    projectCode: undefined,
    milestonePlan: undefined,
    managerId: undefined,
    deptId: undefined,
    chargeId: undefined,
    deputyId: undefined,
    informationNote: undefined,
    sortOrder: undefined,
    ossId: undefined,
    activeFlag: undefined,
    deptName: undefined,
    managerName: undefined,
    chargeName: undefined,
    deputyName: undefined,
    params: {}
  },
  rules: {
    projectName: [{ required: true, message: '项目名称不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

// 打开项目选择框
const openProjectSelect = () => {
  projectSelectRef.value.open();
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
/** 根据部门查询项目周报信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listProjectReport(queryParams.value);
  projectReportList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

//根据项目名称回调项目信息
const projectInfoSelectCallBack = (data: any) => {
  if (data.length > 0) {
    setFromProjectInfo(data);
    getList();
  } else {
    queryFormRef.value?.resetFields();
    proxy?.$modal.msgWarning('请选择项目信息');
    getList();
  }
};
// 根据项目信息设置表格内容
const setFromProjectInfo = (data: any) => {
  const projectInfoList = data;
  queryParams.value.projectId = projectInfoList[0].projectId;
  queryParams.value.projectName = projectInfoList[0].projectName;
  queryParams.value.projectCode = projectInfoList[0].projectCode;
  queryParams.value.managerId = projectInfoList[0].managerId;
  queryParams.value.milestonePlan = projectInfoList[0].milestonePlan;
  queryParams.value.deptId = projectInfoList[0].deptId;
  queryParams.value.chargeId = projectInfoList[0].chargeId;
  queryParams.value.deputyId = projectInfoList[0].deputyId;
  queryParams.value.managerName = projectInfoList[0].managerName;
  queryParams.value.deptName = projectInfoList[0].deptName;
  queryParams.value.chargeName = projectInfoList[0].chargeName;
  queryParams.value.deputyName = projectInfoList[0].deputyName;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 所有表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  projectReportFormRef.value?.resetFields();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: ProjectReportVO[]) => {
  ids.value = selection.map((item) => item.reportId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  proxy.$tab.closePage(route);
  // 构建路由参数，包含所有需要传递的项目信息
  const routeParams = {
    type: 'add',
    projectId: queryParams.value.projectId,
    projectName: queryParams.value.projectName,
    projectCode: queryParams.value.projectCode,
    managerId: queryParams.value.managerId,
    managerName: queryParams.value.managerName,
    deptId: queryParams.value.deptId,
    deptName: queryParams.value.deptName,
    chargeId: queryParams.value.chargeId,
    chargeName: queryParams.value.chargeName,
    deputyId: queryParams.value.deputyId,
    deputyName: queryParams.value.deputyName
  };
  router.push({
    path: '/project/projectReport/edit',
    query: routeParams
  });
};

/** 修改按钮操作 */
const handleUpdate = async (row?: ProjectReportVO) => {
  reset();
  const _reportId = row?.reportId || ids.value[0];
  const res = await getProjectReport(_reportId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改项目周报信息';
};

/** 提交按钮 */
const submitForm = () => {
  projectReportFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.reportId) {
        await updateProjectReport(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addProjectReport(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: ProjectReportVO) => {
  const _reportIds = row?.reportId || ids.value;
  await proxy?.$modal.confirm('是否确认删除项目周报信息编号为"' + _reportIds + '"的数据项？').finally(() => (loading.value = false));
  await delProjectReport(_reportIds);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'oa/erp/projectReport/export',
    {
      ...queryParams.value
    },
    `projectReport_${new Date().getTime()}.xlsx`
  );
};

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