<!--
 * @Author: ysh
 * @Date: 2025-05-15 15:30:09
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2025-06-19 16:13:39
-->
<template>
  <div class="site-cost">
    <table border="1" style="width:100%;">
      <tr style="background-color:#f8f8f9">
        <td style="width: 50px;">序号</td>
        <td style="min-width: 100px;">开支项</td>
        <td style="min-width: 100px;">金额</td>
        <td class="adjust">核算金额</td>
        <td>备注</td>
      </tr>
      <tr v-for="site, index in siteList" :key="site.id">
        <td>{{ index + 1 }}</td>
        <td>{{ site.name }}</td>
        <td style="text-align: right;">{{ Number(site.amount).toFixed(2) }}</td>
        <td style="text-align: right;">
          <el-input-number v-if="taskName === '核算编制'" :controls="false" v-model="site.amountAdjust"
            placeholder="请输入核算金额" @change="handleSiteChange(site)" />
          <span v-else>{{ site.amountAdjust }}</span>
        </td>
        <td style="text-align: left;">{{ site.remark }}</td>
      </tr>
      <tr>
        <td class="amount" colspan="2">现场开支合计</td>
        <td class="amount" style="text-align: right;">{{siteList.reduce((sum, site) => sum + (Number(site.amount)
          || 0), 0).toFixed(2)}}</td>
        <td class="amount" style="text-align: right;">{{siteList.reduce((sum, site) => sum + (Number(site.amountAdjust)
          || 0), 0).toFixed(2)}}</td>
      </tr>
    </table>
    <div class="mt10">
      <el-button type="success" plain icon="el-icon-plus" @click="isOpenSite = true" size="mini">新增开支项</el-button>
    </div>
    <!-- 添加现场开支对话框 -->
    <el-dialog title="添加现场开支" :visible.sync="isOpenSite" width="700px" append-to-body>
      <el-form :model="siteForm" label-width="120px">
        <el-form-item label="开支项">
          <el-input v-model="siteForm.name" placeholder="请输入开支项" />
        </el-form-item>
        <el-form-item label="金额">
          {{ siteForm.amount }}
        </el-form-item>
        <el-form-item label="核算金额">
          <el-input-number v-model="siteForm.amountAdjust" placeholder="请输入核算金额" :controls="false" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" :rows="3" v-model="siteForm.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isOpenSite = false">取 消</el-button>
        <el-button type="primary" @click="addSiteConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addSite } from "@/api/oa/budget/budgetSite";
export default {
  props: {
    siteList: {
      type: Array,
      default: () => []
    },
    taskName: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      isOpenSite: false,
      siteForm: {
        name: '',
        amount: 0,
        amountAdjust: 0,
        remark: ''
      }
    }
  },
  methods: {
    handleSiteChange(site) {
      this.$emit('update:siteList', this.siteList);
    },
    async addSiteConfirm() {
      const budgetId = this.siteList[0].budgetId;
      this.siteForm.budgetId = budgetId;
      await addSite(this.siteForm)
      this.isOpenSite = false;
      this.$emit('updateList');
    }
  },
}
</script>

<style lang="scss" scoped>
.site-cost {
  .add-staff {
    margin-bottom: 10px;
  }

  table {
    text-align: center;
    border-collapse: collapse;
    margin: 0 auto;

    td {
      padding: 5px;
    }
  }

  .adjust {
    color: #F56C6C;
    width: 120px;
  }
}

.amount {
  font-weight: bold;
  font-family: Arial, Helvetica, sans-serif;
}
</style>