<template>
    <div>
        <div class='card'>
            <h3><strong>礼物匹配</strong></h3>
            <p>希望对方的性别是</p>
            <van-radio-group v-model="radio">
                <van-radio name="0" checked-color="#fd6740" style="float: left; margin-right: 20px">男</van-radio>
                <van-radio name="1" checked-color="#fd6740">女</van-radio>
            </van-radio-group>
            <p>礼物名称</p>
            <van-field
                    placeholder="给你的礼物起个名字吧"
                    v-model="presentName"
                    :error="pnError"
                    @click="pnError=false"
                    required
                    maxlength="10"
            />
            <p>我的礼物丨走心介绍</p>
            <van-cell-group>
                <van-field
                        type="textarea"
                        placeholder="请在此输入你的礼物介绍和礼物故事"
                        rows="5"
                        autosize
                        v-model="desc"
                        :error="descError"
                        @click="descError=false"
                        required
                        maxlength="300"
                />
            </van-cell-group>

            <p>礼物标签</p>
            <div style="margin-bottom: 20px">
                <van-checkbox-group v-model="result">
                    <van-checkbox
                            v-for="(item, index) in tags"
                            :key="index"
                            :name="index"
                            :style="(index+1) % 4 === 0 ? br : nobr"
                    >{{ item }}</van-checkbox>
                </van-checkbox-group>
            </div>

        </div>
        <div class="card">
            <p>上传礼物照片</p>
            <add-photo @uploadPhoto="onReadPhoto"/>
        </div>
        <van-button size="large" type="primary" @click="publishPresent">下一步</van-button>

    </div>
</template>

<script>
    import {
        Uploader, Icon,
        CellGroup, Field,
        RadioGroup, Radio,
        CheckboxGroup, Checkbox,
        Picker,
        Button,
        Toast
    } from 'vant'
    import AddPhoto from "../components/addPhoto";
    export default {
        name: "PresentRegister",
        components: {
            AddPhoto,
            [Uploader.name]: Uploader, [Icon.name]: Icon,
            [CellGroup.name]: CellGroup, [Field.name]: Field,
            [RadioGroup.name]: RadioGroup, [Radio.name]: Radio,
            [CheckboxGroup.name]: CheckboxGroup, [Checkbox.name]: Checkbox,
            [Picker.name]: Picker,
            [Button.name]: Button
        },
        data() {
            return {
                radio: '0',
                presentName: '',
                pnError: false,
                desc: '',
                descError: false,
                tags: [
                    "情怀","复古", "有趣", "学霸",  "实用",
                    "同年", "吃货", "佛系",  "土味", "活力",
                    "洋气", "精致",  "轻奢",  "其它",
                ],
                result: [],
                photo: null,
                nobr: "float: left; margin-right: 5px",
                br: ""
            }
        },
        methods: {
            publishPresent() {
                // 检验输入
                if (this.presentName.length === 0) {
                    this.pnError = true;
                    return;
                } else if (this.desc.length <= 15) {
                    this.descError = true;
                    Toast.fail('请输入至少15个字的礼物介绍');
                    return;
                } else if (this.photo === null) {
                    Toast.fail('请上传礼物照片');
                    return;
                }
                // post_present()
                let present = {
                    name: this.presentName,
                    gender: parseInt(this.radio),
                    desc: this.desc,
                    tags: this.result,
                    photo: this.photo
                };
                console.log(present);
                this.$router.push('index');
            },
            onReadPhoto(file) {
                this.photo = file;
            }
        },
        created: function () {
            if(this.$route.query.present) {
                let present = this.$route.query.present;
                console.log(present);
                this.radio = present.gender.toString();
                this.presentName = present.presentName;
                this.desc = present.presentDesc;
            }
        }
    }
</script>

<style scoped>
    p, h3 {
        color: #fd6740;
    }
    h3 {
        padding: 5px;
        border-bottom: solid 1px #e4e4e4;
    }
    .card {
        background-color: white;
        margin: 20px 20px;
        border: solid 1px #e4e4e4;
        padding: 15px;
        text-align: left;
    }
    .van-button {
        background-color: #fd6740;
        border-color: #fd6740;
        margin: 10px 0 10px;
        width: 85%;
    }
</style>