<template>
    <div>
        <!-- <el-button type="primary" @click="resetting">添加</el-button> -->
        <el-table :data="tableData">
            <!-- <el-table-column label="类型" prop="type">
                <template v-slot="row">
                    {{ row.row.type == 'text' ? '文本' : '图片' }}
                </template>
</el-table-column> -->
            <el-table-column label="描述" prop="name"> </el-table-column>
            <el-table-column label="图片" prop="value">
                <template v-slot="row">
                    <img v-if="row.row.type == 'img'" :src="row.row.value" style="width: 40px;height: 40px;">
                    <span v-else>{{ row.row.value }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="250">
                <template v-slot="scope">
                    <el-button type="text" size="mini" round icon="el-icon-edit" @click="edit(scope.row)">编辑</el-button>
                    <!-- <el-popconfirm title="确定删除吗？" @confirm="delHandle([scope.row.id])">
                        <el-button type="text" :disabled="!scope.row.id" size="mini" round icon="el-icon-delete" slot="reference">
                            删除
                        </el-button>
                    </el-popconfirm> -->
                </template>
            </el-table-column>
        </el-table>
        <el-dialog :title="title" :visible.sync="showLocation">
            <el-form ref="form" :model="form" :rules="rules" label-width="100px" label-position="left">
                <el-form-item label="类型" v-if="title == '新增'">
                    <el-radio v-model="form.type" label="text">文本</el-radio>
                    <el-radio v-model="form.type" label="img">图片</el-radio>
                </el-form-item>

                <el-form-item label="描述">
                    <el-input v-model="form.name" placeholder="请输入内容"></el-input>
                </el-form-item>
                <!-- <el-form-item label="自定义key">
                    <el-input v-model="form.autokey" placeholder="请输入内容"></el-input>
                </el-form-item> -->
                <el-form-item label="上传图片" v-if="form.type == 'img'">
                    <el-upload :headers="headers" class="avatar-uploader" :action="qiNiuUploadApi"
                        :show-file-list="false" :on-success="handleSuccess">
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                    <span>{{ form.detail }}</span>
                </el-form-item>
                <el-form-item label="文本" v-else>
                    <el-input v-model="form.value" placeholder="请输入内容"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="showLocation = false">取消</el-button>
                <el-button type="primary" @click="doSubmit">确认</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { tbShopExtend, addtbShopExtend, deletetbShopExtend, querytbShopExtend, edittbShopExtend } from '@/api/extend'
import { getToken } from "@/utils/auth";
import { mapGetters } from "vuex";
export default {
    computed: {
        ...mapGetters(["qiNiuUploadApi"])
    },
    data() {
        return {
            tableData: [],
            showLocation: false,
            fileList: [],
            form: {
                type: 'text',
            },
            imageUrl: '',
            title: '新增',
            rules: {
            },
            headers: {
                Authorization: getToken()
            },
        }
    },
    mounted() {
        this.getList()
    },
    watch: {
        'form.type': (n, o) => {
            if (n == 'img') {
            }
        }
    },
    methods: {
        handleBeforeRemove(file, fileList) {},
        async edit(item) {
            this.title = '编辑'
            this.showLocation = true
            this.form = item
            this.imageUrl = item.value
        },
        // 删除
        async delHandle(proId) {
            let res = await deletetbShopExtend({ idList: proId[0] })
            this.$message({
                message: '删除成功',
                type: 'success'
            })
            this.getList()
        },
        // 刷新列表数据
        async doSubmit() {
            if (this.title == '新增') {
                let res = await addtbShopExtend({
                    ...this.form,
                    shopId: localStorage.getItem('shopId')
                })
                this.$message({
                    message: '添加成功',
                    type: 'success'
                })
            } else {
                let res = await edittbShopExtend({
                    ...this.form,
                })
                this.$message({
                    message: '编辑成功',
                    type: 'success'
                })
            }
            this.form = null
            this.form = {
                type: 'text',
            }
            this.imageUrl = ''
            this.showLocation = false
            this.getList()
        },
        handleSuccess(response, file, fileList) {
            this.imageUrl = response.data[0];
            this.form.value = response.data[0]
            console.log("上传成功", this.form);
        },
        async getList() {
            let res = await tbShopExtend({
                shopId: localStorage.getItem('shopId')
            })
            this.tableData = res.content
        },
        resetting() {
            this.title = '新增'
            this.form = null
            this.form = {
                type: 'text',
            }
            this.imageUrl = ''
            this.showLocation = true
        },
        addtbShopExtend() {}
    }
}
</script>
<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>