<template>
    <div class="edit-view">
        <el-form :model="formData" :rules="rules" ref="form" label-width="66px" label-position="right" size="small"
                 style="max-width: 500px;">
            <el-form-item prop="merchantId" label="商户ID">
                <el-input type="number" v-model="formData.merchantId"></el-input>
            </el-form-item>
            <el-form-item prop="name" label="名称">
                <el-input v-model="formData.name"></el-input>
            </el-form-item>
            <el-form-item prop="introduction" label="简介">
                <el-input v-model="formData.introduction"></el-input>
            </el-form-item>
            <el-form-item prop="img" label="图片">
                <multi-upload v-model="formData.img"></multi-upload>
            </el-form-item>
            <el-form-item prop="inventory" label="库存">
                <el-input-number type="number" v-model="formData.inventory"></el-input-number>
            </el-form-item>
            <!--                <el-form-item prop="totalSales" label="总销量">
                                        <el-input-number type="number" v-model="formData.totalSales"></el-input-number>
                            </el-form-item>-->
            <!--                <el-form-item prop="daySales" label="日销售">
                                        <el-input-number type="number" v-model="formData.daySales"></el-input-number>
                            </el-form-item>-->
            <!--                <el-form-item prop="likes" label="点赞">
                                        <el-input-number type="number" v-model="formData.likes"></el-input-number>
                            </el-form-item>-->
            <el-form-item prop="amount" label="价格">
                <el-input-number type="number" v-model="formData.amount"></el-input-number>
            </el-form-item>
            <el-form-item prop="discountAmount" label="折扣">
                <el-input-number type="number" v-model="formData.discountAmount"></el-input-number>
            </el-form-item>
            <el-form-item prop="signboard" label="招牌">
                <el-switch v-model="formData.signboard"></el-switch>
            </el-form-item>
            <!--<el-form-item prop="takeOff" label="下架">
                <el-switch v-model="formData.takeOff"></el-switch>
            </el-form-item>-->
<!--            <el-form-item prop="sort" label="排布">-->
<!--                <el-input-number type="number" v-model="formData.sort"></el-input-number>-->
<!--            </el-form-item>-->
            <el-form-item prop="isFullReduction" label="满减">
                <el-switch v-model="formData.isFullReduction"></el-switch>
            </el-form-item>
            <el-form-item prop="week" label="周几" id="week">
                <el-select v-model="formData.week" multiple
                           placeholder="请选择" value-key="value">
                    <!--<el-option v-for="item in weekOptions" :key="item.value"
                               :label="item.label" :value="item">
                    </el-option>-->
<!--                    <el-option value="每天">每天</el-option>-->
                    <el-option value="MON">MON</el-option>
                    <el-option value="TUE">TUE</el-option>
                    <el-option value="WED">WED</el-option>
                    <el-option value="THU">THU</el-option>
                    <el-option value="FRI">FRI</el-option>
                    <el-option value="SAT">SAT</el-option>
                    <el-option value="SUN">SUN</el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="startTime" label="时间">
                <el-time-picker
                        v-model="formData.startTime"
                        value-format="HH:mm:ss"
                        placeholder="选择时间"
                >
                </el-time-picker>
            </el-form-item>
            <el-form-item prop="endTime" label="时间">
                <el-time-picker
                        v-model="formData.endTime"
                        value-format="HH:mm:ss"
                        placeholder="选择时间"
                >
                </el-time-picker>
            </el-form-item>
            <el-form-item>
                <el-button @click="onSave" :loading="saving"
                           type="primary">保存
                </el-button>
                <el-button @click="onDelete" :loading="saving"
                           type="danger" v-if="formData.id">删除
                </el-button>
                <el-button @click="$router.go(-1)">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
    export default {
        name: 'GoodsEdit',
        created() {
            if (this.$route.query.id) {
                this.$http
                    .get('goods/get/' + this.$route.query.id)
                    .then(res => {
                        this.formData = res;
                    })
                    .catch(e => {
                        console.log(e);
                        this.$message.error(e.error);
                    });
            }
        },
        data() {
            return {
                saving: false,
                formData: {},
                rules: {},
            }
        },
        methods: {
            onSave() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        this.submit();
                    } else {
                        return false;
                    }
                });
            },
            submit() {
                let data = {...this.formData};
                data.img = data.img.join(',');
                data.week = data.week.join(',')

                this.saving = true;
                this.$http
                    .post('/goods/save', data, {body: 'json'})
                    .then(res => {
                        this.saving = false;
                        this.$message.success('成功');
                        this.$router.go(-1);
                    })
                    .catch(e => {
                        console.log(e);
                        this.saving = false;
                        this.$message.error(e.error);
                    });
            },
            onDelete() {
                this.$alert('删除将无法恢复，确认要删除么？', '警告', {type: 'error'}).then(() => {
                    return this.$http.post(`/goods/del/${this.formData.id}`)
                }).then(() => {
                    this.$message.success('删除成功');
                    this.$router.go(-1);
                }).catch(e => {
                    if (e !== 'cancel') {
                        console.log(e);
                        this.$message.error(e.error);
                    }
                })
            },
            onChange(){
              /*  let */
            }
        }
    }
</script>
<style lang="less" scoped>
</style>
