<template>
    <div style="padding: 20px;">
        <el-form ref="query" style="display: flex;" :model="query" label-width="80px">
            <el-form-item label="展示位置">
                <el-select clearable v-model="query.showPosition" placeholder="请选择">
                    <el-option label="首页" value="home" />
                    <el-option label="点餐页" value="make_order" />
                </el-select>
            </el-form-item>
            <el-form-item label="状态">
                <el-select clearable v-model="query.status" placeholder="请选择">
                    <el-option label="可见" value="1" />
                    <el-option label="不可见" value="2" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="getList">查询</el-button>
                <el-button type="primary" @click="dialogVisible = true; title = '新增'">新增</el-button>
            </el-form-item>
        </el-form>
        <!--表格渲染-->
        <el-table ref="table" :data="tableData" style="width: 100%;">
            <el-table-column label="弹窗广告">
                <template v-slot="scope">
                    <img :src="scope.row.imgUrl" style="width: 100px;height: 100px;">
                </template>
            </el-table-column>
            <el-table-column prop="showPosition" label="弹窗位置">
                <template v-slot="scope">
                    {{ scope.row.showPosition == 'home' ? '首页' : "点餐页" }}
                </template>
            </el-table-column>
            <el-table-column prop="showPosition" label="是否可见">
                <template v-slot="scope">
                    <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0"
                        @change="showChange(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <el-table-column prop="" label="弹窗频率">
                <template v-slot="scope">
                    {{ scope.row.frequency | frequencyFilter }}
                </template>
            </el-table-column>
            <el-table-column prop="createTime" label="创建日期" />
            <el-table-column label="操作" width="200">
                <template v-slot="scope">
                    <el-button type="text" @click="edit(scope.row)">编辑</el-button>
                    <el-popconfirm title="确定删除吗？" @confirm="delHandle(scope.row.id)">
                        <el-button type="text" round slot="reference">
                            删除
                        </el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!-- <div class="">
            <el-pagination :total="tableData.length" layout="total,  prev, pager, next, jumper"></el-pagination>
        </div> -->
        <!-- 增减余额弹窗 -->
        <el-dialog :title="title + '广告'" :visible.sync="dialogVisible" width="60%">
            <el-form ref="form" :model="form" label-width="120px">
                <el-form-item label="上传弹窗广告">
                    <!-- <el-upload

  :on-preview="handlePictureCardPreview"
  :on-remove="handleRemove">
  <i class="el-icon-plus"></i>
</el-upload> -->


                    <el-upload class="upload-demo" :action="qiNiuUploadApi" list-type="picture-card" :headers="headers"
                        :limit="1" :on-remove="removeEvent" :on-success="successEvent" :file-list="fileList">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                    <div slot="tip" class="el-upload__tip">建议尺寸：520px*680px</div>
                </el-form-item>
                <el-form-item label="圆角设置">
                    <el-slider v-model="form.borderRadius" :max="20"></el-slider>
                </el-form-item>
                <el-form-item label="弹窗位置">
                    <el-radio-group v-model="form.showPosition">
                        <el-radio label="home">首页</el-radio>
                        <el-radio label="make_order">点餐页</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="弹窗频率">
                    <el-radio-group v-model="form.frequency">
                        <el-radio label="only_one">仅首次打开显示</el-radio>
                        <el-radio label="every_show">每次打开显示一次</el-radio>
                        <el-radio label="thirty_day">每30天显示一次</el-radio>
                        <el-radio label="seven_day">每7天显示一次</el-radio>
                        <el-radio label="three_day">每3天显示一次</el-radio>
                        <el-radio label="every_day">每1天显示一次</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="跳转页面">
                    <el-select clearable v-model="form.linkPath" placeholder="请选择">
                        <el-option v-for="item in tourl" :key="item.id" :label="item.name" :value="item.path">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="sumbit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import { miniAppPagesget } from '@/api/goxcx'
import { getToken } from '@/utils/auth'
import { adget, adpost, adput, addelete } from '@/api/application'
import { mapGetters } from 'vuex'
export default {
    computed: {
        ...mapGetters(['qiNiuUploadApi'])
    },
    data() {
        return {
            headers: {
                Authorization: getToken()
            },
            tourl: [],
            query: {
                showPosition: "", status: ""
            },
            form: {
                sort: "",
                path: "",
                shopId: localStorage.getItem("shopId"),
                showPosition: 'home',
                frequency: "only_one"
            },
            title: '新增',
            dialogVisible: false,
            tableData: [],
            fileList: [],
        }
    },
    mounted() {
        this.getList()
        // 获取跳转页面
        this.getto()
    },
    filters: {
        frequencyFilter(key) {
            let str = ""
            switch (key) {
                case 'only_one':
                    str = '仅首次展示'
                    break;
                case 'every_show':
                    str = '每次打开都展示'
                    break;
                case 'every_day':
                    str = '每天展示一次'
                    break;
                case 'three_day':
                    str = '每三天展示一次'
                    break;
                case 'seven_day':
                    str = '每七天展示一次'
                    break;
                case 'thirty_day':
                    str = '每30天展示一次'
                    break;
            }
            return str
        },
    },

    methods: {
        successEvent(file) {
            this.fileList = [{ url: file.data[0], name: '图片' }]
        },
        removeEvent(file) {
            // 删除数据
            let index = this.fileList.findIndex(ele => ele.url == file.response.data[0])
            this.fileList.splice(index, 1);
        },
        async showChange(d) {
            if (!d.imgUrl) {
                this.form.imgUrl = this.fileList
            }
            await adput({
                imgUrl:d.imgUrl,
                id: d.id,
                shopId: this.form.shopId,
                status: d.status,
            })
            this.$message({
                message: '成功',
                type: 'success'
            })
            this.getList()
        },
        async getto() {
            let res = await miniAppPagesget()
            this.tourl = res
        },
        async sumbit() {
            this.form.imgUrl = this.fileList[0].url
            this.dialogVisible = false
            if (this.title == '新增') {
                await adpost(this.form)
                this.$message({
                    message: '添加成功',
                    type: 'success'
                })
            } else {
                await adput(this.form)
                this.$message({
                    message: '编辑成功',
                    type: 'success'
                })
            }
            this.form = {
                sort: "",
                path: "",
                status: 1,
                shopId: localStorage.getItem("shopId"),
                showPosition: 'home',
                frequency: "only_one"
            }
            this.fileList = []
            this.getList()
        },
        // 编辑
        async edit(item) {
            this.title = '编辑'
            this.dialogVisible = true
            this.form = item
            console.log(item.imgUrl, '调试1')
            this.fileList = [{ url: item.imgUrl, name: '图片' }]
        },
        // 删除
        async delHandle(adId) {
            let res = await addelete(adId)
            this.$message({
                message: '删除成功',
                type: 'success'
            })
            this.getList()
        },
        async getList() {
            let res = await adget({
                shopId: localStorage.getItem("shopId"),
                ...this.query
            })
            this.tableData = res
        },

    }
}
</script>
<style></style>