<template>
    <div class="edit-view">
        <el-form
            :model="formData"
            :rules="rules"
            ref="form"
            label-width="110px"
            label-position="right"
            size="small"
            style="max-width: 800px;"
        >
            <el-form-item label="分享海报" prop="poster">
                <!-- <poster-upload
                    v-model="formData.poster"
                    :width="80"
                    :height="160"
                    :ratio="[1, 2]"
                    :imgWidth="224"
                    key="22"
                    name="poster"
                >
                    <div slot="tips" class="tips">尺寸120x240px</div>
                </poster-upload> -->
                <single-upload v-model="formData.poster"></single-upload>
            </el-form-item>
            <el-form-item label="分享图" prop="smallPoster">
                <multi-upload v-model="formData.smallPoster"></multi-upload>
            </el-form-item>
            <el-form-item prop="share" label="文案" style="width: 600px;">
                <simple-rich-text v-model="formData.share" class="select-width"></simple-rich-text>
            </el-form-item>
            <el-form-item>
                <el-button @click="onSave" :loading="saving" type="primary" v-if="$route.query.id">保存</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: 'PackageEdit',
    created() {
        if (this.$route.query.id) {
            this.packageId = this.$route.query.id;
            this.$http
                .get('package/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
            // this.$http.get('/stock/byPackageId', { packageId: this.$route.query.id }).then(res => {
            //     this.stockList = res;
            // });
        }
        // this.$http
        //     .post('/attractions/all', { size: 1000, query: { del: false, brand: false } }, { body: 'json' })
        //     .then(res => {
        //         if (res.content.length > 0) {
        //             res.content.forEach(item => {
        //                 this.attractionsOptions.push({
        //                     label: item.name,
        //                     value: item.id
        //                 });
        //             });
        //         }
        //     })
        //     .catch(e => {
        //         console.log(e);
        //         this.$message.error(e.error);
        //     });
        // this.$http
        //     .post('/category/getChildren', { id: 731 })
        //     .then(res => {
        //         if (res.length > 0) {
        //             res.forEach(item => {
        //                 this.categoryOptions.push({
        //                     label: item.name,
        //                     value: item.id
        //                 });
        //             });
        //         }
        //     })
        //     .catch(e => {
        //         console.log(e);
        //         this.$message.error(e.error);
        //     });
        // this.$http
        //     .post('/coupon/all', { size: 1000, query: { del: false } }, { body: 'json' })
        //     .then(res => {
        //         this.coupons = res.content;
        //     })
        //     .catch(e => {
        //         console.log(e);
        //         this.$message.error(e.error);
        //     });
    },
    data() {
        return {
            saving: false,
            formData: {
                img: [],
                tag: [],
                repeatedly: false,
                couponId: []
            },
            rules: {
                attractionsId: [
                    {
                        required: true,
                        message: '请选择景区',
                        trigger: 'blur'
                    }
                ],
                name: [
                    {
                        required: true,
                        message: '请输入套餐名称',
                        trigger: 'blur'
                    }
                ],
                // amount: [
                //     {
                //         required: true,
                //         message: '请输入金额',
                //         trigger: 'blur'
                //     }
                // ],
                categoryId: [
                    {
                        required: true,
                        message: '请选择类型',
                        trigger: 'blur'
                    }
                ]
            },
            typeOptions: [
                { label: '团队', value: 'TEAM' },
                { label: '个人', value: 'PERSONAL' }
            ],
            inputVisible: false,
            inputValue: '',
            attractionsOptions: [],
            categoryOptions: [],
            stockList: [],
            packageId: 0,
            coupons: [],
            list: [],
            subrule: {}
        };
    },
    computed: {
        saveOtherJson() {
            const stockList = [...this.stockList]
                .filter(item => {
                    return !!item.inventory > 0 || item.price > 0 || !!item.specification;
                })
                .map(item => {
                    return {
                        ...item,
                        packageId: this.packageId
                    };
                });
            return JSON.stringify(stockList);
        }
    },
    methods: {
        onSave() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
            // Promise.all(this.$refs.subform.map(i => i.validate())).then(() => {
            //     this.$refs.form.validate(valid => {
            //         if (valid) {
            //             this.submit();
            //         } else {
            //             return false;
            //         }
            //     });
            // });
        },
        submit() {
            let data = { ...this.formData };
            this.saving = true;
            this.$http
                .post('/package/save', data, { body: 'json' })
                .then(res => {
                    this.saving = false;
                    // this.$message.success('成功');
                    this.packageId = res.id;
                    this.$nextTick(() => {
                        // this.$http.post('/stock/batchSave', {
                        //     stocks: this.saveOtherJson
                        // });
                        this.$message.success('成功');
                        this.$router.go(-1);
                    });
                    this.$emit('next', 'second');
                    this.$router.replace({
                        name: 'PackageEdit',
                        query: {
                            id: res.id,
                            attractionsId: res.attractionsId
                        }
                    });
                })
                .catch(e => {
                    console.log(e);
                    this.saving = false;
                    this.$message.error(e.error);
                });
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/package/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);
                    }
                });
        },
        removeTag(i) {
            this.formData.tag.splice(i, 1);
        },
        showInput() {
            this.inputVisible = true;
            this.$nextTick(_ => {
                this.$refs.saveTagInput.$refs.input.focus();
            });
        },
        handleInputConfirm() {
            let inputValue = this.inputValue;
            if (inputValue) {
                this.formData.tag.push(inputValue);
            }
            this.inputVisible = false;
            this.inputValue = '';
        },
        addStockForm() {
            this.stockList.push({
                specification: '',
                day: '',
                price: 0,
                inventory: 0
            });
        },
        deleteStockForm(info, index) {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'warning' })
                .then(() => {
                    const stockList = [...this.stockList];
                    if (info.id) {
                        info.del = true;
                        stockList[index] = info;
                    } else {
                        stockList.splice(index, 1);
                    }
                    this.stockList = stockList;
                    this.$message.success('删除成功');
                })
                .catch(e => {});
        },
        delItem(i) {
            this.list.splice(i, 1);
        }
    }
};
</script>
<style lang="less" scoped>
.el-tag + .el-tag {
    margin-left: 10px;
}
.button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
}
.input-new-tag {
    width: 100px;
    margin-left: 10px;
    vertical-align: bottom;
}
.input-title {
    width: 350px;
}
.select-width {
    width: 350px;
}
.spec {
    margin-top: 15px;
    background-color: #f7f7f7;
    padding: 20px 20px 15px 20px;
    span {
        font-size: 13px;
        color: #565b66;
        line-height: 16px;
        padding: 0 15px 0 15px;
    }
    .input {
        width: 180px;
        margin: 0 10px 5px 0;
    }
    .el-button {
        height: 34px;
        border-width: 0;
        margin-top: 23px;

        &.del {
            width: 34px;
            padding: 9px 5px;
            font-size: 16px;
        }
    }
}
.stock {
    display: flex;
    align-items: center;
    margin-top: 20px;
    .name {
        width: 200px;
    }
    .name2 {
        width: 180px;
    }

    .value {
        width: 180px;
    }

    span {
        font-size: 12px;
        color: #565b66;
        line-height: 16px;
        padding: 0 20px 0 20px;
    }

    .el-button {
        height: 36px;
        border-width: 0;

        &.del {
            width: 36px;
            padding: 9px 5px;
            font-size: 16px;
            margin-left: 10px;
        }
    }
}
.subform {
    padding: 16px 16px 1px 16px;
    background: #f2f3f5;
    border-radius: 8px;
    border: 1px solid #eee;
    margin-bottom: 20px;
}
</style>
