<template>
    <!-- 广告位管理 -->
    <div>
        <!-- 头部搜索框部分 -->
        <div class="title" v-if="this.tabbarType == 1">
            <el-row>
                <el-col :span="6" class="left">
                    <div class="searchBox">
                        <div class="searchTitle">列表标题</div>
                        <el-input v-model="listTitle" class="input" placeholder="请输入列表标题"></el-input>
                    </div>
                </el-col>
                <el-col :span="6" class="left">
                    <div class="searchBox">
                        <div class="searchTitle">内容标题</div>
                        <el-input v-model="contentTitle" class="input" placeholder="请输入内容标题"></el-input>
                    </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" />
            <button class="btn" @click="addWebsite">添加详情</button>
            <el-row>
                <template>
                    <el-table class="my-table" :data="tableData" style="width: 100%">
                        <el-table-column fixed prop="id" label="编号" width="90">
                        </el-table-column>
                        <el-table-column prop="list_title" label="列表标题" width="" v-if="this.tabbarType == 1">
                        </el-table-column>
                        <el-table-column prop="con_title" label="内容标题" width="">
                        </el-table-column>
                        <el-table-column prop="created_at" label="添加时间" width="">
                        </el-table-column>
                        <el-table-column prop="updated_at" label="修改时间" width="">
                        </el-table-column>
                        <el-table-column fixed="right" label="操作" width="200">
                            <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>
                            </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="50%" top="8vh"
            :close-on-click-modal="false">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
                <div class="dialogText">
                    <el-form-item label="列表标题:" prop="listTitle" v-if="this.tabbarType == 1">
                        <el-input v-model="ruleForm.listTitle" placeholder="请输入列表标题"></el-input>
                    </el-form-item>
                    <el-form-item label="内容标题:" prop="contentTitle">
                        <el-input v-model="ruleForm.contentTitle" placeholder="请输入内容标题"></el-input>
                    </el-form-item>
                     <el-form-item label="内容详情" prop="contentDetail">
                        <myEditor ref="myEditor" v-model="ruleForm.contentDetail"></myEditor>
                    </el-form-item>
                    <!--el-form-item label="内容详情:" prop="contentDetail">
                        <el-input v-model="ruleForm.contentDetail" type="textarea" :rows="10" placeholder="请输入内容详情"
                            resize="none">
                        </el-input>
                    </el-form-item-->
                </div>
                <div class="dialogBtn">
                    <el-button type="info" @click="cancelForm">取消</el-button>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                </div>
            </el-form>
        </el-dialog>
        <!-- 弹出框 编辑 end----------------------------------------------------------->
    </div>
</template>

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

//引入公用样式
import '@/styles/global.less';
import { addFooterContent, getFooterContent, upFooterContent, delFooterContent } from '@/api/tabbar'
//引入富文本编辑器
import myEditor from '../../components/edit/myEditor.vue';
export default {
    components: {
        tableTitle,//表格标题
        myEditor,
    },
    data() {
        const validateEmpty = (rule, value, callback) => {
            if (value.length == 0) {
                callback(new Error('该项不能为空！'))
            } else {
                callback()
            }
        }
        return {
            tableDivTitle: "单页详情",  //列表标题
            dialogTableVisible: false,  //编辑弹框
            dialogName: '编辑', //编辑弹窗名称

            tableData: [],//表格数据
            tabbarId: '',//底部导航传递的id
            tabbarType: 1,//底部导航传递的类型

            //活动id
            activeid: "",

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


            // 搜索框相关
            listTitle: '', //网站名称id
            contentTitle: '', //广告位名称

            //弹框相关
            ruleForm: {
                listTitle: '', //列表标题
                contentTitle: '', //内容标题
                contentDetail: '', //内容详情
            },
            rules: {
                listTitle: [{ required: true, trigger: 'blur', validator: validateEmpty }],
                contentTitle: [{ required: true, trigger: 'blur', validator: validateEmpty }],
                contentDetail: [{ required: true, trigger: 'blur', validator: validateEmpty }],
            }
        }
    },
    methods: {
        //1.列表和分页相关 start ------------------------------------------------------------>
        //1.1 开始请求列表信息方法
        getData() {
            getFooterContent({
                fcat_id: this.tabbarId,
                page: this.page,
                pageSize: this.pageSize
            }).then(data => {
                console.log(data);
                this.tableData = data.data.rows
                this.total = data.data.count
            })
        },

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

                delFooterContent({
                    id: id
                }).then(data => {
                    console.log(data);
                    if (data.code = 200) {
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        })
                        this.getData()
                    } else if (data.code == 0) {
                        this.$message({
                            message: data.message,
                            type: 'error'
                        })
                    }
                })

            }).catch(() => {
                this.$message({
                    type: 'warning',
                    message: '已取消删除'
                });
            });
        },

        //1.3 列表内容分页
        //直接跳转
        handleSizeChange(val) {
            this.page = val;
            this.getData();
        },
        //1.4 点击分页
        handleCurrentChange(val) {
            this.page = val;
            this.getData();
        },
        // 1.5 搜索按钮
        goSearch() {
            getFooterContent({
                fcat_id: this.tabbarId,
                page: this.page,
                pageSize: this.pageSize,
                list_title:this.listTitle,
                con_title:this.contentTitle,
            }).then(data => {
                console.log(data);
                this.tableData = data.data.rows
                this.total = data.data.count
            })
        },
        //1.6 重置按钮
        goReset() {
            this.listTitle = ""
            this.contentTitle = ""
            this.getData()
        },
        //列表和分页相关 end ------------------------------------------------------------>

        //1.7 编辑
        goEdit(id, val) {
            // console.log(id);
            this.dialogName = '编辑'
            this.activeid = id
            this.dialogTableVisible = true
            console.log(id, val);

            //数据回显
            this.ruleForm.listTitle = val.list_title //列表标题
            this.ruleForm.contentTitle = val.con_title //内容标题
            this.$nextTick(() => {
                this.ruleForm.contentDetail = val.content //内容详情
            });
            
        },

        //1.8 添加
        addWebsite() {
            this.dialogTableVisible = true
            this.dialogName = "添加"

            //添加时清空回显回来的数据
            this.ruleForm.contentTitle = '' //内容标题
            this.ruleForm.contentDetail = '' //内容详情
        },

        // 弹出层相关方法
        // 1.9 提交表单
        submitForm(formName) {
            // 判断用户是否输入数据
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    console.log('submit!');
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
            if (this.dialogName == "添加") {
                if (this.tabbarType == 0) { //详情页类型
                    addFooterContent({
                        fcat_id: this.tabbarId,
                        type: this.tabbarType,
                        con_title: this.ruleForm.contentTitle,
                        content: this.ruleForm.contentDetail,
                    }).then(data => {
                        console.log(data);
                        if (data.code == 200) {
                            this.$message({
                                message: '添加成功',
                                type: 'success'
                            })
                            this.dialogTableVisible = false
                            this.getData()
                        } else {
                            this.$message({
                                message: data.message,
                                type: 'error'
                            })
                        }
                    })
                } else if (this.tabbarType == 1) { //列表页类型
                    addFooterContent({
                        fcat_id: this.tabbarId,
                        type: this.tabbarType,
                        list_title: this.ruleForm.listTitle,
                        con_title: this.ruleForm.contentTitle,
                        content: this.ruleForm.contentDetail,
                    }).then(data => {
                        console.log(data);
                        if (data.code == 200) {
                            this.$message({
                                message: '添加成功',
                                type: 'success'
                            })
                            this.dialogTableVisible = false
                            this.getData()
                        } else {
                            this.$message({
                                message: data.message,
                                type: 'error'
                            })
                        }
                    })
                }
            }
            if (this.dialogName == "编辑") {
                if (this.tabbarType == 0) { //详情页类型
                    upFooterContent({
                        id: this.activeid, //内容id
                        type: this.tabbarType,
                        con_title: this.ruleForm.contentTitle,
                        content: this.ruleForm.contentDetail,
                    }).then(data => {
                        console.log(data);
                        if (data.code == 200) {
                            this.$message({
                                message: '修改成功!',
                                type: 'success'
                            })
                            this.dialogTableVisible = false
                            this.getData()
                        } else {
                            this.$message({
                                message: data.message,
                                type: 'error'
                            })
                        }
                    })
                } else if (this.tabbarType == 1) { //列表页类型
                    upFooterContent({
                        id: this.activeid, //内容id
                        type: this.tabbarType,
                        list_title: this.ruleForm.listTitle,
                        con_title: this.ruleForm.contentTitle,
                        content: this.ruleForm.contentDetail,
                    }).then(data => {
                        console.log(data);
                        if (data.code == 200) {
                            this.$message({
                                message: '修改成功!',
                                type: 'success'
                            })
                            this.dialogTableVisible = false
                            this.getData()
                        } else {
                            this.$message({
                                message: data.message,
                                type: 'error'
                            })
                        }
                    })
                }
            }

        },
        //取消
        cancelForm() {
            this.dialogTableVisible = false
        },
    },
    mounted() {
        console.log(this.$route.query.id);
        this.tabbarId = this.$route.query.id
        this.tabbarType = this.$route.query.type

        this.getData() //获取数据
    },
}
</script>

<style scoped lang="less">
input[aria-hidden=true] {
    display: none !important;
}

// 提示信息
.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: 80%;
        }

        .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 30px;
        }

        .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 {
        margin-left: 0px;
        padding-left: 0px;
        margin-right: 20px;
        width: 76px;
        height: 36px;
        line-height: 36px;
    }

    ::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: 30px 60px 1px 20px;
    background-color: #f5f7fb;

    .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;
        }
    }



    ::v-deep .avatar {
        width: 140px;
        height: auto;
    }

    .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: 48%;
    }

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

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

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

// 弹出层按钮
.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>