<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="merchant.showName" label="商家">
                <el-card class="rt-card" shadow="hover">{{formData.merchant.showName}}</el-card>
            </el-form-item>
            <el-form-item prop="name" label="名称">
                <el-card class="rt-card" shadow="hover">
                    {{formData.name}}
                </el-card>
                <!--<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-card class="rt-card" shadow="hover">
                    {{formData.introduction}}
                </el-card>
            </el-form-item>
            <el-form-item prop="img" label="图片">
                <!--<multi-upload v-model="formData.img"></multi-upload>-->
                <el-image style="width: 50px; height: 50px"
                          :src="picList(formData.img)[0]" fit="cover"
                          :preview-src-list="picList(formData.img)?picList(formData.img):[]">
                    <div slot="error">
                        <el-image style="width: 120px; height: 120px"
                                  src="https://idingdong.oss-cn-hangzhou.aliyuncs.com/image/2020-08-07-17-26-23hRwKPxhh.gif"
                                  fit="cover"/>
                    </div>
                </el-image>
            </el-form-item>
            <!--<el-form-item prop="inventory" label="库存">
                <el-input-number type="number" v-model="formData.inventory" disabled></el-input-number>
            </el-form-item>-->
            <el-form-item prop="totalSales" label="销量">
                <!-- <el-input-number type="number" v-model="formData.totalSales" disabled> </el-input-number>-->
                <el-card class="rt-card" shadow="hover">总销量 : {{formData.totalSales}} &nbsp;&nbsp; ( 库存 :
                    {{formData.inventory}} )
                </el-card>
            </el-form-item>
            <el-form-item prop="likes" label="点赞">
                <el-input-number type="number" v-model="formData.likes"
                                 disabled class="rt-input"></el-input-number>
            </el-form-item>
            <el-form-item prop="bad" label="差评">
                <el-input-number type="number" v-model="formData.bad"
                                 disabled class="rt-input"></el-input-number>
            </el-form-item>
            <el-form-item prop="amount" label="价格">
                <el-input-number type="number" v-model="formData.amount"
                                 disabled class="rt-input"></el-input-number>
            </el-form-item>
            <el-form-item prop="discountAmount" label="折扣">
                <el-input-number type="number" v-model="formData.discountAmount"
                                 disabled class="rt-input"></el-input-number>
            </el-form-item>
            <el-form-item prop="signboard" label="招牌">
                <el-switch v-model="formData.signboard" disabled></el-switch>
            </el-form-item>
            <el-form-item prop="takeOff" label="下架">
                <el-switch v-model="formData.takeOff" disabled></el-switch>
            </el-form-item>
            <el-form-item prop="isFullReduction" label="满减">
                <el-switch v-model="formData.isFullReduction" disabled></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 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-input type="textarea" readonly v-model="formData.week"
                ></el-input>
            </el-form-item>
            <el-form-item prop="startTime" label="时间" class="rt-input">
                <el-time-picker disabled
                                v-model="formData.startTime"
                                value-format="HH:mm:ss"
                                placeholder="选择时间"
                >
                </el-time-picker>
            </el-form-item>
            <el-form-item prop="endTime" label="时间" class="rt-input">
                <el-time-picker disabled
                                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: {
                    merchant: {},
                },
                rules: {},
            }
        },
        methods: {
            picList(row) {
                if (row) {
                    return row.split(",");
                } else {
                    return "";
                }
            },
            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);
                    }
                })
            },
        }
    }
</script>
<style lang="less" scoped>
    .rt-card /deep/ .el-card__body {
        padding: 4px 15px !important;
    }

    .rt-input /deep/ .el-input__inner {
        color: #303133 !important;
        background-color: white;
    }
</style>
