<template>
    <div>
        <div class="headers">
            <div class="headers-name">年度审计报告及财务情况说明书</div>
            <div>
                <el-form :inline="true" :model="form" class="demo-form-inline">
                    <el-form-item label="年度选择" size="small" style="position: relative;top: -3px;">
                        <el-date-picker
                            v-model="form.year"
                            type="year"
                            placeholder="选择年度"
                            format="yyyy"
                            value-format="yyyy"
                            class="year-picker"
                            style="width: 140px;">
                        </el-date-picker>
                    </el-form-item>
                    
                    <el-form-item label="是否有效" size="small" class="formitem" style="top:-3px;">
                        <el-select v-model="form.status" placeholder="请选择" class="selects">
                            <el-option v-for="item in statusOptions" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" size="small" @click="handleAdd"
                           class="tianjia">添加</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
        <div class="content-table">
            <el-table :data="tableData" border style="width: 100%">
                <el-table-column fixed prop="year" label="年度" align="center" width="100">
                </el-table-column>
                <el-table-column prop="name" label="材料名称" align="center">
                </el-table-column>
                <el-table-column prop="status" label="是否有效" align="center" width="130">
                    <template slot-scope="scope">
                        {{ scope.row.status === 1 || scope.row.status === '1' ? '有效' : '无效' }}
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作" align="center" width="180">
                    <template slot-scope="scope">
                        <el-button @click="handleClick(scope.row)" type="text" size="small">详情</el-button>
                        <el-button @click="handleDelete(scope.row)" type="text" size="small" style="color: #F56C6C;">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="footer">
                <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
                </el-pagination>
            </div>
        </div>
    </div>
</template>
<script>
import { list, deletes } from '@/api/finance/annual/index'
export default {
    props: {
        type: {
            type: Number,
            default: 1 // 年度审计报告
        }
    },
    data() {
        return {
            currentPage: 1,
            pageSize: 10,
            total: 0,
            tableData: [],
            statusOptions: [
                {
                    value: '',
                    label: "全部"
                },
                {
                    value: 1,
                    label: "有效"
                },
                {
                    value: 0,
                    label: "无效"
                }
            ],
            form: {
                year: '',
                status: ''
            }
        }
    },
    mounted() {
        this.getList()
    },
    watch: {
        'form.year'() {
            this.currentPage = 1
            this.getList()
        },
        'form.status'() {
            this.currentPage = 1
            this.getList()
        }
    },
    methods: {
        // 获取列表
        async getList() {
            try {
                const params = {
                    type: this.type.toString(),
                    status: this.form.status !== '' ? this.form.status.toString() : '',
                    year: this.form.year || '',
                    page: this.currentPage.toString(),
                    limit: this.pageSize.toString(),
                    orderBy: 'id',
                    orderDir: 'desc'
                }
                const res = await list(params)
                if (res && res.code === 1 && res.data) {
                    this.tableData = res.data.data || []
                    this.total = res.data.total || 0
                } else {
                    this.tableData = []
                    this.total = 0
                }
            } catch (error) {
                console.error('获取列表失败:', error)
                this.$message.error('获取列表失败，请稍后重试')
            }
        },
        // 新增
        handleAdd() {
            this.$emit('getadd', { type: this.type })
        },
        // 详情
        handleClick(row) {
            this.$emit('getaudit', row)
        },
        // 删除
        async handleDelete(row) {
            if (!row.id) {
                this.$message.warning('数据异常，无法删除')
                return
            }
            this.$confirm('确认删除这条记录吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                try {
                    const res = await deletes(row.id)
                    if (res && res.code === 1) {
                        this.$message.success('删除成功')
                        this.getList()
                    } else {
                        this.$message.error(res?.msg || '删除失败，请稍后重试')
                    }
                } catch (error) {
                    console.error('删除失败:', error)
                    this.$message.error('删除失败，请稍后重试')
                }
            }).catch(() => {})
        },
        // 分页
        handleSizeChange(val) {
            this.pageSize = val
            this.currentPage = 1
            this.getList()
        },
        handleCurrentChange(val) {
            this.currentPage = val
            this.getList()
        }
    }
}
</script>
<style scoped>
.footer {
    display: flex;
    justify-content: end;
    margin-top: 20px;
}
.tianjia {
    position: relative;
    top: -9px;
    background-color: #5065F6;
}
::v-deep .el-table thead {
    background-color: #DCE0FD !important;
    color: #363636 !important;
}
::v-deep .el-table__header th {
    background-color: #DCE0FD !important;
    color: #363636 !important;
}

.formitem {
    position: relative;
    top: -5px;
}

::deep .el-form-item__label {
    font-size: 15px !important;
}
.selects {
    width: 140px;
}
.year-picker {
    width: 140px;
}
/* 添加边框样式 */
::v-deep .el-date-editor.el-input .el-input__inner {
    border: 1px solid #DCDFE6 !important;
}
::v-deep .el-date-picker .el-input__inner {
    border: 1px solid #DCDFE6 !important;
}
::v-deep .el-select .el-input__inner {
    border: 1px solid #DCDFE6 !important;
}
.headers-name {
    font-size: 15px;
    position: relative;
    top: 14px;
}

.headers {
    display: flex;
    justify-content: space-between
}
</style>
