<template>
    <div>
        <div class="coentent">
            <div class="contents-area">
                <div class="areas">
                    <div class="headers">
                        <div class="headers-left">
                            <div class="headers-title">研发支出辅助账汇总表｜金额单位（元）</div>
                        </div>
                        <div class="headers-right">
                            <span>申报扣除年度</span>
                            <el-select v-model="value" size="small" style="border: 1px solid #ededed;" placeholder="请选择">
                                <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>
                            <el-button type="primary" size="small" class="download-btn">下载</el-button>
                        </div>
                    </div>

                    <div class="content-table">
                        <el-table
                            :data="tableData"
                            border
                            :header-cell-style="{ background: '#EDEFFE', color: '#363636', textAlign: 'center' }"
                            :cell-style="cellStyle"
                            style="width: 100%">
                            <el-table-column type="index" label="序号" width="70" align="center" :index="formatIndex"></el-table-column>
                            <el-table-column prop="projectCode" label="项目代号" min-width="140"></el-table-column>
                            <el-table-column prop="projectName" label="项目名称" min-width="260"></el-table-column>
                            <el-table-column prop="status" label="项目情况" width="100" align="center"></el-table-column>
                            <el-table-column prop="supportType" label="支出类型" width="100" align="center"></el-table-column>
                            <el-table-column prop="allowableAmount" label="允许加计扣除金额合计" width="180" align="right"></el-table-column>
                            <el-table-column label="人员人工费" width="120" align="center">
                                <template #default="{ row }">
                                    {{ row.costs[0] }}
                                </template>
                            </el-table-column>
                            <el-table-column label="直接投入费" width="120" align="center">
                                <template #default="{ row }">
                                    {{ row.costs[1] }}
                                </template>
                            </el-table-column>
                            <el-table-column label="折旧费" width="120" align="center">
                                <template #default="{ row }">
                                    {{ row.costs[2] }}
                                </template>
                            </el-table-column>
                            <el-table-column label="项目辅助账" width="120" align="center">
                                <template #default="scope">
                                    <el-button type="text" class="link-btn" @click="handleViewDetail(scope.row)">查看</el-button>
                                </template>
                            </el-table-column>
                            <template #append>
                                <tbody class="table-summary">
                                    <tr>
                                        <!-- "合计金额"跨 5 列（序号、项目代号、项目名称、项目情况、支出类型）-->
                                        <td class="summary-label" colspan="5">合计金额</td>
                                        <!-- 允许加计扣除金额合计（右对齐） -->
                                        <td class="summary-amount" align="right">{{ totalAllowableAmount }}</td>
                                        <!-- 人员人工费、直接投入费、折旧费（居中） -->
                                        <td class="summary-amount" align="center">{{ totalCosts[0] }}</td>
                                        <td class="summary-amount" align="center">{{ totalCosts[1] }}</td>
                                        <td class="summary-amount" align="center">{{ totalCosts[2] }}</td>
                                        <!-- 项目辅助账（居中对齐，显示"/"） -->
                                        <td class="summary-empty" align="center">/</td>
                                    </tr>
                                </tbody>
                            </template>
                        </el-table>
                    </div>
                </div>
            </div>
        </div>

        <el-dialog
            :visible.sync="detailDialogVisible"
            width="1400px"
            class="detail-dialog"
            :close-on-click-modal="false"
            :destroy-on-close="true"
            @closed="resetDetailDialog">
            <template #title>
                <div class="detail-title">{{ detailTitle }}</div>
            </template>
            <div class="detail-body">
                <el-table
                    :data="detailTableData"
                    border
                    :header-cell-style="{ background: '#EDEFFE', color: '#363636', textAlign: 'center' }"
                    :cell-style="detailCellStyle"
                    style="width: 100%"
                    max-height="500">
                    <el-table-column type="index" label="序号" width="70" align="center" :index="detailFormatIndex"></el-table-column>
                    <el-table-column prop="date" label="日期" width="120" align="center"></el-table-column>
                    <el-table-column prop="voucherNo" label="凭证号" width="120" align="center"></el-table-column>
                    <el-table-column prop="summary" label="摘要" min-width="300"></el-table-column>
                    <el-table-column prop="totalAmount" label="会计凭证记载金额" width="160" align="right"></el-table-column>
                    <el-table-column prop="personnelCost" label="人员人工费" width="120" align="right"></el-table-column>
                    <el-table-column prop="directInput" label="直接投入费" width="120" align="right"></el-table-column>
                    <el-table-column prop="depreciation" label="折旧费" width="120" align="right"></el-table-column>
                    <el-table-column prop="amortization" label="无形资产摊销" width="140" align="right"></el-table-column>
                    <el-table-column prop="designFee" label="新产品设计费等" width="150" align="right"></el-table-column>
                </el-table>
            </div>
            <template #footer>
                <div class="detail-footer">
                    <el-button size="small" @click="detailDialogVisible = false">关闭</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            filters: {
                applyYear: '2024'
            },
            value: '2024',
            options: [
                { value: '2024', label: '2024' },
                { value: '2023', label: '2023' },
                { value: '2022', label: '2022' }
            ],
            detailDialogVisible: false,
            detailTitle: '',
            detailTableData: [],
            currentRow: null,
            tableData: [
                {
                    id: 1,
                    projectCode: 'WU2412001',
                    projectName: '高速公路收费景观智能集成系统研发项目',
                    status: '未结束',
                    supportType: '费用化',
                    allowableAmount: '2000.00',
                    costs: ['0.00', '0.00', '0.00']
                },
                {
                    id: 2,
                    projectCode: 'WU2412001',
                    projectName: '反光连点标志安全管控算法模型调试研究',
                    status: '已结束',
                    supportType: '费用化',
                    allowableAmount: '2000.00',
                    costs: ['0.00', '0.00', '0.00']
                },
                {
                    id: 3,
                    projectCode: 'WU2412001',
                    projectName: '基于高分卫星技术的数据资源投标研究与应用',
                    status: '已结束',
                    supportType: '费用化',
                    allowableAmount: '2000.00',
                    costs: ['0.00', '0.00', '0.00']
                },
                {
                    id: 4,
                    projectCode: 'WU2412001',
                    projectName: '隧道无人值守一体化系统研发项目',
                    status: '已结束',
                    supportType: '费用化',
                    allowableAmount: '2000.00',
                    costs: ['0.00', '0.00', '0.00']
                },
                {
                    id: 5,
                    projectCode: 'WU2412001',
                    projectName: '常温光伏智慧检测可视化系统研发项目',
                    status: '已结束',
                    supportType: '费用化',
                    allowableAmount: '2000.00',
                    costs: ['0.00', '0.00', '0.00']
                },
                {
                    id: 6,
                    projectCode: 'WU2412001',
                    projectName: '联网收费软件智能链运维系统研发项目',
                    status: '已结束',
                    supportType: '费用化',
                    allowableAmount: '2000.00',
                    costs: ['0.00', '0.00', '0.00']
                }
            ]
        }
    },
    computed: {
        totalAllowableAmount() {
            const total = this.tableData.reduce((sum, item) => sum + Number(item.allowableAmount), 0)
            return total.toFixed(2)
        },
        totalCosts() {
            const sums = [0, 0, 0]
            this.tableData.forEach((item) => {
                item.costs.forEach((cost, index) => {
                    sums[index] += Number(cost)
                })
            })
            return sums.map((value) => value.toFixed(2))
        }
    },
    methods: {
        cellStyle({ column }) {
            const leftAlignedProps = ['projectCode', 'projectName']
            return {
                textAlign: leftAlignedProps.includes(column.property) ? 'left' : 'center',
                color: '#363636'
            }
        },
        formatIndex(index) {
            return index + 1
        },
        handleViewDetail(row) {
            this.currentRow = row
            this.detailTitle = `${this.value}年-${row.projectName}`
            this.detailTableData = this.getDetailData(row)
            this.detailDialogVisible = true
        },
        resetDetailDialog() {
            this.currentRow = null
            this.detailTitle = ''
            this.detailTableData = []
        },
        detailCellStyle({ column }) {
            const leftAlignedProps = ['summary']
            const rightAlignedProps = ['totalAmount', 'personnelCost', 'directInput', 'depreciation', 'amortization', 'designFee']
            return {
                textAlign: leftAlignedProps.includes(column.property) ? 'left' : rightAlignedProps.includes(column.property) ? 'right' : 'center',
                color: '#363636'
            }
        },
        detailFormatIndex(index) {
            return index + 1
        },
        getDetailData(row) {
            return [
                {
                    id: 1,
                    date: '2024-12-10',
                    voucherNo: '转-0057',
                    summary: '计提12月研发部员工养老保险',
                    totalAmount: '2000.00',
                    personnelCost: '2000.00',
                    directInput: '0.00',
                    depreciation: '0.00',
                    amortization: '0.00',
                    designFee: '0.00'
                },
                {
                    id: 2,
                    date: '2024-12-10',
                    voucherNo: '转-0057',
                    summary: '计提12月研发部员工失业保险',
                    totalAmount: '2000.00',
                    personnelCost: '2000.00',
                    directInput: '0.00',
                    depreciation: '0.00',
                    amortization: '0.00',
                    designFee: '0.00'
                },
                {
                    id: 3,
                    date: '2024-12-10',
                    voucherNo: '转-0057',
                    summary: '计提12月研发部员工工伤保险',
                    totalAmount: '2000.00',
                    personnelCost: '2000.00',
                    directInput: '0.00',
                    depreciation: '0.00',
                    amortization: '0.00',
                    designFee: '0.00'
                },
                {
                    id: 4,
                    date: '2024-12-10',
                    voucherNo: '转-0057',
                    summary: '计提12月研发部员工医疗保险',
                    totalAmount: '2000.00',
                    personnelCost: '2000.00',
                    directInput: '0.00',
                    depreciation: '0.00',
                    amortization: '0.00',
                    designFee: '0.00'
                },
                {
                    id: 5,
                    date: '2024-12-10',
                    voucherNo: '转-0057',
                    summary: '计提12月研发部员工住房公积金',
                    totalAmount: '2000.00',
                    personnelCost: '2000.00',
                    directInput: '0.00',
                    depreciation: '0.00',
                    amortization: '0.00',
                    designFee: '0.00'
                },
                {
                    id: 6,
                    date: '2024-12-10',
                    voucherNo: '转-0057',
                    summary: '计提12月研发部员工工资',
                    totalAmount: '2000.00',
                    personnelCost: '2000.00',
                    directInput: '0.00',
                    depreciation: '0.00',
                    amortization: '0.00',
                    designFee: '0.00'
                },
                {
                    id: 7,
                    date: '2024-12-10',
                    voucherNo: '转-0057',
                    summary: '技术研发部应付中远海运服务费',
                    totalAmount: '60000.00',
                    personnelCost: '0.00',
                    directInput: '60000.00',
                    depreciation: '0.00',
                    amortization: '0.00',
                    designFee: '0.00'
                },
                {
                    id: 8,
                    date: '2024-12-10',
                    voucherNo: '转-0057',
                    summary: '11月固定资产折旧费',
                    totalAmount: '2000.00',
                    personnelCost: '0.00',
                    directInput: '0.00',
                    depreciation: '2000.00',
                    amortization: '0.00',
                    designFee: '0.00'
                },
                {
                    id: 9,
                    date: '2024-12-10',
                    voucherNo: '转-0057',
                    summary: '12月固定资产折旧费',
                    totalAmount: '2000.00',
                    personnelCost: '0.00',
                    directInput: '0.00',
                    depreciation: '2000.00',
                    amortization: '0.00',
                    designFee: '0.00'
                },
                {
                    id: 10,
                    date: '2024-12-10',
                    voucherNo: '转-0057',
                    summary: '11月固定资产折旧费',
                    totalAmount: '2000.00',
                    personnelCost: '0.00',
                    directInput: '0.00',
                    depreciation: '2000.00',
                    amortization: '0.00',
                    designFee: '0.00'
                },
                {
                    id: 11,
                    date: '2024-12-10',
                    voucherNo: '转-0057',
                    summary: '付技研部陈超贵阳至清镇差旅费,研3234/ZY0012',
                    totalAmount: '2000.00',
                    personnelCost: '0.00',
                    directInput: '0.00',
                    depreciation: '0.00',
                    amortization: '0.00',
                    designFee: '0.00'
                },
                {
                    id: 12,
                    date: '2024-12-10',
                    voucherNo: '转-0057',
                    summary: '差旅报销',
                    totalAmount: '2000.00',
                    personnelCost: '0.00',
                    directInput: '0.00',
                    depreciation: '0.00',
                    amortization: '0.00',
                    designFee: '0.00'
                },
                {
                    id: 13,
                    date: '2024-12-10',
                    voucherNo: '转-0057',
                    summary: '日常报销',
                    totalAmount: '2000.00',
                    personnelCost: '0.00',
                    directInput: '0.00',
                    depreciation: '0.00',
                    amortization: '0.00',
                    designFee: '0.00'
                }
            ]
        }
    }
}
</script>

<style scoped>
.coentent {
    width: 100%;
    height: 100%;
    overflow-x: hidden;
}

.contents-area {
    width: 100%;
    margin-top: 20px;
    overflow-x: hidden;
}

.areas {
    width: 97%;
    margin: 0 auto;
    overflow-x: hidden;
}

.headers {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    flex-wrap: wrap;
    gap: 16px;
}

.headers-left {
    display: flex;
    align-items: center;
}

.headers-title {
    font-size: 15px;
    color: #17192C;
}

.headers-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

.search-input {
    width: 120px;
}

.download-btn {
    background: #5165F7;
    border-color: #5165F7;
}

.content-table {
    background: #fff;
    border-radius: 8px;
    padding: 12px 0 0;
}

::v-deep .el-table__body-wrapper .table-summary {
    background: #F0F2FF;
}

::v-deep .el-table__body-wrapper .table-summary tr {
    background: #F0F2FF;
}

::v-deep .el-table__body-wrapper .table-summary td {
    background: #F0F2FF !important;
    padding: 12px 16px;
    font-size: 13px;
    color: #363636;
    border: 1px solid #E0E3F5;
    border-top: none;
    vertical-align: middle;
    box-sizing: border-box;
}

::v-deep .el-table__body-wrapper .table-summary td:first-child {
    border-left: 1px solid #E0E3F5;
}

/* 合计金额标签 - 左对齐 */
::v-deep .el-table__body-wrapper .table-summary td.summary-label {
    text-align: left !important;
    font-weight: 600;
}

::v-deep .el-table__body-wrapper .table-summary td.summary-label .cell {
    text-align: left !important;
}

/* 允许加计扣除金额合计 - 右对齐 */
::v-deep .el-table__body-wrapper .table-summary td.summary-amount[align="right"] {
    text-align: right !important;
}

::v-deep .el-table__body-wrapper .table-summary td.summary-amount[align="right"] .cell {
    text-align: right !important;
}

/* 人员人工费、直接投入费、折旧费 - 居中 */
::v-deep .el-table__body-wrapper .table-summary td.summary-amount[align="center"] {
    text-align: center !important;
}

::v-deep .el-table__body-wrapper .table-summary td.summary-amount[align="center"] .cell {
    text-align: center !important;
}

/* 项目辅助账 - 居中 */
::v-deep .el-table__body-wrapper .table-summary td.summary-empty {
    text-align: center !important;
}

::v-deep .el-table__body-wrapper .table-summary td.summary-empty .cell {
    text-align: center !important;
}

.link-btn {
    color: #5165F7;
    padding: 0;
}

.detail-dialog ::v-deep .el-dialog {
    border-radius: 12px;
}

.detail-dialog ::v-deep .el-dialog__header {
    padding: 20px 28px 12px;
    border-bottom: none;
}

.detail-title {
    font-size: 18px;
    font-weight: 600;
    color: #17192C;
    text-align: left;
}

.detail-body {
    padding: 0 28px 20px;
}

.detail-footer {
    display: flex;
    justify-content: center;
    gap: 16px;
}

</style>