<template>
  <baseTitle title="上传费用"></baseTitle>
  <el-form :model="tableForm" ref="form" class="query-form">
    <el-form-item label="项目名称">
      <span>{{projectName}}</span>
    </el-form-item>
    <el-table :data="tableForm.tableData" style="width: 100%">
      <el-table-column prop="time" label="时间">
        <template #default="scope">
          <el-form-item prop="time" :rules="scope.row.time?'1':rules.time">
            <el-date-picker
                v-model="scope.row.time"
                type="date"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
                placeholder="选择时间"
            >
            </el-date-picker>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column prop="projectCost" label="项目费用">
        <template #default="scope">
          <el-form-item prop="projectCost" :rules="scope.row.projectCost?'1':rules.projectCost">
            <el-select v-model="scope.row.projectCost" placeholder="请选择费用性质" clearable filterable>
              <el-option
                  v-for="item in cacheStore.getDict('project_cost')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column prop="researchStage" label="研发阶段">
        <template #default="scope">
          <el-form-item prop="researchStage"  :rules="scope.row.researchStage?'1':rules.researchStage">
            <el-select v-model="scope.row.researchStage" placeholder="请选择研发阶段" clearable filterable>
              <el-option
                  v-for="item in cacheStore.getDict('fee_stage')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column prop="digest" label="摘要">
        <template #default="scope">
          <el-form-item prop="digest" :rules="scope.row.digest?'1':rules.digest">
            <el-input v-model="scope.row.digest" placeholder="请输入摘要" clearable>
            </el-input>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column prop="afterTax" label="税后余额(元)">
        <template #default="scope">
          <el-form-item prop="afterTax" :rules="scope.row.afterTax?'1':rules.afterTax">
            <el-input v-model="scope.row.afterTax" placeholder="请输入税后余额" clearable>
            </el-input>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column prop="oper" label="操作">
        <template #default="scope">
          <el-button type="primary" @click="handleDelete(scope.$index)" link style="font-size: 18px">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <div style="width:100%;text-align: center;padding: 10px">
    <el-button type="primary" @click="handleAdd" link style="font-size: 18px">添加一行</el-button>
  </div>
  <div class="oper-page-btn">
    <el-button color="#DED0B2" @click="handleSubmit(form)">提交</el-button>
    <el-button @click="handleBack">返回</el-button>
  </div>
</template>

<script setup lang="jsx">
import {ElNotification} from "element-plus";
import {addLedger} from "@/api/project-manage";
import {useTagsView} from '@/stores/tagsview.js'
import {useRoute} from "vue-router";
import {useCacheStore} from '@/stores/cache.js'

const cacheStore = useCacheStore()
const route = useRoute()
const router = useRouter()
const tagsViewStore = useTagsView()
const formData = ref({})
const form = ref()
const projectName=ref(localStorage.getItem('projectName'))
const rules = reactive({
  time: [{required: true, message: '请选择时间', trigger: 'blur'}],
  projectCost: [{required: true, message: '请输入项目费用', trigger: 'blur'}],
  researchStage: [{required: true, message: '请输入研发阶段', trigger: 'blur'}],
  digest: [{required: true, message: '请输入摘要', trigger: 'blur'}],
  afterTax: [{required: true, message: '请输入税后余额', trigger: 'blur'}]
})
const tableForm = reactive({
  tableData: [
    {
      projectId: route.query.id,
      time: '',
      projectCost: '',
      researchStage: '',
      digest: '',
      afterTax: ''
    }
  ]
})
const handleAdd = () => {
  let row = {
    projectId: route.query.id,
    time: '',
    projectCost: '',
    researchStage: '',
    digest: '',
    afterTax: ''
  }
  tableForm.tableData.push(row)
}
const handleDelete = (index) => {
  tableForm.tableData.splice(index, 1)
}
const handleSubmit = (instance) => {
  if (!instance) return
  instance.validate(async (valid) => {
    if (!valid) return
    instance.clearValidate()
    tableForm.tableData.forEach(item=>{
      item.projectName=projectName.value
    })
    const res =await addLedger(tableForm.tableData)
    ElNotification({
      title: '提示',
      message: res.msg,
      type: res.code === 1000 ? 'success' : 'error'
    })
    if (res.code === 1000) {
      tagsViewStore.delVisitedViews(router.currentRoute.value.path)
      router.push({
        name: 'Implementation/account',
        query:{
          id:route.query.id
        }
      })
    }
  })
}
const handleBack = () => {
  history.back()
}
</script>

