<template>
    <!-- 广告列表 -->
    <div>
        <!-- 提示信息 -->
        <div class="tips">
            <el-row>
                <el-col :span="24">
                    <i class="tipsIcon"></i>
                    <span class="tipsText">24小时内，若运营没有操作用户的申请，则用户申请的时间、信息等将会释放。</span>
                </el-col>
            </el-row>
        </div>
        <!-- 头部搜索框部分 -->
        <div class="title">
            <el-row>
                <el-col :span="6" class="left">
                    <div class="searchBox">
                        <div class="searchTitle">工单编号</div>
                        <el-input class="input" v-model="order_code" clearable placeholder="请输入工单编号"></el-input>
                    </div>
                </el-col>
                <el-col :span="6" class="left">
                    <div class="searchBox">
                        <div class="searchTitle">订单状态</div>
                        <el-select v-model="order_status" clearable placeholder="请选择订单状态">
                            <el-option v-for="item in order" :key="item.value" :label="item.label" :value="item.value">
                            </el-option>
                        </el-select>
                    </div>
                </el-col>
                <el-col :span="8" class="right">
                    <div class="btnList">
                        <button class="search" @click="goSearch">搜索</button>
                        <button class="reset" @click="goReset">重置</button>
                    </div>
                </el-col>
            </el-row>
        </div>
        <!--表格内容 start------------------------------------------------------------>
        <div class="layerBox">
            <tableTitle :name="tableDivTitle" />
            <el-row>
                <template>
                    <el-table class="my-table" :data="tableData" style="width: 100%">
                        <el-table-column fixed prop="id" label="编号" width="80">
                        </el-table-column>
                        <el-table-column prop="order_num" label="工单编号" width="175">
                        </el-table-column>
                        <el-table-column prop="name" label="广告名称" width="160">
                        </el-table-column>
                        <el-table-column prop="orderStatus" label="订单状态" width="100">
                        </el-table-column>
                        <el-table-column prop="sttime" label="开始时间" width="">
                        </el-table-column>
                        <el-table-column prop="edtime" label="结束时间" width="">
                        </el-table-column>
                        <el-table-column prop="cttime" label="创建时间" width="">
                        </el-table-column>
                        <el-table-column prop="price" label="总金额" width="110">
                        </el-table-column>
                        <el-table-column fixed="right" label="操作" width="330">
                            <template slot-scope="scope">
                                <div class="listBtnBox">
                                    <div class="listDeleteBtn" @click="deleteRow(scope.row.id)"><i
                                            class="el-icon-delete"></i>删除</div>
                                    <div class="listEditBtn" @click="goEdit(scope.row.id, scope.row)"><i
                                            class="el-icon-edit-outline"></i>编辑</div>
                                    <div class="listReviewBtn" @click="goReview(scope.row.id, scope.row)"><i
                                            class="review"></i>审核</div>
                                </div>
                            </template>
                        </el-table-column>
                    </el-table>
                </template>
            </el-row>
        </div>
        <!--分页 start------------------------------------------------------------>
        <div class="alignBox">
            <el-row>
                <el-col :span="24">
                    <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                        :current-page="page" :page-size="pageSize" layout="total, prev, pager, next, jumper"
                        :total="total">
                    </el-pagination>
                </el-col>
            </el-row>
        </div>
        <!--分页 end------------------------------------------------------------>
        <!--表格内容 end------------------------------------------------------------>

        <!-- 弹出框 编辑 start----------------------------------------------------------->
        <el-dialog :title="dialogName" :visible.sync="dialogTableVisible" width="56%" top="8vh">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <div class="dialogText">
                    <el-form-item label="广告名称:" prop="">
                        <el-input v-model="ruleForm.name" disabled placeholder="请输入网站名称"></el-input>
                    </el-form-item>
                    <el-form-item label="广告网址:" prop="">
                        <el-input v-model="ruleForm.ad_url" disabled placeholder="请输入网站名称"></el-input>
                    </el-form-item>
                    <el-form-item label="广告图:" prop="">
                        <div class="adImage">
                            <img :src="ruleForm.adImg" v-if="this.ruleForm.adImg" alt="">
                            <img :src="'../../assets/advertise/404.png'" v-else-if="this.ruleForm.adImg = ''" alt="">
                        </div>
                    </el-form-item>
                    <el-form-item label="持续时间:" prop="">
                        <el-col :span="11">
                            <el-date-picker v-model="ruleForm.startTime" type="datetime" disabled placeholder="选择日期时间">
                            </el-date-picker>
                        </el-col>
                        <el-col :span="2" class="zhi">
                            至
                        </el-col>
                        <el-col :span="11">
                            <el-date-picker v-model="ruleForm.endTime" type="datetime" disabled placeholder="选择日期时间">
                            </el-date-picker>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="广告尺寸:" prop="">
                        <el-radio v-model="ruleForm.radio" label="1">1200x90px</el-radio>
                        <el-radio v-model="ruleForm.radio" label="2">420x560px</el-radio>
                    </el-form-item>

                    <el-form-item label="价格:" prop="price">
                        <div class="price">
                            <el-col :span="6">
                                <el-input v-model="ruleForm.price" placeholder="请输入价格"></el-input>
                            </el-col>
                            <el-col :span="2" class="zhi">
                                元 x
                            </el-col>
                            <el-col :span="6">
                                <el-input v-model="ruleForm.days" disabled placeholder="请输入天数"></el-input>
                            </el-col>
                            <el-col :span="2" class="zhi">
                                天 x
                            </el-col>
                            <el-col :span="6">
                                <el-input v-model="ruleForm.num" disabled placeholder="请输入个数"></el-input>
                            </el-col>
                            <el-col :span="2" class="zhi">
                                个
                            </el-col>
                        </div>
                    </el-form-item>
                    <el-form-item label="广告位置:" prop="">
                        <span class="example" @click="goGraph">查看示例图</span>
                        <div>
                            <el-button disabled v-for="item in ruleForm.adPlace" class="el_btnList">{{ item
                                }}</el-button>
                        </div>
                    </el-form-item>
                </div>
                <div class="dialogBtn">
                    <button class="cancel" @click="cancelForm">取消</button>
                    <button class="submit" @click="submitForm">提交</button>
                </div>
            </el-form>
        </el-dialog>
        <!-- 弹出框 编辑 end----------------------------------------------------------->

        <!-- 弹出框 广告位示例图 start----------------------------------------------------------->
        <el-dialog title="审核状态" :visible.sync="dialogVisible" width="50%" top="25vh">
            <ul class="graph">
                <li>
                    <h3>首页:</h3>
                    <img src="../../assets/advertise/guanggaowei2.png" alt="">
                </li>
                <li>
                    <h3>列表页:</h3>
                    <img src="../../assets/advertise/guanggaowei1.png" alt="">
                </li>
            </ul>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 弹出框 广告位示例图 end----------------------------------------------------------->

        <!-- 弹出框 审核 start----------------------------------------------------------->
        <el-dialog title="审核状态" :visible.sync="dialogFormVisible" width="35%" top="25vh">
            <template slot="title">
                <div style="display: flex; align-items: center;">
                    <span>审核状态</span>
                    <span class="redTips">(确定后,审核状态不可再次更改.请确认好再审核)</span>
                </div>
            </template>
            <el-form ref="form" :model="form" label-width="80px">
                <div class="radioGroup">
                    <el-radio-group v-model="status_radio">
                        <el-radio :label="3">通过</el-radio>
                        <el-radio :label="6">驳回</el-radio>
                    </el-radio-group>
                    <el-form-item v-show="this.status_radio == 6" label="驳回原因:" :label-width="formLabelWidth"
                        prop="name">
                        <el-input v-model="textarea" type="textarea" resize="none" :rows="3" placeholder="请输入内容">
                        </el-input>
                    </el-form-item>
                </div>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="subimtStatus">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 弹出框 审核 end----------------------------------------------------------->
    </div>

</template>

<script>
//表格标题
import tableTitle from './components/tableTitle.vue';

//引入公用样式
import '@/styles/global.less';
import { getStatus, getOrderList, getOrderListAdmin, getOrderDetail, getOrderDetailAdmin, applyOrderStatusAdmin, editOrderAdmin, rejectOrderAdmin, endOrderAdmin, delOrderAdmin } from '@/api/advertise'
export default {
    components: {
        tableTitle,//表格标题-
    },
    data() {
        return {
            tableDivTitle: "广告列表",  //列表标题
            dialogTableVisible: false,      //编辑弹框
            dialogName: '编辑', //编辑弹窗名称
            dialogFormVisible: false,     //审核弹框
            dialogVisible: false,  //示例图弹窗

            form: {},  //审核状态的ref属性名称

            tableData: [],//表格数据

            ad_img: 'http://183.131.25.186:9501/image/20250102/1735810611310322.png', //广告图

            // 搜索框相关
            order_code: '', //工单编号 
            order_status: '',//订单状态
            order: [ //订单状态数据
                {
                    value: 1,
                    label: '已通过'
                },
                {
                    value: 2,
                    label: '已驳回'
                },
                {
                    value: 3,
                    label: '已取消'
                },
                {
                    value: 5,
                    label: '已过期'
                },
                {
                    value: 6,
                    label: '待审核'
                },
                {
                    value: 7,
                    label: '已完成'
                }
            ],

            //活动id
            activeid: "",

            // 分页相关
            page: 1,
            pageSize: 10,
            total: 0,

            orderads: [],

            // 审核弹窗相关
            status_radio: "",   //审核状态
            textarea: '',    //驳回原因

            formLabelWidth: '',

            ruleForm: {
                name: '', //广告名称
                ad_url: '', //广告网址
                adImg: '', //广告图
                radio: '',//广告尺寸
                startTime: '', //开始时间
                endTime: '',   //结束时间
                price: '',  //价格
                days: '',   //天数
                num: '',   //个数
                adPlace: [] //广告位
            },
            rules: {
                name: [
                    { required: true, message: '请输入网站名称', trigger: 'blur' },
                ],
                price: [
                    { required: true, message: '请输入要修改的价格', trigger: 'change' }
                ]
            }
        }
    },
    methods: {
        //1.列表和分页相关 start ------------------------------------------------------------>
        //1.1 开始请求列表信息方法
        getData() {
            if (this.order_code == '' || this.order_status == '') {
                let data = new FormData()
                data.append('page', this.page)
                data.append('pageSize', this.pageSize)
                data.append('status', this.order_status)
                data.append('order_num', this.order_code)

                getOrderListAdmin(data).then(data => {
                    console.log(data);
                    this.tableData = data.data.rows
                    this.total = data.data.count
                    for (let item of this.tableData) {
                        if (item.status == 1) {
                            item.orderStatus = '已通过'
                        } else if (item.status == 7) {
                            item.orderStatus = '已完成'
                        } else if (item.status == 2) {
                            item.orderStatus = '已驳回'
                        } else if (item.status == 3) {
                            item.orderStatus = '已取消'
                        } else if (item.status == 5) {
                            item.orderStatus = '已过期'
                        } else if (item.status == 6) {
                            item.orderStatus = '待审核'
                        }
                    }
                })
            } else {
                let data = new FormData()
                data.append('page', this.page)
                data.append('pageSize', this.pageSize)
                console.log(data);
                getOrderListAdmin(data).then(data => {
                    console.log("getOrderListAdmin", data);
                    this.tableData = data.data.rows
                    this.total = data.data.count
                    for (let item of this.tableData) {
                        if (item.status == 1) {
                            item.orderStatus = '已通过'
                        } else if (item.status == 7) {
                            item.orderStatus = '已完成'
                        } else if (item.status == 2) {
                            item.orderStatus = '已驳回'
                        } else if (item.status == 3) {
                            item.orderStatus = '已取消'
                        } else if (item.status == 5) {
                            item.orderStatus = '已过期'
                        } else if (item.status == 6) {
                            item.orderStatus = '待审核'
                        }
                    }
                })
            }

        },

        //1.2 删除内容
        deleteRow(id) {
            console.log(id);
            let data = new FormData()
            data.append('id', id)

            this.$confirm('注意:删除后，该条信息及其绑定关系全部删除', '是否确认删除该条信息？', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
            }).then(() => {
                console.log("当前删除：" + id)
                let data = new FormData()
                data.append('id', id)
                console.log(data);

                delOrderAdmin(data).then(data => {
                    console.log(data);
                    if (data.code == 0) {
                        this.$message({
                            message: data.message,
                            type: 'error'
                        })
                    }
                    if (data.code == 200) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    }
                    this.getData()
                })
            }).catch(() => {
                this.$message({
                    type: 'warning',
                    message: '已取消删除'
                });
            });
        },
        //1.3 查看示例图
        goGraph() {
            this.dialogVisible = true
        },

        //1.4 列表内容分页
        //直接跳转
        handleSizeChange(val) {
            this.page = val;
            this.getData();
        },
        //1.5 点击分页
        handleCurrentChange(val) {
            this.page = val;
            this.getData();
        },
        // 1.6 搜索按钮
        goSearch() {
            console.log(this.status);

            let data = new FormData()
            data.append('page', this.page)
            data.append('pageSize', this.pageSize)
            data.append('status', this.order_status)
            data.append('order_num', this.order_code)
            console.log(data);

            getOrderListAdmin(data).then(data => {
                console.log(data);
                this.tableData = data.data.rows
                this.total = data.data.count
                for (let item of this.tableData) {
                    if (item.status == 1) {
                        item.orderStatus = '已通过'
                    } else if (item.status == 7) {
                        item.orderStatus = '已完成'
                    } else if (item.status == 2) {
                        item.orderStatus = '已驳回'
                    } else if (item.status == 3) {
                        item.orderStatus = '已取消'
                    } else if (item.status == 5) {
                        item.orderStatus = '已过期'
                    } else if (item.status == 6) {
                        item.orderStatus = '待审核'
                    }
                }
            })
        },
        //1.7 重置按钮
        goReset() {
            this.order_code = ''
            this.order_status = ''
            this.page = 1
            this.pageSize = 10
            this.getData();
        },
        //列表和分页相关 end ------------------------------------------------------------>

        //1.9 编辑
        goEdit(id, val) {
            console.log(id, val);
            this.activeid = id
            this.dialogTableVisible = true
            this.ruleForm.name = val.name
            if (val.width == 1200) {
                this.ruleForm.radio = '1'
            } else {
                this.ruleForm.radio = '2'
            }
            this.ruleForm.startTime = val.sttime
            this.ruleForm.endTime = val.edtime
            this.ruleForm.days = val.days

            let data = new FormData()
            data.append('id', id)

            //查看列表详情
            getOrderDetailAdmin(data).then(data => {
                // console.log('详情', data);
                console.log('详情', data.data.ad[0]?.image_url);

                this.ruleForm.ad_url = data.data.ad[0]?.image_url //广告链接
                this.ruleForm.adImg = data.data.ad[0]?.image_src //广告图
                this.ruleForm.num = data.data.ad.length //广告个数
                this.ruleForm.price = val.price / val.days / this.ruleForm.num //广告价格

                let adPlace = data.data.ad

                this.ruleForm.adPlace = []

                for (let item of adPlace) {
                    this.ruleForm.adPlace.push(item.ad_name)//广告位名称
                }
                console.log(this.ruleForm.adPlace);
            })
        },

        //2.0 审核
        goReview(id, val) {
            this.activeid = id
            this.activeid = id
            if (val.status != 6) {
                this.$message({
                    message: '该广告已经审核，不能再次审核',
                    type: 'warning'
                })
                this.dialogFormVisible = false
            } else {
                this.dialogFormVisible = true
            }
        },

        //审核中的确定
        subimtStatus() {
            if (this.status_radio == '3') {
                let data = new FormData()
                data.append('id', this.activeid)
                applyOrderStatusAdmin(data).then(data => {
                    console.log(data);
                    if (data.code == 200) {
                        this.$message({
                            message: '已修改审核状态',
                            type: 'success'
                        })
                        this.dialogFormVisible = false
                        this.getData()
                    }
                    if (data.code == 0) {
                        this.$message({
                            message: data.message,
                            type: 'error'
                        })
                    }
                })
            }
            if (this.status_radio == '6') {
                let data = new FormData()
                data.append('id', this.activeid)
                data.append('reason', this.textarea)
                rejectOrderAdmin(data).then(data => {
                    console.log(data);
                    if (data.code == 200) {
                        this.$message({
                            message: '修改成功',
                            type: 'success'
                        })
                        this.dialogFormVisible = false
                        this.getData()
                    }
                    if (data.code == 0) {
                        this.$message({
                            message: data.message,
                            type: 'error'
                        })
                    }
                })
            }

        },
        // 弹出层相关方法
        // 提交表单
        submitForm() {
            let total = this.ruleForm.price * this.ruleForm.days * this.ruleForm.num
            let data = new FormData()
            data.append('id', this.activeid)
            data.append('price', total.toFixed(2))
            editOrderAdmin(data).then(data => {
                console.log(data);
                if (data.code == 200) {
                    this.dialogTableVisible = false
                    this.$message({
                        message: '修改成功',
                        type: 'success'
                    })
                    this.getData()
                }
                if (data.code == 0) {
                    this.$message({
                        message: data.message + ',需要保留两位小数',
                        type: 'error'
                    })
                }
            })
        },
        //取消添加或编辑
        cancelForm() {
            this.dialogTableVisible = false
        },

    },
    mounted() {
        this.getData()
    },
}
</script>

<style scoped lang="less">
.redTips {
    color: red;
    display: inline-block;
    margin-left: 20px;
    font-size: 14px;
}

// 提示信息
.tips {
    margin: 30px;
    background-color: #e9ecf9;
    border-radius: 11px;

    .tipsIcon {
        margin: 10px 15px;
        display: inline-block;
        width: 24px;
        height: 24px;
        background: url("../../assets/advertise/Info Circle.png") no-repeat;
        vertical-align: middle;
    }

    .tipsText {
        font-size: 14px;
        color: #666666;
    }
}

// 头部
.title {
    margin: 30px 30px 20px 30px;
    padding: 30px 30px 40px 30px;
    background-color: #fff;
    border-radius: 20px 20px 20px 20px;
    border: 1px solid #E9EDF7;

    .left {
        float: left;
    }

    .right {
        float: right;
    }


    .searchBox {
        ::v-deep .el-input {
            position: relative;
            font-size: 14px;
            display: inline-block;
            width: 90%;
        }

        .searchTitle {
            padding-bottom: 10px;
            font-family: Microsoft YaHei, Microsoft YaHei;
            font-weight: 400;
            font-size: 14px;
            color: #999999;
            line-height: 16px;
        }

        .el-select {
            width: 100%;
            display: inline-block;
            position: relative;
        }
    }


    .btnList {
        float: right;
        padding-top: 28px;

        button {
            height: 38px;
            border: none;
            border-radius: 8px;
            padding: 0 20px;
        }

        .search {
            background-color: #5570f1;
            color: #fff;
            margin-right: 20px;
        }

        .reset {
            font-family: Microsoft YaHei, Microsoft YaHei;
            font-weight: 400;
            font-size: 16px;
            color: #333333;
            background: #F5F7FB;
            border-radius: 8px 8px 8px 8px;
            border: 1px solid rgba(85, 112, 241, 0.11);
        }
    }
}

.layerBox {
    padding: 30px 20px;
    position: relative;

    .btn {
        position: absolute;
        top: 30px;
        right: 20px;
        height: 38px;
        color: #fff;
        background-color: #5570f1;
        border: none;
        border-radius: 8px;
        padding: 8px 28px 9px;
        box-sizing: border-box;
    }

    .listBtnBox {
        justify-content: left;
    }


    .listDeleteBtn,
    .listEditBtn,
    .listReviewBtn {
        margin-left: 0px;
        padding-left: 0px;
        margin-right: 20px;
        width: 76px;
        height: 36px;
        line-height: 36px;
    }


    .listReviewBtn {
        text-align: center;
        border-radius: 8px;
        cursor: pointer;
        color: #519C66;
        background-color: rgba(81, 156, 102, 0.16);

        >.review {
            display: inline-block;
            width: 20px;
            height: 20px;
            background: url("../../assets/advertise/Shield.png");
            margin-right: 6px;
            vertical-align: -6px;
        }
    }

    ::v-deep .el-form-item {
        margin-bottom: 50px;
    }

    ::v-deep .el-select {
        width: 100%;
    }

    ::v-deep .el-input--medium,
    ::v-deep .el-form-item__label {
        line-height: 36px;
        font-size: 16px;
    }
}

// 弹出层内容
.dialogText {
    margin: 0 7px 0 3px;
    padding-bottom: 1px;
    padding: 0px 60px 1px 20px;

    .zhi {
        text-align: center;
    }

    .adImage {
        width: 140px;
        height: 140px;
        // line-height: 210px;
        border-radius: 12px;
        border: 1px solid rgba(85, 112, 241, 0.11);

        img {
            width: 140px;
            // height: 80px;
        }
    }

    // .price {
    //     ::v-deep .el-input {
    //         width: 29%;
    //     }
    // }

    .example {
        font-family: Microsoft YaHei;
        color: #5570F1;
    }

    .el_btnList {
        margin-right: 15px;
        margin-top: 20px;
    }

    //日期时间选择器的宽
    ::v-deep .el-date-editor.el-input {
        width: 100%;
    }

    ::v-deep .el-button+.el-button {
        margin-left: 0px;
    }
}

// 弹出层按钮
.dialogBtn {
    text-align: center;
    margin: 50px auto 20px;

    button {
        width: 184px;
        padding: 16px;
        font-family: Microsoft YaHei, Microsoft YaHei;
        font-weight: 400;
        font-size: 20px;
        border: none;
        border-radius: 12px 12px 12px 12px;
    }

    // 取消
    .cancel {
        color: #333333;
        background-color: #f5f7fb;
        border: 2px solid rgba(85, 112, 241, 0.11);
    }

    // 提交
    .submit {
        color: #fff;
        background-color: #5570F1;
        margin-left: 40px;
    }
}

//审核弹出框
.radioGroup {
    ::v-deep .el-form-item {
        margin-top: 40px;
        margin-bottom: 0;
    }
}

.graph {
    background-color: #f5f7fb;
    padding: 60px 100px;
    overflow: hidden;

    li {
        float: left;
    }

    >li:first-child {
        margin-right: 100px;
    }
}

.dialog-footer {
    margin: 0 auto;
}
</style>