<template>
    <el-dialog :title="form.id ? '编辑店铺' : '添加店铺'" :visible.sync="dialogVisible" @close="reset">
        <div style="height: 50vh;overflow-y: auto;">
            <el-form ref="form" :model="form" :rules="rules" label-width="120px" label-position="left">
                <el-form-item label="店铺名称" prop="shopName">
                    <el-input v-model="form.shopName" placeholder="请输入门店名称"></el-input>
                </el-form-item>
                <el-form-item label="门店logo" prop="logo">
                    <el-image :src="form.logo || require('@/assets/images/upload.png')" fit="contain"
                        style="width: 80px;height: 80px;" @click="showUpload = true; uploadIndex = 1"></el-image>
                </el-form-item>
                <el-form-item label="门店照片">
                    <el-image :src="form.coverImg || require('@/assets/images/upload.png')" fit="contain"
                        style="width: 80px;height: 80px;" @click="showUpload = true; uploadIndex = 2"></el-image>
                </el-form-item>
                <el-form-item label="店铺类型">
                    <el-radio-group v-model="form.registerType">
                        <el-radio-button label="munchies">快餐版</el-radio-button>
                        <el-radio-button label="restaurant">餐饮版</el-radio-button>
                    </el-radio-group>
                    <div class="tips">请谨慎修改！！！</div>
                </el-form-item>
                <el-form-item label="试用/正式">
                    <el-radio-group v-model="form.profiles">
                        <el-radio-button label="probation">试用</el-radio-button>
                        <el-radio-button label="release">正式</el-radio-button>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="激活码">
                    <el-input v-model="form.registerCode" placeholder="请输入激活码"></el-input>
                    <div class="tips">注：输入有效激活码表示添加的同时直接激活该店铺。</div>
                </el-form-item>
                <el-form-item label="登录账号" prop="account">
                    <el-input v-model="form.account" placeholder="请输入登录账号"></el-input>
                </el-form-item>
                <el-form-item label="登录密码" prop="password">
                    <el-input type="password" show-password v-model="form.password" placeholder="请输入登录密码"></el-input>
                </el-form-item>
                <el-form-item label="联系电话">
                    <el-input v-model="form.phone" placeholder="请输入联系电话"></el-input>
                </el-form-item>
                <el-form-item label="设备数量">
                    <el-input-number v-model="form.supportDeviceNumber" controls-position="right" :min="1" :step="1"
                        step-strictly></el-input-number>
                </el-form-item>
                <!-- <el-form-item label="外卖起送金额">
                    <el-input-number v-model="form.takeaway_money" placeholder="0.00" controls-position="right"
                        :min="0"></el-input-number>
                </el-form-item> -->
                <el-form-item label="店铺经度">
                    <el-row>
                        <el-col :span="4">
                            <el-input v-model="form.lng" placeholder="经度"></el-input>
                        </el-col>
                        <el-col :span="4">
                            <el-input v-model="form.lat" placeholder="纬度" style="margin-left: 10px;"></el-input>
                        </el-col>
                        <el-col :span="4">
                            <el-button type="primary" plain icon="el-icon-place" style="margin-left: 20px;"
                                @click="showLocation = true">选择坐标</el-button>
                        </el-col>
                    </el-row>
                </el-form-item>
                <el-form-item label="店铺详细地址">
                    <el-input type="textarea" v-model="form.address" placeholder="请输入门店详细地址"></el-input>
                </el-form-item>
                <el-form-item label="店铺简介">
                    <el-input type="textarea" v-model="form.detail" placeholder="请输入店铺简介"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="form.status">
                        <el-radio :label="1">开启</el-radio>
                        <el-radio :label="0">关闭</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
        </div>
        <el-dialog title="选择地址" :visible.sync="showLocation" :modal="false" :modal-append-to-body="false">
            <div class="map_box">
                <div class="map">
                    <el-amap :center="amapOptions.center">
                        <el-amap-marker :position="amapOptions.center"></el-amap-marker>
                    </el-amap>
                </div>
                <div class="search_box">
                    <el-amap-search-box :search-option="searchOption"
                        :on-search-result="onSearchResult"></el-amap-search-box>
                </div>

                <div class="search_wrap">
                    <div class="item" v-for="item in locationSearchList" :key="item.id">
                        <div class="left">
                            <div class="name">{{ item.name }}-{{ item.address }}</div>
                            <div class="location">
                                经纬度：{{ item.lng }},{{ item.lat }}
                            </div>
                        </div>
                        <div class="btn">
                            <el-button type="primary" @click="selectLocationHandle(item)">
                                选择
                            </el-button>
                        </div>
                    </div>
                </div>
            </div>
        </el-dialog>
        <el-dialog :visible.sync="showUpload" :close-on-click-modal="false" append-to-body width="500px"
            @close="showUpload = false">
            <el-upload :before-remove="handleBeforeRemove" :on-success="handleSuccess" :on-error="handleError"
                :file-list="fileList" :headers="headers" :action="qiNiuUploadApi" class="upload-demo" multiple>
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" style="display: block;" class="el-upload__tip">请勿上传违法文件，且文件不超过15M</div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="doSubmit">确认</el-button>
                <el-button @click="uploadClose">取消</el-button>
            </div>
        </el-dialog>
        <div slot="footer" class="dialog-footer">
            <el-button @click="close">取消</el-button>
            <el-button type="primary" @click="submitHandle" :loading="formLoading">
                <span v-if="!formLoading">保存</span>
                <span v-else>保存中...</span>
            </el-button>
        </div>
    </el-dialog>
</template>

<script>
import { getToken } from '@/utils/auth'
import { mapGetters } from 'vuex'
import crudQiNiu from '@/api/tools/qiniu'
import { tbShopInfoPost } from '@/api/shop'
export default {
    computed: {
        ...mapGetters([
            'qiNiuUploadApi'
        ])
    },
    data() {
        const validateLogo = (rule, value, callback) => {
            if (!this.form.logo) {
                callback(new Error('请上传门店logo'))
            } else {
                callback()
            }
        }
        return {
            dialogVisible: false,
            showLocation: false,
            showUpload: false,
            uploadIndex: 1,
            startTime: '',
            endTime: '',
            formLoading: false,
            form: {
                id: '',
                shopName: '',
                registerType: 'restaurant',
                profiles: 'release',
                registerCode: '',
                account: '',
                password: '',
                phone: '',
                supportDeviceNumber: '',
                lat: '',
                lng: '',
                address: '',
                detail: '',
                status: 1,
                logo: '',
                coverImg: ''
            },
            resetForm: '',
            rules: {
                shopName: [
                    {
                        required: true,
                        message: ' ',
                        trigger: 'blur'
                    }
                ],
                logo: [
                    {
                        required: true,
                        validator: validateLogo,
                        trigger: 'change'
                    }
                ],
                account: [
                    {
                        required: true,
                        message: ' ',
                        trigger: 'change'
                    }
                ],
                password: [
                    {
                        required: true,
                        message: ' ',
                        trigger: 'change'
                    }
                ]
            },
            fileList: [],
            files: [],
            headers: {
                'Authorization': getToken()
            },
            searchOption: {
                city: '西安',
                citylimit: false
            },
            locationSearchList: [],
            amapOptions: {
                center: [108.946465, 34.347984],
                position: []
            }
        }
    },
    mounted() {
        this.resetForm = { ...this.form }
    },
    methods: {
        onSearchResult(res) {
            this.locationSearchList = res
            this.amapOptions.center = [res[0].lng, res[0].lat]
        },
        // 确认地址选择
        selectLocationHandle(item) {
            this.form.lng = item.lng
            this.form.lat = item.lat
            this.showLocation = false
        },
        // 保存
        submitHandle() {
            this.$refs.form.validate(async (valid) => {
                if (valid) {
                    this.formLoading = true
                    try {
                        await tbShopInfoPost(this.form, this.form.id ? 'put' : 'post')
                        this.$emit('success')
                        this.formLoading = false
                        this.$notify({
                            title: '成功',
                            message: `${this.form.id ? '编辑' : '添加'}成功`,
                            type: 'success'
                        });
                        this.close()
                    } catch (error) {
                        this.formLoading = false
                        console.log(error)
                    }
                }
            })
        },
        handleSuccess(response, file, fileList) {
            // const uid = file.uid
            // const id = response.id
            // this.files.push({ uid, id })
            console.log('上传成功', response)
            this.files = response.data
        },
        handleBeforeRemove(file, fileList) {
            for (let i = 0; i < this.files.length; i++) {
                if (this.files[i].uid === file.uid) {
                    crudQiNiu.del([this.files[i].id]).then(res => { })
                    return true
                }
            }
        },
        handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url
            this.dialogVisible = true
        },
        // 监听上传失败
        handleError(e, file, fileList) {
            const msg = JSON.parse(e.message)
            this.crud.notify(msg.message, CRUD.NOTIFICATION_TYPE.ERROR)
        },
        // 刷新列表数据
        doSubmit() {
            this.fileList = []
            this.showUpload = false
            switch (this.uploadIndex) {
                case 1:
                    this.form.logo = this.files[0]
                    break;
                case 2:
                    this.form.coverImg = this.files[0]
                    break;
                default:
                    break;
            }
        },
        show(obj) {
            this.dialogVisible = true
            if (obj && obj.id) {
                this.form = { ...obj }
            }
        },
        close() {
            this.dialogVisible = false
        },
        uploadClose() {
            this.showUpload = false
        },
        reset() {
            this.form = { ...this.resetForm }
        }
    }
}
</script>

<style scoped lang="scss">
.map_box {
    width: 100%;
    position: relative;

    .map {
        height: 300px;
    }

    .search_box {
        position: absolute;
        top: 10px;
        left: 10px;
    }

    .search_wrap {
        padding: 6px 0;

        .item {
            display: flex;
            padding: 12px 0;

            .left {
                flex: 1;
                display: flex;
                flex-direction: column;
                padding-right: 20px;

                .location {
                    color: #999;
                    padding-top: 4px;
                }
            }
        }
    }
}
</style>