<template>
    <div class="main-container">
        <el-card class="box-card !border-none" shadow="never">

            <div class="flex justify-between items-center">
                <span class="text-lg">{{pageName}}</span>
                <el-button type="primary" @click="addEvent">
                    {{ t('addSales') }}
                </el-button>
            </div>

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="salesTable.searchParam" ref="searchFormRef">
                    <el-form-item :label="t('studentPhone')" prop="student_phone">
                        <el-input v-model="salesTable.searchParam.student_phone" :placeholder="t('studentPhonePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('studentName')" prop="student_name">
                        <el-input v-model="salesTable.searchParam.student_name" :placeholder="t('studentNamePlaceholder')" />
                    </el-form-item>
                    
                    <el-form-item :label="t('sex')" prop="sex">
                        <el-select class="w-[280px]" v-model="salesTable.searchParam.sex" clearable :placeholder="t('sexPlaceholder')">
                            <el-option label="全部" value=""></el-option>
                            <el-option
                                v-for="(item, index) in sexList"
                                :key="index"
                                :label="item.name"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    
                    <el-form-item :label="t('age')" prop="age">
                        <range-input v-model="salesTable.searchParam.age"/>
                    </el-form-item>
                    
                    <el-form-item :label="t('schoolName')" prop="school_name">
                        <el-input v-model="salesTable.searchParam.school_name" :placeholder="t('schoolNamePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('grade')" prop="grade">
                        <el-input v-model="salesTable.searchParam.grade" :placeholder="t('gradePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('className')" prop="class_name">
                        <el-input v-model="salesTable.searchParam.class_name" :placeholder="t('classNamePlaceholder')" />
                    </el-form-item>
                    
                    <el-form-item :label="t('sourceChannel')" prop="source_channel">
                        <el-select class="w-[280px]" v-model="salesTable.searchParam.source_channel" clearable :placeholder="t('sourceChannelPlaceholder')">
                            <el-option label="全部" value=""></el-option>
                            <el-option
                                v-for="(item, index) in source_channelList"
                                :key="index"
                                :label="item.name"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    
                    
                    <el-form-item :label="t('customerSource')" prop="customer_source">
                        <el-select class="w-[280px]" v-model="salesTable.searchParam.customer_source" clearable :placeholder="t('customerSourcePlaceholder')">
                            <el-option label="全部" value=""></el-option>
                            <el-option
                                v-for="(item, index) in customer_sourceList"
                                :key="index"
                                :label="item.name"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    
                    
                    <el-form-item :label="t('addStaffId')" prop="add_staff_id">
                        <el-select class="w-[280px]" v-model="salesTable.searchParam.add_staff_id" clearable :placeholder="t('addStaffIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in addStaffIdList"
                                       :key="index"
                                       :label="item['name']"
                                       :value="item['id']"
                                   />
                        </el-select>
                    </el-form-item>
                    
                    
                    <el-form-item :label="t('getStaffId')" prop="get_staff_id">
                        <el-select class="w-[280px]" v-model="salesTable.searchParam.get_staff_id" clearable :placeholder="t('getStaffIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in getStaffIdList"
                                       :key="index"
                                       :label="item['name']"
                                       :value="item['id']"
                                   />
                        </el-select>
                    </el-form-item>
                    
                    <el-form-item :label="t('contactName')" prop="contact_name">
                        <el-input v-model="salesTable.searchParam.contact_name" :placeholder="t('contactNamePlaceholder')" />
                    </el-form-item>
                    
                    <el-form-item :label="t('provinceId')" prop="province_id">
                        <el-select class="w-[280px]" v-model="salesTable.searchParam.province_id" clearable :placeholder="t('provinceIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in provinceIdList"
                                       :key="index"
                                       :label="item['name']"
                                       :value="item['id']"
                                   />
                        </el-select>
                    </el-form-item>
                    
                    
                    <el-form-item :label="t('cityId')" prop="city_id">
                        <el-select class="w-[280px]" v-model="salesTable.searchParam.city_id" clearable :placeholder="t('cityIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in cityIdList"
                                       :key="index"
                                       :label="item['name']"
                                       :value="item['id']"
                                   />
                        </el-select>
                    </el-form-item>
                    
                    
                    <el-form-item :label="t('districtId')" prop="district_id">
                        <el-select class="w-[280px]" v-model="salesTable.searchParam.district_id" clearable :placeholder="t('districtIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in districtIdList"
                                       :key="index"
                                       :label="item['name']"
                                       :value="item['id']"
                                   />
                        </el-select>
                    </el-form-item>
                    
                    <el-form-item :label="t('communityName')" prop="community_name">
                        <el-input v-model="salesTable.searchParam.community_name" :placeholder="t('communityNamePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('createTime')" prop="create_time">
                        <el-date-picker v-model="salesTable.searchParam.create_time" type="datetimerange" format="YYYY-MM-DD hh:mm:ss"
                            :start-placeholder="t('startDate')" :end-placeholder="t('endDate')" />
                    </el-form-item>
                    

                    <el-form-item>
                        <el-button type="primary" @click="loadSalesList()">{{ t('search') }}</el-button>
                        <el-button @click="resetForm(searchFormRef)">{{ t('reset') }}</el-button>
                    </el-form-item>
                </el-form>
            </el-card>

            <div class="mt-[10px]">
                <el-table :data="salesTable.data" size="large" v-loading="salesTable.loading">
                    <template #empty>
                        <span>{{ !salesTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="id" :label="t('id')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="student_phone" :label="t('studentPhone')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="student_name" :label="t('studentName')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column :label="t('sex')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in sexList">
                                <div v-if="item.value == row.sex">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    
                    <el-table-column prop="age" :label="t('age')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="school_name" :label="t('schoolName')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="grade" :label="t('grade')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="class_name" :label="t('className')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column :label="t('sourceChannel')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in source_channelList">
                                <div v-if="item.value == row.source_channel">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    
                    <el-table-column :label="t('customerSource')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in customer_sourceList">
                                <div v-if="item.value == row.customer_source">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    
                    <el-table-column prop="add_staff_id_name" :label="t('addStaffId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="get_staff_id_name" :label="t('getStaffId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="contact_name" :label="t('contactName')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="province_id_name" :label="t('provinceId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="city_id_name" :label="t('cityId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="district_id_name" :label="t('districtId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="community_name" :label="t('communityName')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column :label="t('customerTags')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in customer_tagsList">
                                <div v-if="item.value == row.customer_tags">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    
                     <el-table-column :label="t('createTime')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            {{ row.create_time || '' }}
                        </template>
                    </el-table-column>
                    
                     <el-table-column :label="t('updateTime')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            {{ row.update_time || '' }}
                        </template>
                    </el-table-column>
                    

                    <el-table-column :label="t('operation')" fixed="right" min-width="120">
                       <template #default="{ row }">
                           <el-button type="primary" link @click="editEvent(row)">{{ t('edit') }}</el-button>
                           <el-button type="primary" link @click="deleteEvent(row.id)">{{ t('delete') }}</el-button>
                       </template>
                    </el-table-column>

                </el-table>
                <div class="mt-[16px] flex justify-end">
                    <el-pagination v-model:current-page="salesTable.page" v-model:page-size="salesTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="salesTable.total"
                        @size-change="loadSalesList()" @current-change="loadSalesList" />
                </div>
            </div>

            
        </el-card>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from 'vue'
import { t } from '@/lang'
import { useDictionary } from '@/app/api/dict'
import { getSalesList, deleteSales, getWithStaffList, getWithStaffList, getWithSysAreaList, getWithSysAreaList, getWithSysAreaList } from '@/addon/zhjw/api/sales'
import { img } from '@/utils/common'
import { ElMessageBox,FormInstance } from 'element-plus'
import { useRouter } from 'vue-router'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageName = route.meta.title;

let salesTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "student_phone":"",
      "student_name":"",
      "sex":"",
      "age":[],
      "school_name":"",
      "grade":"",
      "class_name":"",
      "source_channel":"",
      "customer_source":"",
      "add_staff_id":"",
      "get_staff_id":"",
      "contact_name":"",
      "province_id":"",
      "city_id":"",
      "district_id":"",
      "community_name":"",
      "customer_tags":"",
      "create_time":""
    }
})

const searchFormRef = ref<FormInstance>()

// 选中数据
const selectData = ref<any[]>([])

// 字典数据
    const sexList = ref([] as any[])
    const sexDictList = async () => {
    sexList.value = await (await useDictionary('users_sex')).data.dictionary
    }
    sexDictList();
    const source_channelList = ref([] as any[])
    const source_channelDictList = async () => {
    source_channelList.value = await (await useDictionary('source_channel')).data.dictionary
    }
    source_channelDictList();
    const customer_sourceList = ref([] as any[])
    const customer_sourceDictList = async () => {
    customer_sourceList.value = await (await useDictionary('customer_source')).data.dictionary
    }
    customer_sourceDictList();
    const customer_tagsList = ref([] as any[])
    const customer_tagsDictList = async () => {
    customer_tagsList.value = await (await useDictionary('customer_tags')).data.dictionary
    }
    customer_tagsDictList();


/**
 * 获取销售管理列表
 */
const loadSalesList = (page: number = 1) => {
    salesTable.loading = true
    salesTable.page = page

    getSalesList({
        page: salesTable.page,
        limit: salesTable.limit,
         ...salesTable.searchParam
    }).then(res => {
        salesTable.loading = false
        salesTable.data = res.data.data
        salesTable.total = res.data.total
    }).catch(() => {
        salesTable.loading = false
    })
}
loadSalesList()

const router = useRouter()

/**
 * 添加销售管理
 */
const addEvent = () => {
    router.push('/sales/sales_edit')
}

/**
 * 编辑销售管理
 * @param data
 */
const editEvent = (data: any) => {
    router.push('/sales/sales_edit?id='+data.id)
}

/**
 * 删除销售管理
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('salesDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteSales(id).then(() => {
            loadSalesList()
        }).catch(() => {
        })
    })
}

    
    const addStaffIdList = ref([])
    const setAddStaffIdList = async () => {
    addStaffIdList.value = await (await getWithStaffList({})).data
    }
    setAddStaffIdList()
    const getStaffIdList = ref([])
    const setGetStaffIdList = async () => {
    getStaffIdList.value = await (await getWithStaffList({})).data
    }
    setGetStaffIdList()
    const provinceIdList = ref([])
    const setProvinceIdList = async () => {
    provinceIdList.value = await (await getWithSysAreaList({})).data
    }
    setProvinceIdList()
    const cityIdList = ref([])
    const setCityIdList = async () => {
    cityIdList.value = await (await getWithSysAreaList({})).data
    }
    setCityIdList()
    const districtIdList = ref([])
    const setDistrictIdList = async () => {
    districtIdList.value = await (await getWithSysAreaList({})).data
    }
    setDistrictIdList()

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
    loadSalesList()
}
</script>

<style lang="scss" scoped>
/* 多行超出隐藏 */
.multi-hidden {
		word-break: break-all;
		text-overflow: ellipsis;
		overflow: hidden;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}
</style>
