<template>
    <div>
        <!-- header -->
        <el-card class="!border-none" shadow="never">
            <el-alert
                type="warning"
                title="温馨提示：1.优惠券在发放时间内只要未关闭未删除符合条件就能领取；2.优惠券已关闭不能继续领取已发放的优惠券在用券时间内能继续使用；3.优惠券已删除不能继续领取已发放的优惠券不能继续使用。"
                :closable="false"
                show-icon
            />
            <el-form ref="formRef" class="mb-[-16px] mt-[15px]" :model="queryParams" :inline="true">
                <el-form-item label="优惠券名称">
                    <el-input
                        class="ls-input"
                        v-model="queryParams.name"
                        placeholder="请输入优惠券名称"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>

                <el-form-item label="推广方式">
                    <el-select v-model="queryParams.get_type" placeholder="全部">
                        <el-option label="全部" value="" />
                        <el-option label="用户领取" value="1" />
                        <el-option label="系统赠送" value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="创建时间">
                    <daterange-picker
                        class="w-[385px]"
                        v-model:startTime="queryParams.start_time"
                        v-model:endTime="queryParams.end_time"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <!-- 导航栏 -->
            <el-tabs v-model="queryParams.status" class="demo-tabs" @tab-click="handleTabclick">
                <el-tab-pane :label="`全部(${pager.extend.all || '0'})`" :name="0" />
                <el-tab-pane :label="`未开始(${pager.extend.not || '0'})`" :name="1" />
                <el-tab-pane :label="`进行中(${pager.extend.conduct || '0'})`" :name="2" />
                <el-tab-pane :label="`已结束(${pager.extend.end || '0'})`" :name="3" />
                <router-link
                    :to="{
                        path: getRoutePath('marketing.coupon.add')
                    }"
                >
                    <el-button class="w-[84px] h-[30px] mb-[9px] text-[12px] ml-2" type="primary">
                        新增优惠券
                    </el-button>
                </router-link>

                <!-- 表格 -->
                <el-table
                    size="large"
                    :data="pager.lists"
                    @selection-change="handleSelectionChange"
                >
                    <el-table-column label="优惠券名称" prop="name" />
                    <el-table-column label="优惠内容">
                        <template #default="{ row }">
                            <div class="flex items-center">
                                <div class="ml-[10px] text_hidden">{{ row.discount_content }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="推广方式" prop="get_method">
                        <template #default="{ row }">
                            <span
                                :class="{
                                    'text-tip': row.get_method === '系统赠送' ? true : false
                                }"
                            >
                                {{ row.get_method }}
                            </span>
                        </template>
                    </el-table-column>
                    <el-table-column label="使用时间">
                        <template #default="{ row }">
                            {{ row.use_time_text }}
                        </template>
                    </el-table-column>
                    <el-table-column label="发放数量" prop="send_total_text" />
                    <el-table-column label="已领取" prop="receive_number" />
                    <el-table-column label="已使用" prop="use_number" />
                    <el-table-column label="使用率">
                        <template #default="{ row }">
                            <!-- <span v-if="row.receive_number != 0"
                                >{{
                                    (Number(row.use_number) / Number(row.receive_number)).toFixed(
                                        0
                                    )
                                }}%</span
                            >
                            <span v-if="row.receive_number == 0">0</span> -->
                            {{
                                Number(row.use_ratio.slice(0, row.use_ratio.length - 1)).toFixed(2)
                            }}%
                        </template>
                    </el-table-column>
                    <el-table-column label="优惠券状态">
                        <template #default="{ row }">
                            <el-tag
                                class="ml-2"
                                type="success"
                                v-if="row.status_text === '进行中'"
                                >{{ row.status_text }}</el-tag
                            >
                            <el-tag
                                class="ml-2"
                                type="danger"
                                v-if="row.status_text === '未开始'"
                                >{{ row.status_text }}</el-tag
                            >
                            <el-tag class="ml-2" type="info" v-if="row.status_text === '已结束'">{{
                                row.status_text
                            }}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="创建时间" prop="create_time" width="180" />
                    <el-table-column label="操作" min-width="160" fixed="right">
                        <template #default="{ row }">
                            <router-link
                                :to="{
                                    path: getRoutePath('marketing.coupon.add'),
                                    query: {
                                        id: row.id
                                    }
                                }"
                            >
                                <el-button
                                    v-if="row.status_text != '已结束'"
                                    type="primary"
                                    link
                                    @click="handleedit(row.id)"
                                >
                                    编辑
                                </el-button>
                            </router-link>
                            <router-link
                                :to="{
                                    path: getRoutePath('marketing.coupon.add'),
                                    query: {
                                        id: row.id,
                                        detail: 'true'
                                    }
                                }"
                            >
                                <el-button
                                    v-if="row.root != 1"
                                    type="primary"
                                    link
                                    @click="handleedit(row.id)"
                                >
                                    详情
                                </el-button>
                            </router-link>
                            <el-button
                                v-if="row.status_text == '未开始'"
                                type="primary"
                                link
                                @click="handlestart(row.id)"
                            >
                                开始领取
                            </el-button>
                            <el-button
                                v-if="row.status_text == '进行中'"
                                type="primary"
                                link
                                @click="handleover(row.id)"
                            >
                                结束领取
                            </el-button>
                            <el-button
                                v-if="row.root != 1"
                                type="danger"
                                link
                                @click="handledel(row.id)"
                            >
                                删除
                            </el-button>
                            <router-link
                                :to="{
                                    path: getRoutePath('marketing.coupon.send'),
                                    query: {
                                        id: row.id
                                    }
                                }"
                            >
                                <el-button v-if="row.get_method == '系统赠送'" type="primary" link>
                                    系统赠送
                                </el-button>
                            </router-link>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tabs>
            <!-- 分页 -->
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>

<script lang="ts" setup>
import { usePaging } from '@/hooks/usePaging'
import {
    GetcouponLists,
    setcouponlists,
    startCoupon,
    overCoupon,
    delCoupon
} from '@/api/application/coupon'
import { adminLists } from '@/api/perms/admin'
import { useDictOptions } from '@/hooks/useDictOptions'
import feedback from '@/utils/feedback'
import { getRoutePath } from '@/router'

const queryParams = reactive({
    name: '',
    get_type: '',
    start_time: '',
    end_time: '',
    status: 0
})

//分页处理
const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: GetcouponLists,
    params: queryParams
})
getLists()

const { optionsData } = useDictOptions<{
    lists: any[]
}>({
    lists: {
        api: adminLists
    }
})

//删除商品
let ids: Array<number> = []
const handleSelectionChange = (val: any[]) => {
    ids = val.map((item) => {
        return item.id
    })
    console.log(ids)
}
const handleedit = async (id: number) => {
    // await feedback.confirm('确定要作废？')
    // await setcouponlists({ id: [id] })
    // getLists()
}

//切换导航栏
const handleTabclick = async () => {
    await nextTick()
    getLists()
}

//开始领取
const handlestart = async (id: number) => {
    await startCoupon({ id })
    getLists()
}
//结束领取
const handleover = async (id: number) => {
    await overCoupon({ id })
    getLists()
}
//删除
const handledel = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await delCoupon({ id })
    getLists()
}
</script>

<style lang="scss" scope>
//TODO
.el-alert--warning.is-light {
    background-color: rgba(246, 248, 255, 1);
    color: rgba(64, 115, 250, 1);
}

.ls-input {
    width: 280px;
}
.text-tip {
    color: #67c23a;
}
</style>
