<template>
    <div class="main-content12">
        <!-- page header -->
        <div class="pageheader">
            <span class="pagetitle">查重订单</span>
        </div>

        <div class="elementWrap mb-10">
            <el-form>
                <div class="flex-between operateBtns">
                    <b>筛选及导出列表</b>
                    <div class="flex gap24">
                        <GuipButton size="table" type="ignore" @click="resetSearchParams">清空</GuipButton>
                        <GuipButton size="table" type="system" @click="getFilterParams">筛选</GuipButton>
                        <GuipButton size="table" type="primary" @click="exportData">导出</GuipButton>
                    </div>
                </div>
                <div class="flex">
                    <p class="mr-12">时间范围</p>
                    <el-date-picker class="mr-32" v-model="searchDate" type="daterange" range-separator="至"
                        start-placeholder="开始日期" end-placeholder="结束日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd">
                    </el-date-picker>

                    <GuipSelect class="mr-32" label="服务类型" v-model="searchType" @change="changeSearchType()" :options="searchTypeList" 
                        :extraItem="{label:'全部',value:'0'}" placeholder="" clearable />

                    <GuipSelect class="mr-32" label="站点" v-model="searchUid" @change="changeSearchSite()" :options="searchSiteList" valueKey="uid" labelKey="name"
                        :extraItem="{label:'全部',value:'0'}" placeholder="" clearable />

                    <GuipSelect class="mr-32" width="100px" label="报告状态" v-model="searchStatus" @change="changeSearchStatus()"
                        :options="searchStatusList" :extraItem="{ label: '全部', value: 'all' }" defaultValue="" placeholder="不限" clearable />
                </div>
            </el-form>
        </div>

        <!-- page content -->
        <div class="elementWrap">
            <div>
                <el-form>
                    <div class="flex" style="justify-content: space-between;">
                        <div class="flex">
                            <h3 class="checktitle mr-24">查重列表</h3>
                            <GuipSelect class="mr-32" label="报告格式" v-model="searchUid" :options="siteList"
                                valueKey="uid" labelKey="name" @change="changeSearchSite()" defaultValue=""
                                placeholder="不限" clearable />
                        </div>
                        <GuipInput label="订单号" v-model="searchTid" @blur="changeSearchTid()" placeholder="请输入订单号"
                            ref="GuipInput" />

                    </div>

                    <PromptText class="my-32" text='订单提示：电商订单，提交检测后会自动发货，请勿手动发货、代收款订单，退款须由平台操作，不可私自退款' :type="2" />

                    <GuipTable :tableData="orderList" ref="multipleTable" autoColumn="true" :loading="tableLoading">
                        <el-table-column prop="sitename" label="站点" fixed="left" min-width="60px">
                            <template slot-scope="scope">
                                <div class="flex">
                                    <GuipToolTip :content="scope.row.sitename">
                                        <img class="wh-16" src="@/assets/site.svg" alt="">
                                    </GuipToolTip>
                                </div>
                            </template>
                        </el-table-column>

                        <el-table-column prop="sale_id" label="ID" fixed="left" min-width="120px">
                            <template slot-scope="scope">
                                <div class="flex">
                                    <p>{{ scope.row.sale_id }}</p>
                                    <GuipToolTip class="ml-7" v-if="scope.row.ispublic == 1" content="自供货">
                                        <img class="wh-16" src="@/assets/self_supply.svg" />
                                    </GuipToolTip>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="type_desc" label="检测类型" fixed="left" min-width="160px"></el-table-column>
                        <el-table-column prop="pay_type" label="买家" min-width="60px">
                            <template slot-scope="scope">
                                <div class="flex">
                                    <GuipToolTip :content="scope.row.pay_type_desc">
                                        <img class="wh-16" :src="getPayImg(scope.row.pay_type)" alt="">
                                    </GuipToolTip>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="tids" label="订单号" min-width="220px">
                            <template slot-scope="scope">
                                <div class="flex" v-if="scope.row.status == 1">
                                    <GuipToolTip :content="'下载报告 作者：' + scope.row.author + ' 标题：' + scope.row.title">
                                        <a :href="'http://' + scope.row.domain + '/report/tid/' + tid + '/sid/' + scope.row.sale_id"
                                            target="_blank" v-for="tid in scope.row.tids" :key="tid">{{ tid }}</a>
                                    </GuipToolTip>
                                </div>
                                <div class="" v-else>
                                    <p v-for="tid in scope.row.tids" :key="tid">
                                        {{ tid }}
                                        <GuipToolTip content="待发货">
                                            <img v-if="scope.row.pay_type == 11 && scope.row.pdd_is_wait_deliver_goods[tid] == true"
                                                class="wh-16 ml-7" src="@/assets/wait_deliver_goods.svg" />
                                        </GuipToolTip>
                                    </p>

                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="author" label="作者">
                            <template slot-scope="scope">
                                <div class="flex">
                                    <GuipToolTip :content="'作者：' + scope.row.author + ' 标题：' + scope.row.title">
                                        <p>{{ scope.row.author }}</p>
                                    </GuipToolTip>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="src" label="渠道" min-width="60px">
                            <template slot-scope="scope">
                                <div class="flex">
                                    <GuipToolTip :content="scope.row.src_desc">
                                        <img class="wh-16" :src="getChannelImg(scope.row.src)" alt="">
                                    </GuipToolTip>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="ip" label="IP" min-width="150px"></el-table-column>
                        <el-table-column prop="sale_date" label="销售日期" min-width="200px"></el-table-column>
                        <el-table-column prop="wordnum" label="字数"></el-table-column>
                        <el-table-column prop="similarity" label="相似度">
                            <template slot-scope="scope">
                                <div class="flex">
                                    <p v-if="scope.row.similarity > 0">{{ scope.row.similarity }}%</p>
                                    <p v-else>-</p>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="income" label="实际收入" min-width="100px"></el-table-column>
                        <el-table-column prop="cost" label="成本"></el-table-column>
                        <el-table-column prop="status_desc" label="状态" min-width="150px">
                            <template slot-scope="scope">
                                <div class="flex">
                                    <div v-if="scope.row.status == 1" class="status-item divgreen">
                                        <span class="fontgreen">{{ scope.row.status_desc }}</span>
                                    </div>
                                    <div v-else-if="scope.row.status == 3 || scope.row.status == 11 || scope.row.status == 12"
                                        class="status-item divblue">
                                        <span class="fontblue">{{ scope.row.status_desc }}</span>
                                    </div>
                                    <div v-else-if="scope.row.status == 2 || scope.row.status == 10 || scope.row.status == 4"
                                        class="status-item divred">
                                        <span class="fontred">{{ scope.row.status_desc }}</span>
                                    </div>
                                    <div v-else-if="scope.row.status == 0 || scope.row.status == 8"
                                        class="status-item divgray">
                                        <span class="fontgray">{{ scope.row.status_desc }}</span>
                                    </div>
                                    <div v-else-if="scope.row.status == 5" class="status-item divorange">
                                        <span class="fontorange">{{ scope.row.status_desc }}</span>
                                    </div>
                                    <div v-else-if="scope.row.status == 6 || scope.row.status == 7"
                                        class="status-item divpurple">
                                        <span class="fontpurple">{{ scope.row.status_desc }}</span>
                                    </div>
                                    <GuipToolTip v-if="scope.row.status == 1 && scope.row.is_downloaded_report == 1"
                                        content="已下载报告标识">
                                        <img class="wh-16 ml-7" src="@/assets/downloaded.svg" />
                                    </GuipToolTip>

                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" min-width="80px">
                            <template slot-scope="scope">
                                <div class="flex">
                                    <el-button v-if="scope.row.status == 1" @click="showConfirmDelReport(scope.row)"
                                        type="text">删报告</el-button>
                                </div>
                            </template>
                        </el-table-column>
                    </GuipTable>
                    <Page :current-page="orderListCurrentPage" :has-next-page="isHasNextPage"
                        :total-records="orderListTotalRecords" :page-size="orderListPageSize" :is-loading="tableLoading"
                        @page-change="handlePageChange" />
                   
                </el-form>
            </div>
            <GuipDialog :dialogVisible="dialogVisibleDelReport" title="删除报告" confirmText="删除" cancelText="取消"
                :show-close-button="false" :show-cancel-button="true" @confirm="confirmDelReport" @cancel="handleCancel"
                @close="handleClose" @dialogVisibleChange="dialogVisibleChange">
                <!-- 自定义内容 -->
                <p class="mx-24 mt12 flex">是否删除 ID:{{ delReportId }} 报告？删除后无法恢复</p>
            </GuipDialog>
        </div>
    </div>
</template>

<script>

import GuipTable from '@/components/GuipTable.vue';
import GuipSelect from '@/components/GuipSelect.vue';
import GuipInput from '@/components/GuipInput.vue';
import GuipButton from '@/components/GuipButton.vue';
import GuipToolTip from '@/components/GuipToolTip.vue';
import GuipDialog from '@/components/GuipDialog.vue';
import Page from '@/components/Page.vue';
import PromptText from '@/components/PromptText.vue';

export default {
    name: 'siteList',
    components: {
        GuipTable,
        GuipSelect,
        GuipButton,
        GuipInput,
        GuipToolTip,
        GuipDialog,
        Page,
        PromptText,
    },
    data() {
        return {
            // AUTH
            token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3NTI2NDY1NDUsIm5iZiI6MTc1MjY0NjU0NSwiZXhwIjoxNzU1MjM4NTQ1LCJ1c2VyIjoic3VidXNlciIsImxvZ2luX3R5cGUiOjAsImFpZCI6IjEifQ.G-Is-x9qPMiV_urOlDPQVRjfAIozySxL5EK2k82d46k',
            // 表格加载
            tableLoading: false,
            // 订单列表
            orderList: [],
            orderListTotalRecords: 0,
            isHasNextPage: true,
            orderListCurrentPage: 1,
            orderListPageSize: 100,
            orderListNextMinId: 0,
            orderListPrevMaxId: 0,
            orderListEndPage: 0,

            siteList: [],

            searchDate: '',
            searchStatus: 'all',
            searchStatusList: [],
            searchUid: '0',
            searchSiteList: [],
            searchTid: 0,
            searchType: '0',
            searchTypeList: [],

            dialogVisibleDelReport: false,
            delReportId: '',

        }
    },
    mounted() {
        this.getOrderList()
    },
    methods: {
        getFilterParams(){
            var stime = '';
            var etime = '';
            if (this.searchDate && this.searchDate.length > 0) {
                stime = this.searchDate[0];
                etime = this.searchDate[1];
            }
            var params = {};
            if (stime) params.stime = stime;
            if (etime) params.etime = etime;
            if (this.searchStatus) params.status = this.searchStatus;
            if (this.searchType) params.type = this.searchType;
            if (this.searchUid) params.uid = this.searchUid;

            this.getOrderList(params)
            
        },
        resetSearchParams(){
            this.searchDate = ''
            this.searchStatus = 'all'
            this.searchType = '0'
            this.searchUid = '0'
            // 或者使用 $set
        },
        handleError(error) {
            console.error('分页错误:', error)
            // 可以在这里显示错误提示
        },
        getOrderList(params) {
            console.log(this.searchDate)
            this.tableLoading = true
            try {
                const requestParams = {
                    pagesize: this.orderListPageSize,
                    page: this.orderListCurrentPage,
                    ...params
                }
                console.log(requestParams, 'requestParams===');
                this.$http('POST', '/agentnew/ajax_get_check_order_list', requestParams, {
                    headers: {
                        'Auth': this.token
                    }
                }).then(response => {
                    this.tableLoading = false
                    this.$nextTick(() => {
                        this.orderList = response.data.list
                        this.searchStatusList = response.data.search_check_status
                        this.orderListTotalRecords = response.data.total_records
                        this.searchSiteList = response.data.sitelist
                        this.searchTypeList = response.data.type2name
                        // 更新minId和maxId
                        if (response.data.minid) this.orderListNextMinId = response.data.minid
                        if (response.data.maxid) this.orderListPrevMaxId = response.data.maxid
                        this.isHasNextPage = response.data.is_has_next_page;
                        if (this.orderListTotalRecords > 0) {
                            // 计算总页数
                            this.orderListCurrentPage = Math.ceil(this.orderListTotalRecords / this.orderListPageSize)
                            // this.page = this.totalPages || 1
                            // this.jumpPage = this.page 
                            // this.end_page = 1;
                        }
                        // console.log(this.isHasNextPage, 'isHasNextPage')
                    })
                }).catch(error => {
                    console.error(error, 'error')
                })


            } catch (error) {
                console.error('数据加载失败:', error)
            } finally {
                this.tableLoading = false
            }
        },
        exportData() {
            var stime = '';
            var etime = '';
            if (this.searchDate && this.searchDate.length > 0) {
                stime = this.searchDate[0];
                etime = this.searchDate[1];
            }
            var params = {};
            if (stime) params.stime = stime;
            if (etime) params.etime = etime;
            if (this.searchStatus) params.status = this.searchStatus;
            if (this.searchType) params.type = this.searchType;
            if (this.searchUid) params.uid = this.searchUid;

            this.$http('GET', '/agentnew/export_order', params, {
                headers: {
                    'Auth': this.token
                }
            }).then(response => {
                console.log(response)
            }).catch(error => {
                console.error(error, 'error')
            })

            return

            // var stime = '';
            // var etime = '';
            // if (this.searchDate && this.searchDate.length > 0) {
            //     stime = this.searchDate[0];
            //     etime = this.searchDate[1];
            // }
            // var url = '/agentnew/export_order';
            // if (stime) url += '/stime/' + stime;
            // if (etime) url += '/etime/' + etime;
            // if (this.searchStatus) url += '/status/' + this.searchStatus;
            // if (this.searchType) url += '/type/' + this.searchType;
            // if (this.searchUid) url += '/uid/' + this.searchUid;

            // location.href = url;
        },
        getPayImg(pay_type) {
            return require('@/assets/pay/pay_' + pay_type + '.svg');
        },
        getChannelImg(src) {
            return require('@/assets/channel_' + src + '.svg');
        },
        changeSearchSite() {
            console.log(this.searchUid)
        },
        changeSearchTid() {
            console.log(this.searchTid,'this.searchTid')
            this.getOrderList({orderid:this.searchTid})
        },
        changeSearchType() {
            console.log(this.searchType)
        },
        changeSearchStatus() {
            console.log(this.searchStatus)
        },
        // 弹框相关方法---start
        showConfirmDelReport(row) {
            this.dialogVisibleDelReport = true;
            this.delReportId = row.sale_id;
        },
        // 确认按钮事件
        confirmDelReport() {
            this.$http('POST', '/agentnew/ajax_del_report', {
                saleid: this.delReportId,
            }, {
                headers: {
                    'Auth': this.token
                }
            }).then(response => {
                this.$nextTick(() => {
                    if (response.status) {
                        this.$Message.success(response.info);
                        this.dialogVisibleDelReport = false;
                    } else {
                        this.$Message.error(response.info);
                    }
                })
            }).catch(error => {
                console.error(error, 'error')
            })
        },
        // 取消按钮事件
        handleCancel() {
            this.dialogVisibleDelReport = false;
        },
        // 关闭弹框事件
        handleClose() {
            this.dialogVisibleDelReport = false;
        },
        dialogVisibleChange(data) {
            console.log(data, 'data098908090');
        },
        // 弹框相关方法---end

        handlePageChange({ page, endPage, direction }) {
            console.log(endPage, 'endPage===');
            this.orderListCurrentPage = page

            const params = { page }

            // 根据翻页方向设置参数
            if (direction === 'prev') {
                params.maxid = this.orderListPrevMaxId
                if (this.orderListNextMinId) params.minid = null
            } else if (direction === 'next') {
                params.minid = this.orderListNextMinId
                if (this.orderListPrevMaxId) params.maxid = null
            } else if (direction === 'last') {
                params.end_page = 1
                params.page = null
                params.minid = null
                params.maxid = null
            }

            this.getOrderList(params)
        }
    },
    computed: {
    },
}
</script>
<style lang="scss" scoped>
.pageheader {
    display: flex;
    justify-content: space-between;
    /* 关键属性 */
    align-items: center;
    margin: 0px 0px 16px 0px;
}

.pagetitle {
    font-size: 16px;
    font-weight: bold;
    line-height: normal;
    letter-spacing: 0.08em;
    color: #1E2226;
    margin-top: 8px;
}

.elementWrap {
    padding: 32px 36px;
    background: #fff;
}

.operateBtns {
    padding: 17px 0;
    border-bottom: 1px solid #DFE2E6;
    margin-bottom: 24px;
}

::v-deep .custom-popover {
    position: fixed !important;
    // max-height: 290px;
    // overflow-y: auto;
    margin-top: 0 !important;
    margin-left: 0 !important;
    transform: none !important;
}

.wh-16 {
    width: 16px;
    height: 16px;
    z-index: 1;
}

.mb-10 {
    margin-bottom: 10px;
}

.mr-12 {
    margin-right: 12px;
}

.mr-24 {
    margin-right: 24px;
}

.mr-32 {
    margin-right: 32px;
}

.my-32 {
    margin-bottom: 32px;
    margin-top: 32px;
}

a {
    text-decoration: none;
    color: #006AFF;
}

a:hover {
    text-decoration: underline;
}

.ml-7 {
    margin-left: 7px;
}



.status-item {
    height: 22px;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 6px;
    z-index: 0;
    padding: 2px 10px;
    border-radius: 4px;

}

.divgreen {
    background: rgba(239, 255, 224, 0.5);
    box-sizing: border-box;
    border: 1px solid rgba(0, 194, 97, 0.6);
}

.fontgreen {
    font-family: Microsoft YaHei UI;
    font-size: 14px;
    font-weight: normal;
    line-height: normal;
    text-align: center;
    letter-spacing: 0.08em;
    color: #0DAF49;
}

.divblue {
    background: #F2F7FF;
    box-sizing: border-box;
    border: 1px solid #BFDAFF;
}

.fontblue {
    font-family: Microsoft YaHei UI;
    font-size: 14px;
    font-weight: normal;
    line-height: normal;
    text-align: center;
    letter-spacing: 0.08em;
    color: #006AFF;
}

.divred {
    background: #FFF1F0;
    box-sizing: border-box;
    border: 1px solid #FFA39E;
}

.fontred {
    font-family: Microsoft YaHei UI;
    font-size: 14px;
    font-weight: normal;
    line-height: normal;
    text-align: center;
    letter-spacing: 0.08em;
    color: #FF4D4F;
}

.divgray {
    background: #F6F7FA;
    box-sizing: border-box;
    border: 1px solid #DFE2E6;
}

.fontgray {
    color: #626573;
}

.divorange {
    background: #FFFBF2;
    box-sizing: border-box;
    border: 1px solid rgba(251, 131, 45, 0.38);
}

.fontorange {
    color: #FB832D;
}

.divpurple {
    background: #F9F2FF;
    box-sizing: border-box;
    border: 1px solid rgba(126, 118, 253, 0.28);
}

.fontpurple {
    color: #6258FF;
}

.checktitle {
    /* body/body 2_bold */
    font-family: Microsoft YaHei UI;
    font-size: 14px;
    font-weight: bold;
    line-height: normal;
    letter-spacing: 0.08em;
    /* text/text_1 */
    color: #1E2226;
}

.el-form-item {
    margin-bottom: 0px
}
</style>