<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-select v-model="form.revenueType" clearable  placeholder="请选择" class="selects" style="width: 140px;" @change="handleRevenueTypeChange">
                                            <el-option v-for="item in typeDate" :key="item.id" :label="item.title"
                                                :value="item.id">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                    <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 label="时间段选择" size="small">
                                        <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 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="200" align="center">
                                    <template slot-scope="scope">
                                        <span>{{ scope.row.revenueType }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="sale_income" label="销售收入(元)" align="center"  width="150">
                                    <template slot-scope="scope">
                                        <span>{{ formatCurrency(scope.row.sale_income || scope.row.salesRevenue || 0) }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column fixed="right" label="操作" align="center" width="150">
                                    <template slot-scope="scope">
                                        <el-button @click="handleHomepage(scope.row)" type="text" size="small" style="color: #5165f7;">主页</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="[100, 200, 300, 400]" 
                                    :page-size="pageSize"
                                    layout="total, sizes, prev, pager, next, jumper" 
                                    :total="total">
                                </el-pagination>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 添加编辑产品对话框 -->
        <el-dialog
            title="添加编辑产品"
            :visible.sync="dialogVisible"
            width="800px"
            :close-on-click-modal="false"
            class="product-dialog">
            <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-input v-model="productForm.name" placeholder="请输入内容"></el-input></span>
                         </div>
                       </div>
                       <div class="content-areas content-areas2" style="top: 1px;">
                         <div class="contents-left">
                           <span class="table-name">收入类型</span>
                         </div>
                         <div class="contents-right">
                            <div class="rightss rightss1"><span class="table-name">
                                <el-select v-model="productForm.income_type" placeholder="请选择" class="no-border-select">
                                <el-option
                                v-for="item in typeDate"
                                :key="item.id"
                                :label="item.title"
                                :value="item.id">
                                </el-option>
                            </el-select>
                            </span></div>
                            <div class="rightss rightss2"><span class="table-name" style="margin-left: 0;">是否主要产品</span></div>
                            <div class="rightss rightss3" ><span class="table-name">
                                <el-select v-model="productForm.is_main" placeholder="请选择" class="no-border-select">
                                <el-option
                                v-for="item in changpingDate"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>
                            </span></div>
                         </div>
                       </div>
                       <div class="content-areas content-areas2" style="top: 1px;">
                         <div class="contents-left" >
                           <span class="table-name" >技术来源</span>
                         </div>
                         <div class="contents-right">
                            <div class="rightss rightss-full">
                                <el-select v-model="productForm.tech_source" placeholder="请选择" class="no-border-select" style="width: calc(100% - 40px); margin: 0 20px;" clearable>
                                <el-option
                                v-for="item in jishuDate"
                                :key="item.id"
                                :label="item.title"
                                :value="item.id">
                                </el-option>
                            </el-select>
                            </div>
                         </div>
                       </div>
                       <div class="content-areas content-areas2" style="top: 1px;">
                         <div class="contents-left" >
                           <span class="table-name">技术领域</span>
                         </div>
                         <div class="contents-right">
                            <div class="rightss rightss-full">
                                <el-select v-model="productForm.tech_area_one" placeholder="请选择" class="no-border-select" style="width: calc(100% - 40px); margin: 0 20px;" clearable>
                                <el-option
                                v-for="item in lingyuDate"
                                :key="item.id"
                                :label="item.title"
                                :value="item.id">
                                </el-option>
                            </el-select>
                            </div>
                         </div>
                       </div>
                    </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="handleCancel">取消</el-button>
                <el-button type="primary" @click="handleSave" style="background-color: #5165f7;">添加保存</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { all,list,save } from '@/api/businessScope/productService'
export default {
    data() {
        return {
            typeDate:[],
            active: 0,
            currentPage: 1,
            pageSize: 10,
            total: 0,
            dialogVisible: false,
            form: {
                income_type: '',
                year: ''
            },
            changpingDate:[
                {
                    label:'是',
                    value:1,
                },
                {
                    label:'否',
                    value:2,
                }
            ], //产品数据
            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月'
                        }
                    ]
                }
            ],
            productForm: {
                name: '',
                income_type: '',
                is_main: '',
                tech_source: '',
                tech_area_one: ''
            },
            revenueTypeOptions: [
                {
                    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: [],
            jishuDate:[],
            lingyuDate:[],
        }
    },
    mounted(){
        this.getall()
        this.getList()
        this.getall2()
        this.getall3()
    },
    methods: {
        //获取技术来源
        getall2(){
            all({dict_type_id:17}).then((res)=>{
                this.jishuDate=res.data
            }) 
        },
        //获取技术领域
        getall3(){
            all({dict_type_id:16}).then((res)=>{
                this.lingyuDate=res.data
            }) 
        },
        //获取收入类型
        getall(){
            all({dict_type_id:5}).then((res)=>{
                this.typeDate=res.data
            })
        },
        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 })
        },
        // 获取级联选择器的值
        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(',')
        },
        // 获取列表数据
        getList() {
            const params = {
                income_type: this.form.revenueType || '',
                year: this.form.year || '',
                period: this.getPeriodValue(),
                page: this.currentPage,
                limit: this.pageSize
            }
            list(params).then((res) => {
                if (res && res.code === 1 && res.data) {
                    // 映射后端数据到前端表格
                    this.tableData = (res.data.data || []).map((item, index) => {
                        return {
                            id: item.id,
                            serial: (this.currentPage - 1) * this.pageSize + index + 1,
                            productName: item.name || item.product_name || '',
                            isMainProduct: item.is_main_product === 1 || item.is_main_product === '1' ? '是' : '否',
                            revenueType: item.income_type_name || item.revenue_type || '',
                            salesRevenue: item.sales_revenue || item.sales_money || 0,
                            sale_income: item.sale_income || item.sales_revenue || item.sales_money || 0
                        }
                    })
                    this.total = res.data.total || 0
                } else {
                    this.$message.error(res?.msg || '获取数据失败')
                    this.tableData = []
                    this.total = 0
                }
            }).catch((error) => {
                console.error('获取列表数据失败:', error)
                this.$message.error('获取数据失败')
                this.tableData = []
                this.total = 0
            })
        },
        // 年度选择变化
        handleYearChange() {
            this.currentPage = 1
            this.getList()
        },
        // 时间段选择变化
        handlePeriodChange() {
            this.currentPage = 1
            this.getList()
        },
        // 收入类型变化
        handleRevenueTypeChange() {
            this.currentPage = 1
            this.getList()
        },
        handleHomepage(row) {
       
             this.$router.push('/productServiceDetail')
            // 跳转到产品主页
        },
        handleDelete(row) {
            this.$confirm('确定要删除该产品吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                console.log('删除', row)
                // 处理删除逻辑
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },
        btnAdd() {
            this.dialogVisible = true
            // 重置表单
            this.productForm = {
                name: '',
                income_type: '',
                is_main: '',
                tech_source: '',
                tech_area_one: ''
            }
        },
        handleSave() {
            // 表单验证
            if (!this.productForm.name || this.productForm.name.trim() === '') {
                this.$message.error('请输入产品（服务）名称')
                return
            }
            if (!this.productForm.income_type) {
                this.$message.error('请选择收入类型')
                return
            }
            if (!this.productForm.is_main) {
                this.$message.error('请选择是否主要产品')
                return
            }
            
            // 构建提交参数
            const params = {
                name: this.productForm.name.trim(),
                income_type: this.productForm.income_type,
                is_main: this.productForm.is_main
            }
            
            // 可选字段
            if (this.productForm.tech_source) {
                params.tech_source = this.productForm.tech_source
            }
            if (this.productForm.tech_area_one) {
                params.tech_area_one = this.productForm.tech_area_one
            }
            
            // 调用保存接口
            save(params).then((res) => {
                if (res && res.code === 1) {
                    this.$message({
                        type: 'success',
                        message: '保存成功!'
                    })
                    this.dialogVisible = false
                    // 刷新列表
                    this.getList()
                } else {
                    this.$message.error(res?.msg || '保存失败')
                }
            }).catch((error) => {
                console.error('保存产品失败:', error)
                this.$message.error('保存失败，请稍后重试')
            })
        },
        handleCancel() {
            this.dialogVisible = false
            // 重置表单
            this.productForm = {
                name: '',
                income_type: '',
                is_main: '',
                tech_source: '',
                tech_area_one: ''
            }
        },
        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;
}
::deep .el-form-item__label {
    font-size: 15px !important;
}

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


::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;
}
/* 移除对话框中下拉框的边框 */
.product-dialog .no-border-select ::v-deep .el-input__inner {
    border: none !important;
}
.product-dialog .no-border-select ::v-deep .el-input {
    border: none !important;
}
.product-dialog .no-border-cascader ::v-deep .el-input__inner {
    border: none !important;
}
.product-dialog .no-border-cascader ::v-deep .el-input {
    border: none !important;
}
</style>
