<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.value)">
                            {{ 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="targetType">
                        <el-select v-model="form.targetType" @change="handleQuery">
                            <el-option v-for="item in typeData" :label="item.label" :value="item.value"></el-option>
                        </el-select>
                    </el-form-item>
                    <SelectTime ref="selectTimeRef" v-model="summaryType" v-model:query-params="form" />
                    <!-- <el-form-item prop="deptIds" v-if="navIndex == 1">
                        <SelectOrg authorty clearable :inputWidth="215" multiple v-model="form.deptIds" :type="3"></SelectOrg>
                    </el-form-item> -->
                    <el-form-item prop="ownerByIds" v-if="navIndex == 0">
                        <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>
        <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" :key="navIndex">
                <template #left>
                    <div>
                        线索数量：{{ newBusinessData?.clusNumTotal }}，
                        商机数量：{{ newBusinessData?.businessNumTotal }}，
                        客户数量：{{ newBusinessData?.customerNumTotal }}，
                        订单数量：{{ newBusinessData?.orderFormNumTotal }}
                    </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 { getNewBusinessRanking } from '@/api/statistics/salesAnalysis'
import { SalesProcessQuery, SalesProcessVO } from '@/api/statistics/salesAnalysis/types';
import SelectTime from '../../components/selectTime.vue';
import useUserStore from '@/store/modules/user';

// 顶部类型切换
const headerNav = [
    { label: '个人排名', value: '80' },
    { label: '部门排名', value: '70' },
]
const navIndex = ref(0)

const typeData = ref([
    { label: '客户', value: '10', type: 'customerNum' },
    { label: '线索', value: '11', type: 'clusNum' },
    { label: '商机', value: '30', type: 'businessNum' },
    { label: '订单', value: '40', type: 'orderFormNum' },
])
const summaryType = ref('30')
const { userInfo } = useUserStore();
const form = ref({
    deptIds: [],
    ownerByIds: [userInfo.id],
    beginCycleTime: moment().subtract(12, 'months').format('YYYY-MM') + '-01',
    endCycleTime: moment().endOf('month').format('YYYY-MM-DD'),
    summaryType: '80',
    targetType: '10'
})

const tableData = ref([])
const newBusinessData = ref()
const getData = () => {
    let params = JSON.parse(JSON.stringify(form.value))
    if(params.summaryType == '80') {
        delete params.deptIds
    } else {
        delete params.ownerByIds
    }
    getNewBusinessRanking(params).then(res => {
        if(res.success) {
            newBusinessData.value = res.result
            tableData.value = res.result.datas
            let xAxisData = res.result.datas.map(item => form.value.summaryType == '80' ? item.groupType : item.deptName)
            echartsOption.xAxis.data = xAxisData
            echartsOption.series[0].name = typeData.value.find(a => a.value == form.value.targetType)?.label
            echartsOption.series[0].data = res.result.datas.map(item => item[typeData.value.find(a => a.value == form.value.targetType)?.type])
        }
    })
}

const echartsOption = reactive({
    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: [
        {
            name: '',
            type: 'bar',
            barWidth: 18,
            color:'#3F9EFF',
            itemStyle: {
                borderRadius: [4, 4, 0, 0] // 这里设置圆角的大小
            },
            data: []
        }
    ]
})

const queryFormRef = ref<ElFormInstance>();

const tableParams = reactive<tableTypes<SalesProcessQuery, SalesProcessVO>>({
    tableColumn: []
})

const { tableColumn } = toRefs(tableParams)

const changeNav = (index: number, val: string) => {
    navIndex.value = index
    form.value.summaryType = val
    tableColumn.value = [
        { show: true, label: '排名', prop: 'index', render: ({ index }) => {
            return index + 1
        }},
        { show: index == 0, label: '员工姓名', prop: 'groupType' },
        { show: index == 0, label: '所在部门', prop: 'deptName' },
        { show: index == 1, label: '部门名称', prop: 'deptName' },
        { show: true, label: '线索数量', prop: 'clusNum' },
        { show: true, label: '商机数量', prop: 'businessNum' },
        { show: true, label: '客户数量', prop: 'customerNum' },
        { show: true, label: '订单数量', prop: 'orderFormNum' }
    ]
    getData()
}
changeNav(0, '80')

/** 搜索按钮操作 */
const handleQuery = () => {
    getData()
}

/** 重置按钮操作 */
const selectTimeRef = ref()
const resetQuery = () => {
    queryFormRef.value?.resetFields();
    form.value.beginCycleTime = moment().subtract(12, 'months').format('YYYY-MM') + '-01'
    form.value.endCycleTime = moment().endOf('month').format('YYYY-MM-DD')
    selectTimeRef.value?.resetTime()
    handleQuery();
}
</script>
