<template>
  <div class="tab-content">
    <div class="tab-content">
      <el-form
        ref="lxApprovalFormRef"
        :model="formData"
        :rules="formRules"
        :validate-on-rule-change="false"
        :label-width="150"
        :disabled="!isBulu && (isPreview || Number(formData.projectLzType) >= 5)"
      >
        <el-collapse v-model="activeCollapse">
          <el-collapse-item
            title="项目投资总额（全口径）"
            name="项目投资总额（全口径）"
          >
            <el-table
              :data="tzzeqkjData"
              style="width: 100%"
              empty-text="暂无数据"
              default-expand-all
              row-key="xh"
              border
            >
              <el-table-column prop="xh" label="序号" />
              <el-table-column prop="zb" label="指标" />
              <el-table-column prop="dw" label="" width="70" />
              <el-table-column prop="rmbjj" label="人民币计价">
                <template #default="scope">
                  <el-input
                    v-model="scope.row.rmbjj"
                    :min="0"
                    :max="99999999999.99999999"
                    controls-position="right"
                  />
                </template>
              </el-table-column>
            </el-table>
          </el-collapse-item>
          <el-collapse-item name="财务评价指标">
            <template #title>
              财务评价指标
              <el-tooltip placement="top" effect="dark">
                <template #content>
                  <div style="max-width: 300px">
                    严格对照决策文件中的批复的指标进行填报，无需过多填报。
                    其中"社会投资人财务内部收益率"一般对应"能建方内部收益率"。
                  </div>
                </template>
                <el-icon
                  style="
                    margin-left: 4px;
                    vertical-align: middle;
                    cursor: pointer;
                  "
                >
                  <QuestionFilled />
                </el-icon>
              </el-tooltip>
            </template>
            <el-table
              :data="cwpjzbData"
              style="width: 100%"
              empty-text="暂无数据"
              border
            >
              <el-table-column prop="name" label="指标" />
              <el-table-column prop="unit" label="" width="70" />
              <el-table-column prop="tzpgjdpfz" label="投资评估阶段批复值">
                <template #default="scope">
                  <el-input
                    v-model="scope.row.num"
                    :min="0"
                    :max="99999999999.99999999"
                    controls-position="right"
                  />
                </template>
              </el-table-column>
            </el-table>
          </el-collapse-item>
          <el-collapse-item title="核心边界条件" name="核心边界条件">
            <div class="tab-handle">
              <el-button type="primary" size="small" @click="addHxbjtj"
                >新增</el-button
              >
            </div>
            <el-table
              :data="hxbjtjData"
              style="width: 100%"
              empty-text="暂无数据"
              border
            >
              <el-table-column type="index" width="50" />
              <el-table-column prop="type" label="业务类型" width="130">
                <template #default="scope">
                  <CommonSelector
                    v-model="scope.row.type"
                    dictName="hxbjtjywlx"
                  />
                </template>
              </el-table-column>
              <el-table-column label="单位工程造价">
                <el-table-column prop="dwgczjSz" label="数值" width="180">
                  <template #default="scope">
                    <el-input
                      v-model="scope.row.dwgczjSz"
                      :min="0"
                      :max="99999999999.99999999"
                      controls-position="right"
                    />
                  </template>
                </el-table-column>
                <el-table-column prop="dwgczjDw" label="单位" width="130">
                  <template #default="scope">
                    <CommonSelector
                      v-model="scope.row.dwgczjDw"
                      dictName="gczjdw"
                    />
                  </template>
                </el-table-column>
              </el-table-column>
              <el-table-column label="销售/服务能力">
                <el-table-column prop="xsfwnlSz" label="数值" width="180">
                  <template #default="scope">
                    <el-input
                      v-model="scope.row.xsfwnlSz"
                      :min="0"
                      :max="99999999999.99999999"
                      controls-position="right"
                    />
                  </template>
                </el-table-column>
                <el-table-column prop="xsfwnlDw" label="单位" width="60" />
              </el-table-column>
              <el-table-column label="年均负荷率">
                <el-table-column prop="njfhlSz" label="数值" width="180">
                  <template #default="scope">
                    <el-input
                      v-model="scope.row.njfhlSz"
                      :min="0"
                      :max="99999.99"
                      controls-position="right"
                    />
                  </template>
                </el-table-column>
                <el-table-column prop="njfhlDw" label="单位" width="60" />
              </el-table-column>
              <el-table-column label="单位生产/服务总成本">
                <el-table-column prop="dwscfwzcbSz" label="数值" width="180">
                  <template #default="scope">
                    <el-input
                      v-model="scope.row.dwscfwzcbSz"
                      :min="0"
                      :max="99999999999.99999999"
                      controls-position="right"
                    />
                  </template>
                </el-table-column>
                <el-table-column prop="dwscfwzcbDw" label="单位" width="60" />
              </el-table-column>
              <el-table-column label="单位售价（含税）">
                <el-table-column prop="dwsjSz" label="数值" width="180">
                  <template #default="scope">
                    <el-input
                      v-model="scope.row.dwsjSz"
                      :min="0"
                      :max="99999999999.99999999"
                      controls-position="right"
                    />
                  </template>
                </el-table-column>
                <el-table-column prop="dwsjDw" label="单位" width="90">
                  <template #default="scope">
                    <el-input v-model="scope.row.dwsjDw" />
                  </template>
                </el-table-column>
              </el-table-column>
              <el-table-column label="盈亏平衡点">
                <el-table-column prop="ykphdlx" label="类型" width="130">
                  <template #default="scope">
                    <CommonSelector
                      v-model="scope.row.ykphdlx"
                      dictName="ykphdlx"
                    />
                  </template>
                </el-table-column>
                <el-table-column prop="ykphdsz" label="数值" width="180">
                  <template #default="scope">
                    <el-input
                      v-model="scope.row.ykphdsz"
                      :min="0"
                      :max="99999999999.99999999"
                      controls-position="right"
                    />
                  </template>
                </el-table-column>
                <el-table-column prop="ykphddw" label="单位" width="60" />
              </el-table-column>
              <el-table-column
                prop="bcsm"
                label="核心边界条件补充说明"
                width="180"
              >
                <template #default="scope">
                  <el-input v-model="scope.row.bcsm" type="textarea" />
                </template>
              </el-table-column>
              <el-table-column label="操作" width="60" fixed="right">
                <template #default="scope">
                  <el-button
                    link
                    type="danger"
                    size="small"
                    @click="deleteHxbjtj(scope.$index)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-collapse-item>
          <el-collapse-item title="项目立项信息" name="项目立项信息">
            <el-row :gutter="20">
              <el-col :span="24">
                <el-form-item label="其他会议审议情况" prop="qthysyqk">
                  <el-input v-model="formData.qthysyqk" type="textarea" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="立项最终审批意见" prop="lxzzpfyj">
                  <CommonSelector v-model="formData.lxzzpfyj" dictName="spyj" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="项目立项批复时间" prop="lxxmpfsj">
                  <el-date-picker
                    v-model="formData.lxxmpfsj"
                    type="date"
                    placeholder="请选择"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="立项项目批复文号" prop="lxxmpfwh">
                  <el-input v-model="formData.lxxmpfwh" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="审核处室" prop="jcpfshcs">
                  <el-input v-model="formData.jcpfshcs" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="未通过主要原因与要求" prop="wtgzyyyyyq">
                  <el-input v-model="formData.wtgzyyyyyq" type="textarea" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="立项批复文件" :label-width="100">
                  <FileUploader v-model="lxpfwjData" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>
          <el-collapse-item title="审批意见" name="审批意见">
            <FormDynamicTable
              :modelValue="spyjTableData"
              @update:modelValue="updateSpyjTable"
              :columns="spyjColumns"
            />
            <el-form-item label="相关上会材料" :label-width="100">
              <FileUploader v-model="shclData" />
            </el-form-item>
          </el-collapse-item>
        </el-collapse>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { inject, ref } from "vue";
import CommonSelector from "@/components/CommonSelector.vue";
import FormDynamicTable from "@/components/FormDynamicTable/index.vue";
import FileUploader from "@/components/FileUploader/index.vue";

const lxApprovalFormRef = ref();

const requiredInput = (msg) => ({
  required: true,
  message: `请输入${msg}`,
  trigger: "blur",
});
const requiredSelect = (msg) => ({
  required: true,
  message: `请选择${msg}`,
  trigger: [],
});

const formRules = {
  qthysyqk: [requiredInput("其他会议审议情况")],
  lxzzpfyj: [requiredSelect("立项最终审批意见")],
  lxxmpfsj: [requiredSelect("项目立项批复时间")],
  lxxmpfwh: [requiredInput("立项项目批复文号")],
  jcpfshcs: [requiredInput("审核处室")],
  wtgzyyyyyq: [requiredInput("未通过主要原因与要求")],
};

const validate = () => {
  return lxApprovalFormRef.value?.validate();
};

defineExpose({ validate });

const {
  formData,
  isPreview,
  isBulu,
  formDisabled,
  activeCollapse,
  tzzeqkjData,
  cwpjzbData,
  hxbjtjData,
  lxpfwjData,
  lxpfSelectIds,
  spyjTableData,
  shclData,
  shclSelectIds,
  downloadFile,
  ElMessageBox,
  addHxbjtj,
  deleteHxbjtj,
  addLxpfFile,
  deleteLxpf,
  lxpfSelectionChange,
  multiDeleteLxpf,
  addShclFile,
  deleteShcl,
  shclSelectionChange,
  multiDeleteShcl,
  windowConfig,
  token,
} = inject("lxShared");

// 审批意见表格列配置
const spyjColumns = [
  {
    prop: "lx",
    label: "类型",
    type: "select",
    optionKey: "yjlx",
    minWidth: 130,
  },
  { prop: "pfyj", label: "批复意见", type: "textarea" },
  { prop: "pfyjsj", label: "批复意见时间", type: "date" },
];

// 更新审批意见表格数据（适配 reactive 数组与 FormDynamicTable 的 v-model 机制）
const updateSpyjTable = (val) => {
  spyjTableData.splice(0, spyjTableData.length, ...val);
};
</script>

<style scoped>
.tab-content :deep(.el-form-item__error) {
  display: none;
}
</style>
