<script setup name='renewProjectDetail'>
import { getCurrentInstance, h, onMounted, reactive, ref, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { renewalDeclaredDetail } from '@/http/apis/declareMange/renewalProjectCapitalDeclare'
import store from '@/store'
import { renewalProjectAsscess } from '@/http/apis/projectStoreManage/renewalProjectTreasury'
import { contractDetail } from '@/http/apis/declareMange/contractRecord'
const { statusOptions, projectTypeOptions, statusGjOptions } =
        store.dictStore.globalDicts || {},
  tableListRef = ref(),
  ruleFormRef = ref(),
  router = useRouter(),
  route = useRoute(),
  total = ref(0),
  dialogFormVisible = ref(false),
  { proxy } = getCurrentInstance(),
  // 列表数据
  column = reactive([
    {
      label: '项目名称',
      key: 'projectName',
      prop: 'projectName',
      minWidth: '200',
      showOverflowTooltip: true
    },
    {
      label: '项目类型',
      key: 'projectType',
      prop: 'projectType',
      minWidth: '80',
      render: row => h('span', projectTypeOptions[row.projectType] || '-')
    },
    {
      label: '批复总金额（万元）',
      key: 'approvalAmount',
      prop: 'approvalAmount',
      width: 150
    },
    {
      label: '累计年度投资资金',
      key: 'annualAccumulateAmountList',
      slot: 'annualAccumulateAmountList',
      width: 150
    },
    {
      label: '项目状态',
      key: 'status',
      prop: 'status',
      width: '120',
      render: row => [
        h('span', {
          class: ['dot mr-4', `bg-${statusOptions[row.status]?.color || statusGjOptions[row.status]?.color}`]
        }),
        h(
          'span',
          {
            class: `text-${statusOptions[row.status]?.color || statusGjOptions[row.status]?.color}`
          },
          row.status && statusOptions[row.stage]?.name ? `${statusOptions[row.stage]?.name}-${statusOptions[row.status]?.name}` : statusGjOptions[row.status]?.name || '-'
        )
      ]
    }
  ]),
  data = ref([]),
  form = reactive({}),
  rules = reactive({
    recommend: [
      { required: true, message: '请填写审核意见', trigger: 'blur' }
    ]
  }),
  submitForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate(async (valid) => {
      if (valid) {
        await renewalProjectAsscess({
          ...form,
          projectRenewalId: route.query.id,
          result: false
        })
        dialogFormVisible.value = false
        proxy.$message.success('审核成功！')
        router.push({ name: 'renewalProjectTreasury' })
      } else {
        console.log('error submit!')
      }
    })
  },
  redeclare = () => {
    router.push({ name: 'renewalCapitalDeclare', query: { id: route.query.id }})
  },
  adopt = () => {
    proxy.$messageBox
      .confirm('确认审核通过吗?', '提示！', {
        type: 'warning'
      })
      .then(async () => {
        await renewalProjectAsscess({
          projectRenewalId: route.query.id,
          result: true
        })
        proxy.$message.success('审核成功！')
        router.push({ name: 'renewalProjectTreasury' })
      })
  },
  goBack = () => {
    router.go(-1)
  },
  routeName = ref(''),
  detail = ref({}),
  getDetail = async () => {
    const res = await renewalDeclaredDetail(route.query.id)
    detail.value = res.data
    data.value = [{ ...res.data }]
    getPayInfo(data.value[0].projectId)
  },
  // 支付信息
  payColumn = [
    {
      label: '付款笔数',
      key: 'number',
      slot: 'number'
    },
    {
      label: '付款计划',
      key: 'planAmount',
      slot: 'planAmount'
    },
    {
      label: '付款比例（%）',
      key: 'ratio',
      slot: 'ratio'
    },
    {
      label: '付款金额（万元）',
      key: 'paymentAmount',
      slot: 'paymentAmount'
    },
    {
      label: '预计付款时间',
      key: 'paymentTime',
      slot: 'paymentTime'
    },
    {
      label: '实际支付金额（万元）',
      key: 'actualPaymentAmount',
      slot: 'actualPaymentAmount',
      width: 180
    }
  ],
  paymentsData = ref(),
  getPayInfo = async (id) => {
    const res = await contractDetail(id)
    paymentsData.value = res?.data?.payments || []
  }
watch(
  route,
  (val) => {
    routeName.value = route.meta.title
  },
  { immediate: true, deep: true }
)
onMounted(() => {
  getDetail()
})
</script>

<template>
  <div class="">
    <el-alert
      v-if="detail.approvalStatus==='NOT_PASS'"
      title="审核不通过"
      type="error"
      :description="`审核意见：${detail.auditOpinion||'-'}`"
      show-icon
    />
    <el-alert
      v-if="detail.approvalStatus==='PASS'"
      title="审核通过"
      type="success"
      :description="`审核意见：${detail.auditOpinion||'-'}`"
      show-icon
    />
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>申报信息</span>
        </div>
      </template>
      <div class="flex justify-around">
        <div style="font-size: 14px;flex-shrink:0">关联项目：</div>
        <table-list
          ref="tableListRef"
          class="flex-1 mb-16"
          :column="column"
          :data="data"
          :total="total"
          :height="200"
          :pagination="false"
        >
          <template #annualAccumulateAmountList="{scope}">
            <p v-for="(item,index) in scope.row.annualAccumulateAmountList" :key="index">{{ index+1 }}、{{ item.projectYear }}年投资{{ item.annualAmount }}万元</p>
          </template>
        </table-list>
      </div>
      <p class="font-bold mb-8">支付信息</p>
      <table-list
        ref="tableListRef"
        :column="payColumn"
        :data="paymentsData"
        :pagination="false"
        :empty-temp="false"
        class="mb-16"
      >
        <template #number="{scope}">
          <span>第{{ scope.$index+1 }}笔</span>
        </template>
        <template #planAmount="{scope}">
          {{ scope.row.planAmount }}
        </template>
        <template #ratio="{scope}">
          {{ scope.row.ratio }}
        </template>
        <template #paymentAmount="{scope}">
          {{ scope.row.paymentAmount }}
        </template>
        <template #paymentTime="{scope}">
          {{ scope.row.paymentTime }}
        </template>
        <template #actualPaymentAmount="{scope}">
          {{ scope.row.actualPaymentAmount }}
        </template>
      </table-list>
      <template v-if="detail.annualAccumulateAmountList?.filter((i,key)=>key!==0&&i.projectYear!==detail.projectYear)?.length">
        <p class="font-bold mt-20">历史续建资金信息</p>
        <el-descriptions
          v-for="(item,index) in detail.annualAccumulateAmountList.filter((i,key)=>key!==0&&i.projectYear!==detail.projectYear)"
          :key="index"
          class="mt-20"
          :column="2"
          border
        >
          <el-descriptions-item
            label="预算年度"
            span="1"
            width="250px"
          >
            {{ item.projectYear }}
          </el-descriptions-item>
          <el-descriptions-item
            :label="`${item.projectYear}年支付金额（万元）`"
            width="250px"
          >
            {{ item.annualAmount }}
          </el-descriptions-item>
          <el-descriptions-item label="自有金额（万元）">
            {{ item.haveAmount }}
          </el-descriptions-item>
          <el-descriptions-item label="政府投资-本级财政资金（万元）">
            {{ item.govOwnFinanceAmount }}
          </el-descriptions-item>
          <el-descriptions-item label="政府投资-上级补助资金（万元）">
            {{ item.govSuperiorFinanceAmount }}
          </el-descriptions-item>
          <el-descriptions-item label="银行贷款（万元）">
            {{ item.bankLendingAmount }}
          </el-descriptions-item>
          <el-descriptions-item
            label="其他（万元）"
            span="2"
          >
            {{ item.otherAmount }}
          </el-descriptions-item>
        </el-descriptions>
      </template>
      <el-descriptions
        class="mt-20"
        title="申报续建资金信息"
        :column="2"
        border
      >
        <el-descriptions-item
          label="预算年度"
          span="1"
          width="250px"
        >
          {{ detail.projectYear }}
        </el-descriptions-item>
        <el-descriptions-item
          :label="`${detail.projectYear}年支付金额（万元）`"
          width="250px"
        >
          {{ detail.annualPaymentAmount }}
        </el-descriptions-item>
        <el-descriptions-item label="自有金额（万元）">
          {{ detail.haveAmount }}
        </el-descriptions-item>
        <el-descriptions-item label="政府投资-本级财政资金（万元）">
          {{ detail.govOwnFinanceAmount }}
        </el-descriptions-item>
        <el-descriptions-item label="政府投资-上级补助资金（万元）">
          {{ detail.govSuperiorFinanceAmount }}
        </el-descriptions-item>
        <el-descriptions-item label="银行贷款（万元）">
          {{ detail.bankLendingAmount }}
        </el-descriptions-item>
        <el-descriptions-item
          label="其他（万元）"
          span="2"
        >
          {{ detail.otherAmount }}
        </el-descriptions-item>
      </el-descriptions>
      <div class="flex justify-center items-center mt-10">
        <el-button
          v-if="routeName =='续建项目资金申报详情'&&detail.approvalStatus==='NOT_PASS'"
          class="mr-20"
          type="primary"
          @click="redeclare"
        >重新申报</el-button>
        <template v-if="routeName !=='续建项目资金申报详情'&&detail.approvalStatus==='PENDING'">
          <el-button
            class="mr-20"
            type="primary"
            @click="adopt"
          >通过</el-button>
          <el-button
            class="mr-20"
            type="danger"
            @click="dialogFormVisible = true"
          >不通过</el-button>
        </template>
        <el-button
          plain
          @click="goBack"
        >返回</el-button>
      </div>
    </el-card>
    <el-dialog
      v-model="dialogFormVisible"
      title="不通过"
    >
      <el-form
        ref="ruleFormRef"
        :model="form"
        :rules="rules"
        status-icon
      >
        <el-form-item
          label="审核意见"
          label-width="auto"
          prop="auditOpinion"
        >
          <el-input
            v-model="form.auditOpinion"
            maxlength="200"
            placeholder="请输入"
            show-word-limit
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button
            type="primary"
            @click="submitForm(ruleFormRef)"
          >
            提交
          </el-button>
          <el-button @click="dialogFormVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style lang='less' scoped> </style>
