<template>
    <div class="edit-view">
        <div class="edit-view__content-wrapper">
            <div class="edit-view__content-section" v-loading="loading">
                <el-input v-model="param" placeholder="输入参数"></el-input>
                <el-button @click="clean('user', null, param)">user</el-button>
                <el-button @click="clean('user')">all user</el-button>
                <el-button @click="clean('userMy', null, param)">userMy</el-button>
                <el-button @click="clean('userMy')">all userMy</el-button>
                <el-button @click="clean('collection')">all collection</el-button>
                <el-button @click="clean('recommend')">all recommend</el-button>
                <el-divider></el-divider>
                <div>
                    <el-button @click="clearUserProperty">清除UserProperty</el-button>
                    <el-button @click="importUserProperty">导入UserProperty</el-button>
                    <el-button @click="clearVipPoint">清除VipPoint</el-button>
                    <el-button @click="importVipPoint">导入VipPoint</el-button>
                </div>
                <el-divider>库存/销量</el-divider>
                <el-input v-model="collectionId" placeholder="输入collectionId"></el-input>
                <el-input v-model="num" placeholder="输入num"></el-input>
                <el-button @click="operatingStockOrSale('INCREASE_STOCK')">加库存</el-button>
                <el-button @click="operatingStockOrSale('DECREASE_STOCK')">减库存</el-button>
                <el-button @click="operatingStockOrSale('INCREASE_SALE')">加销量</el-button>
                <el-button @click="operatingStockOrSale('DECREASE_SALE')">减销量</el-button>
            </div>
        </div>
    </div>
</template>
<script>
import SingleUpload from '../components/SingleUpload.vue';
export default {
    data() {
        return {
            loading: false,
            param: null,
            collectionId: '',
            num: ''
        };
    },
    methods: {
        clean(name, stringParam, longParam) {
            this.loading = true;
            this.$http
                .post('/cache/clear', { name, stringParam, longParam })
                .then(res => {
                    this.loading = false;
                    this.$message.success('成功');
                })
                .catch(e => {
                    this.loading = false;
                    this.$message.error(e.error || '失败');
                });
        },
        operatingStockOrSale(operationType) {
            let collectionId = this.collectionId.replace(/\s*/g,'');
            let num = this.num.replace(/\s*/g,'');
            var reg = /^[0-9]*$/
            if (collectionId == null || collectionId === undefined || collectionId === '') {
                this.$message.error('请输入collectionId')
                return
            }
            if (num == null || num === undefined || num === '') {
                this.$message.error('请输入num')
                return
            }
            if(!reg.test(this.collectionId)){
                this.$message.error('请正确输入collectionId,只允许输入数字')
                return
            }
            if(!reg.test(this.num)){
                this.$message.error('请正确输入num,只允许输入数字且必须大于0')
                return
            }
            if (this.num <= 0) {
                this.$message.error('num必须大于0')
                return
            }
            this.loading = true
            this.$http
                .post('/cache/operating/' + this.collectionId + '/' + this.num + '/' + operationType)
                .then(res => {
                    this.loading = false;
                    this.$message.success('成功');
                    this.num = null
                    this.collectionId = null
                })
                .catch(e => {
                    this.loading = false;
                    this.$message.error(e.error || '失败');
                })
        },
        clearUserProperty() {
            this.loading = true;
            this.$http
                .get('/eventMgmt/clearUserProperty')
                .then(res => {
                    this.loading = false;
                    this.$message.success('成功');
                })
                .catch(e => {
                    this.loading = false;
                    this.$message.error(e.error || '失败');
                });
        },
        clearVipPoint() {
            this.loading = true;
            this.$http
                .get('/eventMgmt/clearVipPoint')
                .then(res => {
                    this.loading = false;
                    this.$message.success('成功');
                })
                .catch(e => {
                    this.loading = false;
                    this.$message.error(e.error || '失败');
                });
        },
        importUserProperty() {
            const i = document.createElement('input');
            i.type = 'file';
            i.accept =
                'application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel';
            i.onchange = () => {
                console.log(i.files[0]);
                const form = new FormData();
                form.append('file', i.files[0]);
                this.loading = true;
                this.$http
                    .post('/eventMgmt/importUserProperty', form)
                    .then(res => {
                        this.loading = false;
                        this.$message.success('成功');
                    })
                    .catch(e => {
                        this.loading = false;
                        this.$message.error(e.error || '失败');
                    });
            };
            i.click();
        },
        importVipPoint() {
            const i = document.createElement('input');
            i.type = 'file';
            i.accept =
                'application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel';
            i.onchange = () => {
                const form = new FormData();
                form.append('file', i.files[0]);
                this.loading = true;
                this.$http
                    .post('/eventMgmt/importVipPoint', form)
                    .then(res => {
                        this.loading = false;
                        this.$message.success('成功');
                    })
                    .catch(e => {
                        this.loading = false;
                        this.$message.error(e.error || '失败');
                    });
            };
            i.click();
        }
    },
    components: { SingleUpload }
};
</script>
<style lang="less" scoped>
.el-input {
    width: 300px;
    margin-right: 20px;
    margin-bottom: 20px;
}
.el-button {
    margin-bottom: 20px;
}
</style>
