<template>
    <div>
        <div class="coentent">
            <div class="contents-area">
                <div class="areas">
                    <div v-if="active==0">
                        <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-select v-model="form.revenueType" placeholder="请选择" class="selects">
                                            <el-option v-for="item in revenueTypeOptions" :key="item.value" :label="item.label"
                                                :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                    <el-form-item label="时间筛选" size="small" style="position: relative;top: -3px;">
                                        <el-select v-model="form.year" placeholder="请选择" class="selects">
                                            <el-option v-for="item in yearOptions" :key="item.value" :label="item.label"
                                                :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                    <el-form-item size="small" style="position: relative;top: -3px;">
                                        <el-select v-model="form.quarter" placeholder="请选择" class="selects">
                                            <el-option v-for="item in quarterOptions" :key="item.value" :label="item.label"
                                                :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                   
                                </el-form>
                            </div>
                        </div>
                        <div class="content-table">
                            <el-table 
                                :data="tableData" 
                                :header-cell-style="{ background: '#EDEFFE', color: '#363636', textAlign: 'center' }"
                                border 
                                style="width: 100%">
                                <el-table-column prop="serial" label="序号" width="80" align="center"></el-table-column>
                                <el-table-column prop="productName" label="审批模块" align="center"></el-table-column>
                                <el-table-column prop="isMainProduct" label="一级审核人" width="120" align="center">
                                    <template slot-scope="scope">
                                        <span>{{ scope.row.isMainProduct }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="revenueType" label="二级审核人" width="150" align="center">
                                    <template slot-scope="scope">
                                        <span>{{ scope.row.revenueType }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="salesRevenue" label="三级审核人" align="center">
                                    <template slot-scope="scope">
                                        <span>{{ formatCurrency(scope.row.salesRevenue) }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="salesRevenue" label="状态" align="center">
                                    <template slot-scope="scope">
                                        <span>{{ formatCurrency(scope.row.salesRevenue) }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column fixed="right" label="操作" align="center" width="150">
                                    <template slot-scope="scope">
                                        <el-button  type="text" size="small" style="color: #5165f7;">禁用</el-button>
                                        <el-button @click="handleHomepage(scope.row)" type="text" size="small" style="color: #5165f7;">编辑</el-button>
                                       
                                    </template>
                                </el-table-column>
                            </el-table>
                            <div class="footer">
                                <el-pagination 
                                    @size-change="handleSizeChange" 
                                    @current-change="handleCurrentChange"
                                    :current-page="currentPage" 
                                    :page-sizes="[100, 200, 300, 400]" 
                                    :page-size="100"
                                    layout="total, sizes, prev, pager, next, jumper" 
                                    :total="400">
                                </el-pagination>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <el-dialog
            :visible.sync="dialogVisible"
            width="840px"
            class="audit-dialog"
            :close-on-click-modal="false"
            :destroy-on-close="true"
            @closed="resetDialog">
            <template #title>
                <div class="dialog-title">添加/编辑审核</div>
            </template>
            <div class="content-table">
                        <div class="content-areas">
                            <div class="contents-left">
                                <span style="margin-left: 20px;">一级审核人</span>
                            </div>
                            <div class="contents-right">
                                <span style="margin-left: 20px;">
                                    <el-select v-model="value" placeholder="请选择">
                                    <el-option
                                    v-for="item in options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                    </el-option>
                                </el-select>
                        </span>
                            </div>
                            <div class="contents-left">
                                <span style="margin-left: 20px;">一级审核人</span>
                            </div>
                            <div class="contents-right">
                                <span style="margin-left: 20px;">
                                    <el-select v-model="value" placeholder="请选择">
                                    <el-option
                                    v-for="item in options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                    </el-option>
                                </el-select>
                                </span>
                            </div>
                            <div class="contents-left">
                                <span style="margin-left: 20px;">一级审核人</span>
                            </div>
                            <div class="contents-right">
                                <span style="margin-left: 20px;">
                                    <el-select v-model="value" placeholder="请选择">
                                    <el-option
                                    v-for="item in options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                    </el-option>
                                </el-select>
                                </span>
                            </div>
                        </div>
                      
                      
                     
                       
                       
                      
                    </div>
            <div class="dialog-section module-section">
                <div class="dialog-section-header">审核模块选择</div>
                <el-tree
                    ref="moduleTree"
                    :data="moduleTreeData"
                    show-checkbox
                    node-key="id"
                    :default-checked-keys="selectedModules"
                    :props="moduleTreeProps"
                    default-expand-all
                    check-on-click-node
                    class="module-tree">
                </el-tree>
            </div>
            <template #footer>
                <div class="dialog-footer">
                    <el-button size="small" @click="handleDialogCancel">取消</el-button>
                    <el-button type="primary" size="small" @click="submitAudit">保存</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data() {
        return {
            active: 0,
            currentPage: 1,
            dialogVisible: false,
            editingRow: null,
            reviewForm: {
                firstReviewer: '',
                secondReviewer: '',
                thirdReviewer: ''
            },
            reviewerOptions: [
                { value: '陈翔', label: '陈翔' },
                { value: '刘伟', label: '刘伟' },
                { value: '王敏', label: '王敏' },
                { value: '李华', label: '李华' },
                { value: '张婷', label: '张婷' }
            ],
            moduleTreeData: [
                {
                    id: 'resource',
                    label: '资源资料',
                    children: [
                        { id: 'affairs', label: '事务管理' },
                        { id: 'hr', label: '人力资源' },
                        {
                            id: 'achievement',
                            label: '成果管理',
                            children: [
                                {
                                    id: 'ip',
                                    label: '知识产权',
                                    children: [
                                        { id: 'patent', label: '发明专利' },
                                        { id: 'software', label: '软件著作' },
                                        { id: 'ic', label: '集成电路布图设计专有权' }
                                    ]
                                },
                                { id: 'thesis', label: '论文管理' },
                                { id: 'contract', label: '合同登记' }
                            ]
                        }
                    ]
                },
                { id: 'research', label: '研发项目' },
                { id: 'economic', label: '经营项目' },
                { id: 'hightech', label: '高企法人' },
                { id: 'special', label: '专精特新' },
                { id: 'deduction', label: '加计扣除' }
            ],
            moduleTreeProps: {
                children: 'children',
                label: 'label'
            },
            selectedModules: [],
            form: {
                revenueType: '全部',
                year: '2025',
                quarter: '第一季度'
            },
            revenueTypeOptions: [
                {
                    value: '全部',
                    label: '全部'
                },
                {
                    value: '产品收入',
                    label: '产品收入'
                },
                {
                    value: '技术转让收入',
                    label: '技术转让收入'
                }
            ],
            yearOptions: [
                {
                    value: '2023',
                    label: '2023'
                },
                {
                    value: '2024',
                    label: '2024'
                },
                {
                    value: '2025',
                    label: '2025'
                },
                {
                    value: '2026',
                    label: '2026'
                }
            ],
            quarterOptions: [
                {
                    value: '第一季度',
                    label: '第一季度'
                },
                {
                    value: '第二季度',
                    label: '第二季度'
                },
                {
                    value: '第三季度',
                    label: '第三季度'
                },
                {
                    value: '第四季度',
                    label: '第四季度'
                }
            ],
            tableData: [
                {
                    serial: 1,
                    productName: '智慧收费产品',
                    isMainProduct: '是',
                    revenueType: '产品收入',
                    salesRevenue: 1600.00
                },
                {
                    serial: 2,
                    productName: '智慧照明产品',
                    isMainProduct: '否',
                    revenueType: '产品收入',
                    salesRevenue: 1600.00
                },
                {
                    serial: 3,
                    productName: '高速公路机电系统建管养一体化服务',
                    isMainProduct: '否',
                    revenueType: '技术转让收入',
                    salesRevenue: 1600.00
                },
                {
                    serial: 4,
                    productName: '隧道智慧管控运维系统及服务',
                    isMainProduct: '否',
                    revenueType: '技术转让收入',
                    salesRevenue: 0
                },
                {
                    serial: 5,
                    productName: '交通行业省级视频云联网产品及服务',
                    isMainProduct: '否',
                    revenueType: '技术转让收入',
                    salesRevenue: 0
                }
            ]
        }
    },
    methods: {
        formatCurrency(value) {
            if (value === 0 || value === '0') {
                return '0'
            }
            return value.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 })
        },
        handleHomepage(row) {
            this.dialogVisible = true
            this.editingRow = row
        
            this.reviewForm = {
                firstReviewer: row?.firstReviewer || '',
                secondReviewer: row?.secondReviewer || '',
                thirdReviewer: row?.thirdReviewer || ''
            }
            this.$nextTick(() => {
                if (this.$refs.moduleTree) {
                    this.selectedModules = row?.modules || []
                    this.$refs.moduleTree.setCheckedKeys(this.selectedModules)
                }
            })
        },
        handleDialogCancel() {
            this.dialogVisible = false
        },
        resetDialog() {
            this.editingRow = null
            this.reviewForm = {
                firstReviewer: '',
                secondReviewer: '',
                thirdReviewer: ''
            }
            this.selectedModules = []
            if (this.$refs.moduleTree) {
                this.$refs.moduleTree.setCheckedKeys([])
            }
        },
        submitAudit() {
            const checkedKeys = this.$refs.moduleTree ? this.$refs.moduleTree.getCheckedKeys(true) : []
            const payload = {
                ...this.reviewForm,
                modules: checkedKeys,
                row: this.editingRow
            }
            console.log('保存审核配置', payload)
            this.$message.success('保存成功!')
            this.dialogVisible = false
        },
        handleDelete(row) {
            this.$confirm('确定要删除该产品吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                console.log('删除', row)
                // 处理删除逻辑
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },
        handleSizeChange() {
            // 处理分页大小变化
        },
        handleCurrentChange() {
            // 处理当前页变化
        }
    }
}
</script>
<style>
.el-tooltip__popper.is-dark {
    color: #F04B4B !important;
    background: #FCF1EB !important;
}

.el-tooltip__popper .popper__arrow {
    border-top-color: #FCF1EB !important;
}

.el-tooltip__popper .popper__arrow::after {
    border-top-color: #FCF1EB !important;
}

.el-tooltip__popper[x-placement^='bottom'] .popper__arrow {
    border-bottom-color: #FCF1EB !important;
}

.el-tooltip__popper[x-placement^='left'] .popper__arrow {
    border-left-color: #FCF1EB !important;
}

.el-tooltip__popper[x-placement^='right'] .popper__arrow {
    border-right-color: #FCF1EB !important;
}
</style>
<style scoped>
.areas {
    padding-bottom: 15px;
    width: 97%;
    margin: 0 auto;
    margin-top: 12px;
    padding-top: 20px;
}

.active {
    color: #5165f7 !important;
}

.heng {
    width: 20px;
    height: 6px;
    background: #5165f7;
    margin: 0 auto;
    margin-top: 6px;
}

.header-table {
    cursor: pointer;
}

.contentss-header {
    width: 100%;
    display: flex;
    justify-content: space-between;
    width: 150px;
}

.contentss {
    width: 97%;
    margin: 0 auto;
    padding-top: 12px;
}

.contents-area {
    width: 100%;
    background: white;
    margin-top: 20px;
}

.header-title {
    position: relative;
    top: 20px;
    left: 25px;
}

.numbers {
    position: relative;
    top: -7px;
    color: #2AB95F;
}

.number-name2 {
    margin-top: 8px;
    font-size: 14px;
    color: #979BBC;
}

.number-name {
    font-weight: bold;
    font-size: 25px;
    color: #17192C;
}

.header-names {
    display: flex;
    width: 82%;
    justify-content: space-between;
    margin: 0 auto;
    padding-top: 20px;
}

.header-area {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    width: 97%;
    margin: 0 auto;
}

.content-header {
    width: 100%;
    background: white;
    padding-bottom: 22px;
}

.footer {
    display: flex;
    justify-content: end;
    margin-top: 20px;
}

.tianjia {
    position: relative;
    top: -9px;
    background-color: #5065F6;
}

.tianjia2 {
    position: relative;
    top: -9px;
    background-color: #EDEFFE;
    color: #5065F6;
}

::v-deep .el-table thead {
    background-color: #EDEFFE !important;
    color: #363636 !important;
}

::v-deep .el-table__header th {
    background-color: #EDEFFE !important;
    color: #363636 !important;
}

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

::deep .el-form-item__label {
    font-size: 15px !important;
}

.selects {
    width: 100px;
    border: 1px solid #3333;
}

.headers-name {
    font-size: 15px;
    position: relative;
    top: 14px;
}

.headers {
    display: flex;
    justify-content: space-between;
}

::v-deep .el-table__body tr:hover > td {
    background-color: transparent !important;
}

::v-deep .el-table__body tr.el-table__row--striped td {
    background-color: transparent;
}

.audit-dialog ::v-deep .el-dialog {
    border-radius: 8px;
}

.audit-dialog ::v-deep .el-dialog__header {
    padding: 16px 24px;
    border-bottom: 1px solid #EBEEF5;
}

.audit-dialog ::v-deep .el-dialog__body {
    padding: 24px;
}

.audit-dialog ::v-deep .el-dialog__footer {
    padding: 12px 24px 20px;
    border-top: 1px solid #EBEEF5;
}

.dialog-title {
    font-size: 16px;
    font-weight: bold;
    color: #303133;
}

.dialog-section {
    margin-bottom: 24px;
}

.dialog-section-header {
    font-size: 14px;
    font-weight: 600;
    color: #363636;
    margin-bottom: 16px;
}

.reviewer-selects {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
}

.reviewer-item {
    display: flex;
    align-items: center;
    background: #F7F8FF;
    border: 1px solid #DCE0FD;
    border-radius: 6px;
    padding: 12px 16px;
    min-width: 240px;
}

.reviewer-label {
    font-size: 13px;
    color: #363636;
    margin-right: 12px;
    flex: 0 0 auto;
}

.reviewer-select {
    flex: 1;
}

.module-section {
       border-radius: 6px;
    margin-top: 23px;
}

.module-tree {
    max-height: 360px;
    overflow: auto;
    background: #fff;
    border: 1px solid #DCE0FD;
    border-radius: 6px;
    padding: 12px 16px;
}

.dialog-footer {
    text-align: center;
}

.dialog-footer .el-button + .el-button {
    margin-left: 16px;
}
.content-areas{
    display: flex;
    height: 50px;
    flex-wrap: wrap;
    margin-top: -1px;
}
.contents-left{
    padding-right: 20px;
    border: 1px solid #DCE0FD;
    background: #EDEFFE;
    line-height: 50px;
    font-size: 14px;
    color: #363636;
    display: flex;
    align-items: center;
}
.contents-right{
    flex: 1;
    border: 1px solid #DCE0FD;
    border-left: none;
    line-height: 50px;
    font-size: 14px;
    color: #363636;
    display: flex;
    align-items: center;
    position: relative;
}
.contents-right:last-child {
    border-right: 1px solid #DCE0FD;
}
.content-areas2{
    position: relative;
    top: -1px;
}
.rightss{
    height: 100%;
    border: 1px solid #DCE0FD;
    border-top: none;
    border-bottom: none;
    width: 100%;
    display: flex;
    align-items: center;
    flex: 1;
}
.rightss1{
    border-left: none !important;
    border-right: 1px solid #DCE0FD !important;
}
.rightss2{
    background: #EDEFFE;
    border-left: 1px solid #DCE0FD;
    border-right: 1px solid #DCE0FD;
    width: auto;
    min-width: 120px;
    flex: 0 0 auto;
    justify-content: center;
}
.rightss3{
    border-left: none !important;
    border-right: none !important;
    flex: 1;
}
.rightss-full{
    border-left: none !important;
    border-right: 1px solid #DCE0FD !important;
    flex: 1;
    width: 100%;
    min-width: 0;
    display: flex;
    align-items: center;
}
.table-name{
    margin-left: 20px;
}
</style>
