<template>
    <div class="edit-view">
        <el-form :model="formData" :rules="rules" ref="form" label-width="94px" label-position="right" size="small"
                 style="max-width: 500px;">
            <!--            <el-form-item prop="userId" label="用户ID">
                            <el-input-number type="number" v-model="formData.userId"></el-input-number>
                        </el-form-item>-->
            <el-form-item prop="logo" label="Logo">
                <crop-upload v-model="formData.logo"></crop-upload>
            </el-form-item>
            <el-form-item prop="name" label="名称">
                <el-input v-model="formData.name" readonly></el-input>
            </el-form-item>
            <el-form-item prop="showName" label="显示名称">
                <el-input v-model="formData.showName" readonly></el-input>
            </el-form-item>
            <el-form-item prop="introduction" label="简介">
                <el-input type="textarea" v-model="formData.introduction" :rows="2" readonly></el-input>
            </el-form-item>
<!--            <el-form-item prop="proclamation" label="公告">
                <el-input type="textarea" v-model="formData.proclamation" :rows="2"></el-input>
            </el-form-item>-->
<!--            <el-form-item prop="banner" label="banner">
                <single-upload v-model="formData.banner"></single-upload>
            </el-form-item>-->
<!--            <el-form-item prop="img" label="图片">
                <multi-upload v-model="formData.img"></multi-upload>
            </el-form-item>-->
<!--            <el-form-item prop="address" label="地址">
                <el-input v-model="formData.address"></el-input>
            </el-form-item>-->
            <el-form-item prop="phone" label="手机">
                <el-input v-model="formData.phone" readonly></el-input>
            </el-form-item>
<!--            <el-form-item prop="password" label="密码">
                <el-input v-model="formData.password" show-password></el-input>
            </el-form-item>-->
<!--            <el-form-item prop="qualification" label="营业资质">
                <multi-upload v-model="formData.qualification"></multi-upload>
            </el-form-item>-->
<!--            <el-form-item prop="businessNatureId" label="营业性质">-->
                <!--<el-input v-model="formData.businessNature"></el-input>-->
<!--                <el-select v-model="formData.businessNature"-->
<!--                           placeholder="请选择" value-key="value">-->
<!--                    <el-option value="自营">自营</el-option>-->
<!--                    <el-option value="连锁">连锁</el-option>-->
<!--                    <el-option value="加盟">加盟</el-option>-->
<!--                </el-select>-->
<!--            </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="MONDAY">MONDAY</el-option>
                    <el-option value="TUESDAY">TUESDAY</el-option>
                    <el-option value="WEDNESDAY">WEDNESDAY</el-option>
                    <el-option value="THURSDAY">THURSDAY</el-option>
                    <el-option value="FRIDAY">FRIDAY</el-option>
                    <el-option value="SATURDAY">SATURDAY</el-option>
                    <el-option value="SUNDAY">SUNDAY</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-time-picker
                        is-range
                        v-model="formData.startTime"
                        value-format="HH:mm:ss"
                        range-separator="至"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        placeholder="选择时间范围">
                </el-time-picker>
            </el-form-item>-->
            <!--            <el-form-item prop="blacklist" label="黑名单">
                            <el-switch v-model="formData.blacklist"></el-switch>
                        </el-form-item>-->
 <!--           <el-form-item prop="firstOrder" label="首单优惠">
                <el-input-number type="number" v-model="formData.firstOrder"></el-input-number>
            </el-form-item>
            <el-form-item prop="newUser" label="新用户优惠">
                <el-input-number type="number" v-model="formData.newUser"></el-input-number>
            </el-form-item>-->
<!--            <el-form-item prop="category" label="经营品别">
                <el-select v-model="formData.category" multiple
                           placeholder="请选择" value-key="name">
                    <el-option v-for="item in category" :key="item.name"
                               :label="item.name" :value="item">
                    </el-option>
                </el-select>
            </el-form-item>-->
            <!--  <el-form-item prop="tag" label="标签">
                     <el-tree
                       :props="props"
                       :load="loadNode"
                       lazy
                       show-checkbox
                       @check-change="handleCheckChange">
                      </el-tree>
            </el-form-item> -->
<!--            <el-form-item prop="aliName" label="支付宝名称">
                <el-input v-model="formData.aliName"></el-input>
            </el-form-item>
            <el-form-item prop="aliAccount" label="支付宝账户">
                <el-input v-model="formData.aliAccount"></el-input>
            </el-form-item>-->
            <el-form-item prop="promo" label="推广语">
                <el-input v-model="formData.promo"></el-input>
            </el-form-item>
            <el-form-item prop="priority" label="优先级">
                <el-input-number v-model="formData.priority" :max="5" :min="1"
                ></el-input-number>
            </el-form-item>

            <el-form-item prop="buyAlliance" label="口碑联盟">
                <el-switch v-model="formData.rated"></el-switch>
            </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: 'MerchantEdit',
        created() {
            if (this.$route.query.id) {
                this.$http
                    .get('merchant/getDTO/' + this.$route.query.id)
                    .then(res => {
                        this.formData = res;
                    })
                    .catch(e => {
                        console.log(e);
                        this.$message.error(e.error);
                    });
            }
/*            this.$http
                .get('/category/tree', {
                    id: 1
                })
                .then(res => {
                    this.category = res;
                })
                .catch(e => {
                    console.log(e);
                });*/
        },
        data() {
            return {
                saving: false,
                formData: {
                    img: [],
                    qualification: []
                },
                rules: {},
                category: [],
            }
        },
        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.qualification = data.qualification.join(",");
                /*data.week = data.week.join(",")*/

                this.saving = true;
                this.$http
                    .post('/merchant/saveDTO', 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(`/merchant/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);
                    }
                })
            },*/
/*            handleChange(name) {
                console.log(name);
            }*/
        }
    }
</script>
<style lang="less" scoped>
</style>
