<template>
    <div class="charts_area">
        <el-card shadow="never">
            <template #header>
                <div class="top_nav flex">
                    <template v-for="(item,index) in headerNav">
                        <div class="nav_list" @click="changeNav(index)">
                            {{ item.label }}
                            <i :class="navIndex == index ? 'on' : ''"></i>
                        </div>
                    </template>
                </div>
            </template>
            <div class="search">
                <el-form :model="form" ref="queryFormRef" :inline="true">
                    <el-form-item prop="rangeTime">
                        <el-date-picker type="monthrange" v-model="rangeTime" :clearable="false" range-separator="-"
                            start-placeholder="请选择月份区间" end-placeholder="请选择月份区间" value-format="YYYY-MM"
                            @change="changeTime" />
                    </el-form-item>
                    <!-- <el-form-item prop="deptIds">
                        <SelectOrg authorty  clearable :inputWidth="215" multiple v-model="form.deptIds" :type="3"></SelectOrg>
                    </el-form-item> -->
                    <el-form-item prop="ownerByIds">
                        <SelectUser authorty multiple v-model="form.ownerByIds"></SelectUser>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-card>
        <template v-if="navIndex == 0">
            <div class="echarts-body">
                <Echart :height="450" :options="echartsOption" />
            </div>
            <div class="table-body">
                <Table :tableParams="tableParams" :isCheckbox='false' :rightBtn="[]" :btnRole="[]" :isPagination="false"
                    :tableData="tableData">
                    <template #left>
                        <div>商机数量：{{ salesProcessData?.businessNumTotal }}，
                            预计销售金额：¥ {{ salesProcessData?.salesAmountTotal }}，
                            概率金额：¥ {{ salesProcessData?.expectedSalesAmountTotal }}
                        </div>
                    </template>
                    <!-- <template #right>
                        <el-button style="margin-left: 10px;">导出</el-button>
                    </template> -->
                </Table>
            </div>
        </template>
        <div class="table-body" style="margin-top: 16px;" v-else>
            <Table ref="salesProcessTable" :tableParams="detailTableParams" :isCheckbox='false' :rightBtn="[]"
                :btnRole="[]">
                <template #left>
                    <div>商机数量: {{ salesProcessData?.businessNumTotal }}，
                        预计销售金额：¥ {{ salesProcessData?.salesAmountTotal }}，
                        概率金额：¥ {{ salesProcessData?.expectedSalesAmountTotal }}
                    </div>
                </template>
                <!-- <template #right>
                    <el-button style="margin-left: 10px;">导出</el-button>
                </template> -->
            </Table>
        </div>
    </div>
</template>

<script setup name="completionDegree" lang="ts">
import moment from 'moment'
import Table from '@/components/Table/index.vue'
import { tableTypes } from '@/components/Table/types'
import { getSalesProcessTotal, listSalesProcess } from '@/api/statistics/salesAnalysis'
import { SalesProcessQuery, SalesProcessVO } from '@/api/statistics/salesAnalysis/types';
import useUserStore from '@/store/modules/user';

// 顶部类型切换
const headerNav = [
    { label: '预测分析', value: '0' },
    { label: '预测报表', value: '10' },
]
const navIndex = ref(0)
const changeNav = (index: number) => {
    navIndex.value = index
    if(index == 0) {
        getData()
    }
}

const nowYear = moment().format('YYYY')
const rangeTime = ref([nowYear + '-01', nowYear + '-12'])
const changeTime = (val) => {
    form.value.beginCycleTime = val ? val[0] + '-01' : ''
    form.value.endCycleTime = val ? moment(val[1]).endOf('month').format('YYYY-MM-DD') : ''
}
const { userInfo } = useUserStore();
const form = ref({
    // deptIds: [userInfo.deptId],
    ownerByIds: [userInfo.id],
    beginCycleTime: nowYear + '-01-01',
    endCycleTime: nowYear + '-12-31'
})

const tableData = ref([])
const salesProcessData = ref()
const getData = () => {
    getSalesProcessTotal(form.value).then(res => {
        if(res.success) {
            salesProcessData.value = res.result
            tableData.value = res.result.businessMonthCountls
            echartsOption.xAxis.data = tableData.value.map(item => item.month)
            echartsOption.series = [
                {
                    name: '预计销售金额',
                    type: 'bar',
                    barWidth: 18,
                    color:'#3F9EFF',
                    itemStyle: {
                        borderRadius: [4, 4, 0, 0] // 这里设置圆角的大小
                    },
                    data: tableData.value.map(item => item.salesAmount)
                },
                {
                    name: '概率金额',
                    type: 'bar',
                    barWidth: 18,
                    itemStyle: {
                        color:'#58CC74',
                        borderRadius: [4, 4, 0, 0] // 这里设置圆角的大小
                    },
                    data: tableData.value.map(item => item.expectedSalesAmount)
                },
            ]
        }
    })
}

const echartsOption = reactive({
    legend: {
        top: 'bottom'
    },
    tooltip: {},
    grid: {
        left: '16px',    // 组件离容器左侧的距离
        right: '16px',   // 组件离容器右侧的距离
        bottom: '36px',  // 组件离容器底部的距离
        top: '16px',    // 组件离容器顶部的距离
        containLabel: true // 如果为true，则grid 区域会根据标签大小自动调整
    },
    xAxis: {
        type: 'category',
        axisLabel: {
            interval: 0,
            color: '#333', //坐标轴字体颜色
        },
        axisLine: {
            lineStyle: {
                color: '#EBEFF3' // 修改x轴颜色
            }
        },
        data: []
    },
    yAxis: {},
    series: []
})

const queryFormRef = ref<ElFormInstance>();

const tableParams = reactive<tableTypes<SalesProcessQuery, SalesProcessVO>>({
    tableColumn: [
        { show: true, label: '时间', prop: 'month' },
        { show: true, label: '商机数量', prop: 'businessNum' },
        { show: true, label: '预计销售金额', prop: 'salesAmount' },
        { show: true, label: '概率金额', prop: 'expectedSalesAmount', headerQuestionFilled: { content: '预计销售金额*成交机率', width: 180 } }
    ]
})

const detailTableParams = reactive<tableTypes<SalesProcessQuery, SalesProcessVO>>({
    tableColumn: [
        { show: true, label: '商机标题', prop: 'title' },
        { show: true, label: '对应客户', prop: 'customerName' },
        { show: true, label: '预计签单日期', prop: 'expectedSigningDate' },
        { show: true, label: '预计销售金额', prop: 'estimatedSalesAmount' },
        { show: true, label: '销售阶段', prop: 'stageName' },
        { show: true, label: '成交机率', prop: 'stransactionProbabilityName' },
        { show: true, label: '概率金额', prop: 'expectedSalesAmount' },
        { show: true, label: '负责人员', prop: 'ownerByName' }
    ],
    queryParams: form.value,
    listFn: listSalesProcess
})

const salesProcessTable = ref()
/** 搜索按钮操作 */
const handleQuery = () => {
    getData()
    if(navIndex.value == 1) {
        salesProcessTable.value?.getData()
    }
}

/** 重置按钮操作 */
const resetQuery = () => {
    queryFormRef.value?.resetFields();
    rangeTime.value = [nowYear + '-01', nowYear + '-12']
    form.value.beginCycleTime = nowYear + '-01-01'
    form.value.endCycleTime = nowYear + '-12-31'
    handleQuery();
}

onBeforeMount(() => {
    getData()
})
</script>
