<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">
                                        <el-date-picker
                                         style="width: 140px;"
                                            v-model="form.year"
                                            type="year"
                                            placeholder="选择年度"
                                            format="yyyy"
                                            value-format="yyyy"
                                            @change="handleYearChange">
                                        </el-date-picker>
                                    </el-form-item>
                                    <el-form-item class="formitem">
                                        <el-cascader
                                            style="width: 140px;"
                                            v-model="periodValue"
                                            :options="periodOptions"
                                            size="small"
                                            class="cascader-select"
                                            @change="handlePeriodChange">
                                        </el-cascader>
                                    </el-form-item>
                                   
                                    <el-form-item>
                                        <el-button type="primary" @click="btnAdd" size="small" class="tianjia">添加产品</el-button>
                                    </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 type="index" label="序号" width="80" align="center" :index="formatIndex"></el-table-column>
                                <el-table-column prop="name" label="销售（合同）项目名称" align="center"></el-table-column>
                                <el-table-column prop="create_time" label="合同签订日期"  align="center">
                                    
                                </el-table-column>
                                <el-table-column prop="contract_money" label="合同金额（元）" width="150" align="center">
                                    <template slot-scope="scope">
                                        <span>{{ formatCurrency(scope.row.contract_money) }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="purchaser" label="业主单位（购买方）" align="center">
                                    
                                </el-table-column>
                                <el-table-column prop="sales_money" label="销售金额（元）" width="120" align="center">
                                    
                                </el-table-column>
                                <el-table-column fixed="right" label="操作" align="center" width="80">
                                    <template slot-scope="scope">
                                      
                                        <el-button @click="handleDelete(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="pageSize"
                                    layout="total, sizes, prev, pager, next, jumper" 
                                    :total="total">
                                </el-pagination>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
   
    </div>
</template>
<script>
import {list} from '@/api/businessScope/salesperformance.js'
export default {
    data() {
        return {
            active: 0,
            currentPage: 1,
            pageSize: 10,
            total: 0,
            dialogVisible: false,
            form: {
                year: '',
                isValid: ''
            },
            periodValue: [],
            periodOptions: [
                {
                    value: 1,
                    label: "全年"
                },
                {
                    value: 2,
                    label: "半年",
                    children: [
                        {
                            value: '1',
                            label: '上半年'
                        },
                        {
                            value: '2',
                            label: '下半年'
                        }
                    ]
                },
                {
                    value: 3,
                    label: "季度",
                    children: [
                        {
                            value: '1',
                            label: '第一季度'
                        },
                        {
                            value: '2',
                            label: '第二季度'
                        },
                        {
                            value: '3',
                            label: '第三季度'
                        },
                        {
                            value: '4',
                            label: '第四季度'
                        }
                    ]
                },
                {
                    value: 4,
                    label: "月度",
                    children: [
                        {
                            value: '1',
                            label: '1月'
                        },
                        {
                            value: '2',
                            label: '2月'
                        },
                        {
                            value: '3',
                            label: '3月'
                        },
                        {
                            value: '4',
                            label: '4月'
                        },
                        {
                            value: '5',
                            label: '5月'
                        },
                        {
                            value: '6',
                            label: '6月'
                        },
                        {
                            value: '7',
                            label: '7月'
                        },
                        {
                            value: '8',
                            label: '8月'
                        },
                        {
                            value: '9',
                            label: '9月'
                        },
                        {
                            value: '10',
                            label: '10月'
                        },
                        {
                            value: '11',
                            label: '11月'
                        },
                        {
                            value: '12',
                            label: '12月'
                        }
                    ]
                }
            ],
            isValidOptions: [
                {
                    value: 1,
                    label: "有效"
                },
                {
                    value: 2,
                    label: "无效"
                }
            ],
            productForm: {
                productName: '',
                isMainProduct: '',
                revenueType: '技术服务收入',
                technologySource: '',
                technologyField: '电子信息',
                technologyFieldDetail: '网络应用技术'
            },
            productFormRules: {
                productName: [
                    { required: true, message: '请输入产品(服务)名称', trigger: 'blur' }
                ],
                isMainProduct: [
                    { required: true, message: '请选择是否主要产品', trigger: 'change' }
                ],
                revenueType: [
                    { required: true, message: '请选择收入类型', trigger: 'change' }
                ]
            },
            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: '第四季度'
                }
            ],
            dialogRevenueTypeOptions: [
                {
                    value: '产品收入',
                    label: '产品收入'
                },
                {
                    value: '技术转让收入',
                    label: '技术转让收入'
                },
                {
                    value: '技术服务收入',
                    label: '技术服务收入'
                }
            ],
            technologySourceOptions: [
                {
                    value: '计算机产品及其网络应用技术',
                    label: '计算机产品及其网络应用技术'
                },
                {
                    value: '自主开发',
                    label: '自主开发'
                },
                {
                    value: '合作开发',
                    label: '合作开发'
                },
                {
                    value: '引进技术',
                    label: '引进技术'
                }
            ],
            technologyFieldOptions: [
                {
                    value: '电子信息',
                    label: '电子信息'
                },
                {
                    value: '生物与新医药',
                    label: '生物与新医药'
                },
                {
                    value: '航空航天',
                    label: '航空航天'
                },
                {
                    value: '新材料',
                    label: '新材料'
                },
                {
                    value: '高技术服务',
                    label: '高技术服务'
                },
                {
                    value: '新能源与节能',
                    label: '新能源与节能'
                },
                {
                    value: '资源与环境',
                    label: '资源与环境'
                },
                {
                    value: '先进制造与自动化',
                    label: '先进制造与自动化'
                }
            ],
            technologyFieldDetailOptions: [
                {
                    value: '网络应用技术',
                    label: '网络应用技术'
                },
                {
                    value: '计算机软件技术',
                    label: '计算机软件技术'
                },
                {
                    value: '通信技术',
                    label: '通信技术'
                },
                {
                    value: '其他',
                    label: '其他'
                }
            ],
            tableData: []
        }
    },
    mounted() {
        this.getList()
    },
    methods: {
        // 获取列表数据
        getList() {
            const params = {
                year: this.form.year || '',
                period: this.getPeriodValue(),
                isValid: this.form.isValid || '',
                page: this.currentPage,
                limit: this.pageSize
            }
            list(params).then((res) => {
                if (res && res.code === 1 && res.data) {
                    this.tableData = res.data.data || []
                    this.total = res.data.total || 0
                } else {
                    this.$message.error(res?.msg || '获取数据失败')
                    this.tableData = []
                    this.total = 0
                }
                console.log('ererer',this.tableData)
            }).catch((error) => {
                console.error('获取列表数据失败:', error)
                this.$message.error('获取数据失败')
                this.tableData = []
                this.total = 0
            })
        },
        // 获取级联选择器的值
        getPeriodValue() {
            if (!this.periodValue || this.periodValue.length === 0) return ''
            // 如果选择的是全年，返回1
            if (this.periodValue.length === 1 && this.periodValue[0] === 1) return '1'
            // 如果是其他选项，返回对应的值
            return this.periodValue.join(',')
        },
        // 年度选择变化
        handleYearChange() {
            this.currentPage = 1
            this.getList()
        },
        // 时间段选择变化
        handlePeriodChange() {
            this.currentPage = 1
            this.getList()
        },
        // 是否有效变化
        handleIsValidChange() {
            this.currentPage = 1
            this.getList()
        },
        // 格式化序号
        formatIndex(index) {
            return (this.currentPage - 1) * this.pageSize + index + 1
        },
        formatCurrency(value) {
            // 处理 undefined、null、空字符串等情况
            if (value === null || value === undefined || value === '') {
                return '0'
            }
            // 转换为数字
            const numValue = Number(value)
            // 如果转换失败或为 NaN，返回 '0'
            if (isNaN(numValue)) {
                return '0'
            }
            // 如果是 0，返回 '0'
            if (numValue === 0) {
                return '0'
            }
            // 格式化数字
            return numValue.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 })
        },
        handleHomepage(row) {
       
             this.$router.push('/productServiceDetail')
            // 跳转到产品主页
        },
        handleDelete(row) {
            // 跳转到详情页面
            this.$router.push({
                path: '/home/salesperformanceDetial',
                query: { id: row.id }
            })
        },
        btnAdd() {
           this.$router.push('/salesperformanceEdint')
        },
        handleSave() {
            this.$refs.productForm.validate((valid) => {
                if (valid) {
                    console.log('保存产品', this.productForm)
                    // 处理保存逻辑
                    this.$message({
                        type: 'success',
                        message: '保存成功!'
                    })
                    this.dialogVisible = false
                    // 这里可以添加保存后的逻辑，比如刷新列表
                } else {
                    console.log('表单验证失败')
                    return false
                }
            })
        },
        handleCancel() {
            this.dialogVisible = false
            // 重置表单
            this.productForm = {
                productName: '',
                isMainProduct: '',
                revenueType: '技术服务收入',
                technologySource: '',
                technologyField: '电子信息',
                technologyFieldDetail: '网络应用技术'
            }
            if (this.$refs.productForm) {
                this.$refs.productForm.clearValidate()
            }
        },
        handleSizeChange(val) {
            this.pageSize = val
            this.currentPage = 1
            this.getList()
        },
        handleCurrentChange(val) {
            this.currentPage = val
            this.getList()
        }
    }
}
</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;
    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;
}
.cascader-select {
    width: 120px;
}
::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;
}
::v-deep .el-cascader .el-input__inner {
    border: 1px solid #DCDFE6 !important;
}

.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;
}

/* 产品对话框样式 */
.product-dialog ::v-deep .el-dialog__header {
    padding: 20px 20px 10px;
    border-bottom: 1px solid #EBEEF5;
    text-align: center;
}

.product-dialog ::v-deep .el-dialog__title {
    font-size: 16px;
    font-weight: bold;
    color: #303133;
}

.product-dialog ::v-deep .el-dialog__body {
    padding: 20px;
}

.product-dialog ::v-deep .el-form-item__label {
    font-size: 14px;
    color: #606266;
}

.product-dialog .dialog-footer {
    text-align: center;
    padding-top: 10px;
}

.product-dialog .dialog-footer .el-button {
    margin: 0 10px;
}
.content-areas{
    display: flex;
    height: 50px;
    flex-wrap: wrap;
    margin-top: -1px;
}
.contents-left{
    width: 30%;
    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>
