<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" @submit.native.prevent :inline="true" v-show="showSearch" label-width="68px">
            <!--      <el-form-item label="轮播图类型" prop="bannerType" label-width="120px">-->
            <!--        <el-select v-model="queryParams.bannerType" placeholder="轮播图类型" clearable>-->
            <!--          <el-option-->
            <!--            v-for="dict in dict.type.app_banner_type"-->
            <!--            :key="dict.value"-->
            <!--            :label="dict.label"-->
            <!--            :value="dict.value"-->
            <!--          />-->
            <!--        </el-select>-->
            <!--      </el-form-item>-->
            <!--      <el-form-item label="点击类型" prop="clickType">-->
            <!--        <el-select v-model="queryParams.clickType" placeholder="点击类型" clearable>-->
            <!--          <el-option-->
            <!--            v-for="dict in dict.type.app_banner_click_type"-->
            <!--            :key="dict.value"-->
            <!--            :label="dict.label"-->
            <!--            :value="dict.value"-->
            <!--          />-->
            <!--        </el-select>-->
            <!--      </el-form-item>-->
            <el-form-item prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
                    <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd" v-hasPermi="[bannerType == 0 ? 'app:banner:add' : 'app:launchAdver:add']">新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <Page uri="/app/banner/list" :request-params="queryParams" ref="pagination">
            <el-table-column label="ID" align="center" prop="id" />
            <el-table-column label="图片" align="center" prop="imgUrl" width="100">
                <template slot-scope="scope">
                    <el-image style="width: 80px; height: 80px" :src="scope.row.imgUrl" :preview-src-list="[scope.row.imgUrl]"> </el-image>
                </template>
            </el-table-column>
            <el-table-column label="标题" align="center" prop="redirectTitle" />
            <!--            <el-table-column label="图片类型" align="center" prop="bannerType">-->
            <!--                <template slot-scope="scope">-->
            <!--                    <dict-tag :options="dict.type.app_banner_type" :value="scope.row.bannerType" />-->
            <!--                </template>-->
            <!--            </el-table-column>-->
            <el-table-column label="点击类型" align="center" prop="clickType">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.app_banner_click_type" :value="scope.row.clickType" />
                </template>
            </el-table-column>

            <el-table-column label="启用状态" align="center" prop="status">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
                </template>
            </el-table-column>
            <el-table-column label="是否热销" align="center" prop="isHot" width="120" v-if="bannerType == 3">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isHot" />
                </template>
            </el-table-column>
          <el-table-column label="排序" align="center" prop="sort" />
            <el-table-column label="创建人" align="center" prop="createBy" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
            <el-table-column label="修改人" align="center" prop="updateBy" />
            <el-table-column label="修改时间" align="center" prop="createTime" width="180" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="[bannerType == 0 ? 'app:banner:edit' : 'app:launchAdver:edit']">修改</el-button>
                    <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="[bannerType == 0 ? 'app:banner:remove' : 'app:launchAdver:remove']">删除</el-button>
                </template>
            </el-table-column>
        </Page>

        <!-- 添加或修改轮播图对话框 -->
        <el-dialog :title="title" :visible.sync="open" size="30%" append-to-body>
            <el-row :gutter="15">
                <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                    <el-col :span="24">
                        <el-form-item label="上传" prop="imgUrl">
                            <el-upload class="image-uploader" :action="uploadAction" :before-upload="handleBeforeUpload" :on-success="handleUploadSuccess" :on-error="handleUploadError" accept="image/*" :show-file-list="false">
                                <img v-if="form.imgUrl" :src="form.imgUrl" />
                                <i v-else class="el-icon-plus image-uploader-icon"></i>
                            </el-upload>
                        </el-form-item>
                    </el-col>
                    <!--          <el-col :span="24">-->
                    <!--            <el-form-item label="轮播图类型" prop="bannerType">-->
                    <!--              <el-radio-group v-model="form.bannerType" size="small">-->
                    <!--                <el-radio-button v-for="dict in dict.type.app_banner_type" :label="dict.value">{{dict.label}}</el-radio-button>-->
                    <!--              </el-radio-group>-->
                    <!--            </el-form-item>-->
                    <!--          </el-col>-->
                  <el-col :span="24">
                    <el-form-item label="标题" prop="redirectTitle">
                      <el-input v-model="form.redirectTitle" placeholder="请输入标题" />
                    </el-form-item>
                  </el-col>
                    <el-col :span="24">
                        <el-form-item label="点击类型" prop="clickType">
                            <el-radio-group v-model="form.clickType" size="small" @input="onClickTypeChange">
                                <el-radio-button v-for="dict in dict.type.app_banner_click_type" :label="dict.value">{{ dict.label }}</el-radio-button>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24" v-if="form.clickType == '1'">
                        <el-form-item label="跳转内容" prop="redirectContent">
                            <el-input v-model="form.redirectContent" placeholder="请输入跳转网址" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24" v-if="form.clickType == '2'">
                        <el-form-item label="跳转内容" prop="redirectContent">
                            <editor v-model="form.redirectContent" :min-height="192" />
                        </el-form-item>
                    </el-col>
                  <el-col :span="24" v-if="form.clickType == '3'">
                    <el-form-item label="跳转页面" >
                      <el-select placeholder="请选择页面" v-model="pageSelectUrl" @change="switchPage">
                        <el-option v-for="dict in pageData" :key="dict.id" :label="dict.name" :value="dict.url" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                    <el-col :span="24" v-if="form.clickType == '3'">
                        <el-form-item label="页面地址" prop="redirectContent">
                            <el-input v-model="form.redirectContent" placeholder="请输入内部页面路径" />
                            <span style="color: #888a8e">
                              如果地址包含?，则要修改页面地址，把?内容换成对应业务的id
                            </span>
                        </el-form-item>
                    </el-col>
                  <el-col :span="24">
                    <el-form-item label="排序" prop="sort">
                      <el-input-number v-model="form.sort" placeholder="请输入排序号" />
                    </el-form-item>
                  </el-col>
                    <el-col :span="24">
                        <el-form-item label="状态" prop="status">
                            <el-radio-group v-model="form.status" size="small">
                                <el-radio-button v-for="dict in dict.type.sys_normal_disable" :label="dict.value">{{ dict.label }}</el-radio-button>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm" style="margin-left: 5%">提交</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listBanner, getBanner, delBanner, addBanner, updateBanner, getAllPageLink } from '@/api/app/banner'

export default {
    name: 'Banner',
    dicts: ['app_banner_type', 'app_banner_click_type', 'sys_yes_no', 'sys_normal_disable'],
    data() {
        return {
            uploadAction: `${process.env.VUE_APP_BASE_API}` + '/common/uploadOSS',
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
          pageSelectUrl: '',
            // 轮播图表格数据
            bannerList: [],
            pageData: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                bannerType: null,
                imgUrl: null,
                clickType: null,
                status: null,
                createById: null,
                updateById: null
            },
            // 表单参数
            form: {
              sort: 0
            },
            // 表单校验
            rules: {
                bannerType: [
                    {
                        required: true,
                        message: '图片类型不能为空',
                        trigger: 'blur'
                    }
                ],
                imgUrl: [
                    {
                        required: true,
                        message: '请先上传图片',
                        trigger: 'blur'
                    }
                ],
                clickType: [
                    {
                        required: true,
                        message: '点击类型不能为空',
                        trigger: 'blur'
                    }
                ],
                status: [
                    {
                        required: true,
                        message: '状态不能为空',
                        trigger: 'blur'
                    }
                ],
                redirectContent: [
                    {
                        required: true,
                        message: '跳转内容不能为空',
                        trigger: 'blur'
                    }
                ]
            },
            bannerType: ''
        }
    },
    created() {
        this.bannerType = this.getUrlParam('bannerType')
        this.queryParams.bannerType = this.bannerType
        this.form.bannerType = this.bannerType
        this.getList()
        this.getPageLink()
    },
    methods: {
      //获取快递公司
      getPageLink() {
        getAllPageLink().then((res) => {
          this.pageData = res.data
        })
      },
      switchPage(){
        if(this.pageSelectUrl){
          this.form.redirectContent = this.pageSelectUrl
        } else {
          this.form.redirectContent = ''
        }
      },
        onClickTypeChange() {
            this.form.redirectContent = ''
            this.pageSelectUrl = ''
        },
        // 上传前校检格式和大小
        handleBeforeUpload(file) {
            // 校检文件大小
            if (this.fileSize) {
                const isLt = file.size / 1024 / 1024 < this.fileSize
                if (!isLt) {
                    this.$message.error(`上传文件大小不能超过 ${this.fileSize} MB!`)
                    return false
                }
            }
            return true
        },
        handleUploadSuccess(res, file) {
            // 获取富文本组件实例
            let quill = this.Quill
            // 如果上传成功
            if (res.code == 200) {
                // 获取光标所在位置
                this.form.imgUrl = res.data.src
                this.$forceUpdate()
            } else {
                this.$message.error('图片插入失败')
            }
        },
        handleUploadError() {
            this.$message.error('图片插入失败')
        },
        /** 查询轮播图列表 */
        getList() {
            this.$nextTick(() => {
                this.$refs.pagination.handleSearch(true)
            })
        },
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                bannerType: this.bannerType,
                imgUrl: null,
                clickType: null,
                redirectContent: null,
                status: '0',
                 sort: 0
            }
            this.pageSelectUrl = ''
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加轮播图'
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const id = row.id || this.ids
            getBanner(id).then((response) => {
                this.form = response.data
                this.open = true
                this.title = '修改轮播图'
            })
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateBanner(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addBanner(this.form).then((response) => {
                            this.$modal.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids
            this.$modal
                .confirm('是否确认删除轮播图编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delBanner(ids)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('删除成功')
                })
                .catch(() => {})
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'app/banner/export',
                {
                    ...this.queryParams
                },
                `banner_${new Date().getTime()}.xlsx`
            )
        }
    }
}
</script>
