<template>
    <div class="test_cli admin_page">
		<div class="admin_table_search">
			<div class="admin_state">
			
				<div class="admin_state_item">
					<span>Start Date:</span>
					<a-range-picker
						style="width:250px"
						class="range_picker"
						v-model:value="rangePickerValue"
						:placeholder="[
							$t('HistoryPage.StartDate'),
							$t('HistoryPage.EndDate'),
						]"
						valueFormat="YYYY-MM-DD"
					>
						<template #suffixIcon>
							<span
								class="icon iconfont range_picker_icon icon-rili"
							></span>
						</template>
					</a-range-picker>
				</div>
				<!-- <div class="admin_state_item">
					<span>Start Time:</span>
						<a-time-range-picker style="width:250px" class="range_picker" valueFormat="HH:mm:ss" v-model:value="rangeTimeValue" />
				</div> -->
				<div class="admin_state_item">
                    <span>Expired or not:</span>
                    <a-select
                        v-model:value="isExpired"
						show-search
						allowClear
                        style="width: 250px"
                        placeholder="Please select"
                        :options="changeList"
                        @keydown.enter="gettrialList"
                    ></a-select>
                </div>
				<div class="admin_state_item">
                    <span>Cooperator:</span>
                    <input
                        v-model="cooperator"
                        placeholder="Please enter cooperator"
                        @keydown.enter="gettrialList"
                        type="text"
                        style="width: 250px"
                    />
                </div>
				<div class="admin_state_item">
                    <span>Promotion Code:</span>
                    <input
                        v-model="promotionCode"
                        placeholder="Please enter promotion code"
                        @keydown.enter="gettrialList"
                        type="text"
                        style="width: 250px"
                    />
                </div>
			</div>
			<div class="admin_search">
				<div class="admin_search_item" @click="searchHistoryList">Search</div>
				<div class="admin_search_item" @click="addhHistoryList">
                    Add
                </div>
			</div>
		</div>
		<div class="admin_table_content" ref="historyTable">
			<a-table
				@resizeColumn="handleResizeColumn"
				:columns="columns"
				:data-source="dataList"
				:scroll="{ y: historyTableHeight }"
				@change="changePage"
				:pagination="{
					showSizeChanger: true,
					current: currentPage,
					pageSize: pageSize,
					total: total,
					showQuickJumper: true,
					bordered: false,
				}"
			>
				<template #bodyCell="{ column, text, record, index }">
                    <div class="operate_list" v-if="column?.Operations">
                        <div
                            class="operate_item"
                            @click="setAagree(record)"
							style="margin-right: 2rem;"
                        >
                            Edit
                        </div>
						<div
                            class="operate_item"
                            @click="deletePromCode(record)"
							style="margin-right: 2rem;"
                        >
                            Delete
                        </div>
                    </div>
                </template>
			</a-table>
		</div>
		<allUserPoerationsVue ref="allUserPoerationsVue" @searchHistoryList="searchHistoryList"></allUserPoerationsVue>
    </div>
</template>
<script lang="ts">
import { defineComponent, ref, reactive, toRefs, computed, toRef, createVNode } from "vue";
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { useStore } from "vuex";
import { Modal,message,Upload,CascaderProps } from 'ant-design-vue';
import { Https } from "@/tool/https";
import allUserPoerationsVue from "./addAllUser.vue";
export default defineComponent({
    components: {
		allUserPoerationsVue
    },
    setup() {
        const store:any = useStore()
        let rangePickerValue: any = ref([]);
        let rangeTimeValue: any = ref([]);
        let renameData: any = ref({}); //修改名字选中的数据
		const dataDom = reactive({
			allUserPoerationsVue:null as any,
		})
        const columns: any = computed(() => {
            return [
				{
                    title: 'User Id',
                    align: "center",
                    ellipsis: true,
                    dataIndex: "id",
                    key: "id",
					width:100,
					sorter: true,
                    fixed: "left",
                },
                {
                    title: 'Max Redemptions',
                    align: "center",
                    dataIndex: "maxRedemptions",
                    key: "maxRedemptions",
					width:200,
                },
				
				{
                    title: 'Promotion Code',
                    align: "center",
                    ellipsis: true,
                    dataIndex: "promotionCode",
                    key: "promotionCode",
					width:150,
                },
				{
                    title: 'redeemBy',
                    align: "center",
                    ellipsis: true,
                    dataIndex: "redeemBy",
                    key: "redeemBy",
					width:150,
					customRender: (record: any) => {
						if(record.text){
							return new Date(record.text * 1000).toISOString().split('T')[0] // "2025-04-24"	
						}
                    },
                },
				{
                    title: 'percent Off',
                    align: "center",
                    ellipsis: true,
                    dataIndex: "percentOff",
                    key: "percentOff",
					width:150,
					customRender: (record: any) => {
						return record.text+'%'
                    },
                },
				{
                    title: 'commissionRate',
                    align: "center",
                    ellipsis: true,
                    dataIndex: "commissionRate",
                    key: "commissionRate",
					width:150,
					customRender: (record: any) => {
						return record.text+'%'
                    },
                },
				{
                    title: 'cooperator',
                    align: "center",
                    ellipsis: true,
                    dataIndex: "cooperator",
                    key: "cooperator",
					width:150,
                },
				{
                    title: 'Label Volmoney',
                    align: "center",
                    ellipsis: true,
                    dataIndex: "totalEarnings",
                    key: "totalEarnings",
					width:150,
                },
				{
                    title: 'Commission',
                    align: "center",
                    ellipsis: true,
                    dataIndex: "commission",
                    key: "commission",
					width:150,
                },
				{
                    title: 'Commission paid',
                    align: "center",
                    ellipsis: true,
                    dataIndex: "paidCommission",
                    key: "paidCommission",
					width:150,
                },
				{
					title: 'Unpaid commission',
                    align: "center",
                    ellipsis: true,
                    dataIndex: "unpaidCommission",
                    key: "unpaidCommission",
					width:150,
                },
				// 
				{
                    title: 'remark',
                    align: "center",
                    ellipsis: true,
                    dataIndex: "remark",
                    key: "remark",
					width:150,
                }, {
                    title: "Operations",
                    key: "operation",
					width:120,
                    align: "center",
                    fixed: "right",
                    // slots:{customRender:'action'}
                    Operations: true,
                },
            ];
        });

		let cooperator = ref('')
		let promotionCode = ref('')
        let dataList: any = ref([]);
        let status: any = ref(0);
        let orderBy: any = ref(0);
		// let filterOption = (input: any, option: any) => {
        //     // 使用 option.label 进行搜索
        //     return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
        // };
		const changeList = ref([
			{
				value: '',
				label: 'All',
			},
			{
				value: 'false',
				label: 'No',
			},
			{
				value: 'true',
				label: 'Yes',
			}
		])
		let addhHistoryList = () => {
			dataDom.allUserPoerationsVue.init('Add','')
		};
		let setAagree = (data:any) =>{
			dataDom.allUserPoerationsVue.init('Edit',data)
		}
		
        return {
			...toRefs(dataDom),
            rangePickerValue,
			rangeTimeValue,
            columns,
            dataList,
			cooperator,
			promotionCode,
            renameData,
            status,
			changeList,
			orderBy,
			addhHistoryList,
			setAagree,
        };
    },
    data() {
        return {
            currentPage: 1,
            pageSize: 10,
            total: 0,
			isExpired:'',
            historyTableHeight: 0,
			handleResizeColumn: (w:any, col:any) => {
				col.width = w;
			},
        };
    },
    mounted() {
        let historyTable: any = this.$refs.historyTable;
        this.historyTableHeight = historyTable.clientHeight - 200;
        this.gettrialList();
    },
    methods: {
        //改变页码
        changePage(e: any, filters:any, sorter:any) {
            this.currentPage = e.current;
            this.pageSize = e.pageSize;
			console.log(sorter)
            // this.gettrialList();
			// if(sorter.order){
			// 	if(sorter.columnKey == 'id'){
			// 		this.orderBy = 'id'
			// 	}
			// }
			this.orderBy = sorter.order == "descend" ? "DESC" : "ASC";
        },

        //查询列表
        searchHistoryList() {
            this.currentPage = 1;
            this.gettrialList();
        },
		deletePromCode(value:any){
			let this_ = this
            Modal.confirm({
                title: 'Are you sure you want to delete this discount record',
                icon: createVNode(ExclamationCircleOutlined),
                okText: 'Yes',
                cancelText: 'No',
				mask:false,
                centered:true,
                onOk() {
					Https.axiosGet(Https.httpUrls.deletePromCode,{params:{id:value.id}}).then((res:any)=>{
						this_.gettrialList()
					})
                }
            });
		},
		//获取列表
        gettrialList() {
			// let startTime: any = this.rangeTimeValue[0]
            //     ? this.rangeTimeValue[0]
            //     : '00:00:00';
            // let endTime: any = this.rangeTimeValue[1]
            //     ? this.rangeTimeValue[1]
            //     : '23:59:59';
			let startDate: any = this.rangePickerValue[0]
                ? this.rangePickerValue[0]+' '+'00:00:00'
                : "";
            let endDate: any = this.rangePickerValue[1]
                ? this.rangePickerValue[1]+' '+'23:59:59'
                : "";
			let data = {
				endTime:endDate,
				startTime:startDate,
				cooperator:this.cooperator,//合作商
				isExpired:this.isExpired,//是否过期
				promotionCode:this.promotionCode,//优惠码
				orderById:null,//排序字段
				size:this.pageSize,
				page:this.currentPage,
			}
			Https.axiosPost(Https.httpUrls.getAllCoupons,data).then((rv: any) => {
				if (rv) {
					console.log(rv)
					this.dataList = rv.records
					this.total = rv.total
					// this.workspaceItem.position = this.singleTypeList[0].label
				}
			})
        },
    },
});
</script>
<style lang="less" scoped>
.admin_page .admin_table_search .admin_state {
    display: flex;
    flex-wrap: wrap;
}
</style>