<config>
{
    "navigationBarTitleText": "申请报修",
}
</config>
<template>
    <div class="container">
        <div class="content">
            <van-cell-group :border="false" class="form">
                <van-field label="报修人" :value="name" @input="name = $event.detail" placeholder="请输入报修人">
                </van-field>
                <van-field
                    label="联系方式"
                    type="digit"
                    :value="phone"
                    @input="phone = $event.detail"
                    placeholder="请输入联系方式"
                >
                </van-field>
                <van-field label="故障图片" :border="false" readonly> </van-field>
                <van-uploader :file-list="images" max-count="5" :after-read="afterRead" @delete="deleteImg" />
            </van-cell-group>
            <div class="box-bottom">
                <van-field :border="false" label="故障描述" readonly> </van-field>
                <van-field
                    :value="description"
                    @input="description = $event.detail"
                    :border="false"
                    rows="1"
                    autosize
                    type="textarea"
                    placeholder="请描述房间内需要描述事项"
                />
                <div class="num">{{ description.length }}/200</div>
            </div>
        </div>
        <div class="btn-list">
            <van-button :disabled="!canSubmit" type="default" @click="submit">提交</van-button>
        </div>
    </div>
</template>
<script>
import { mapState } from 'vuex';
export default {
    name: 'repairReport',
    metaInfo: {
        title: '申请报修'
    },
    data() {
        return {
            phone: '',
            name: '',
            description: '',
            images: [],
            imgBox: true,
            imagesList: []
        };
    },
    computed: {
        ...mapState(['roomInfo']),
        canSubmit() {
            if (this.phone && this.name && this.description && this.images.length != 0) {
                return true;
            } else {
                return false;
            }
        }
    },
    // onShow() {
    //     this.name = '';
    //     this.phone = '';
    //     this.description = '';
    //     this.imagesList = [];
    // },
    methods: {
        afterRead(file) {
            this.showLoading();
            this.$http
                .uploadFile(file.path)
                .then(res => {
                    this.imgBox = false;
                    this.hideLoading();
                    this.images = [
                        ...this.images,
                        {
                            ...file,
                            url: res
                        }
                    ];
                    // console.log(this.images);
                })
                .catch(e => {
                    this.hideLoading();
                    wx.showToast({
                        icon: 'none',
                        title: e.error
                    });
                });
        },
        deleteImg(e) {
            let list = [...this.images];
            list.splice(e.detail.index, 1);
            this.images = list;
        },
        submit() {
            if (!this.description) {
                wx.showToast({
                    icon: 'none',
                    title: '请填写维修描述'
                });
                return;
            }
            this.imagesList = this.images.map(item => {
                return item.url;
            });
            this.$http
                .post('/repairInfo/create', {
                    targetId: this.roomInfo.bedId,
                    repairType: 'BED',
                    images: this.imagesList,
                    phone: this.phone,
                    description: this.description,
                    name: this.name
                })
                .then(res => {
                    wx.showToast({
                        title: '提交成功'
                    });
                    setTimeout(() => {
                        this.navigateTo('/pages/successfuls');
                    }, 1000);
                })
                .catch(e => {
                    this.hideLoading();
                    wx.showToast({
                        icon: 'none',
                        title: e.error
                    });
                });
        }
    }
};
</script>
<style lang="less" scoped>
.container {
    .content {
        width: calc(100vw - 8px);
        height: 150px;
        display: block;
        margin: 20px auto 0 auto;
        // background: #f2f4f5;
        border-radius: 5px;
        .num {
            font-size: 13px;
            color: #aaacad;
            padding-right: 15px;
            line-height: 18px;
            text-align: right;
        }
    }
    /deep/ .van-uploader {
        padding-left: 18px;
    }
    /deep/.form {
        position: relative;
        .van-cell {
            --cell-vertical-padding: 18px;
            --cell-horizontal-padding: 18px;
            --field-label-color: #000000;
            --cell-text-color: #000;
            .van-field__label,
            .van-cell__title {
                font-weight: bold;
                font-size: 14px;
            }
            .van-cell__value {
                // text-align: left;
            }
            &::after {
                left: 100px !important;
            }
        }
    }
    .box-bottom {
        /deep/ .van-field__label {
            margin: 10px 0 0 2px;
            font-weight: bold;
            color: #000;
            /deep/ .van-field__body {
                margin-left: 3px !important;
            }
        }
    }
    .btn-list {
        position: fixed;
        width: 100%;
        bottom: 0;
        background: #ffffff;
        padding-left: 43px;
        z-index: 999;
        .bottom();
        /deep/.van-button {
            width: 290px;
            height: 48px;
            background: @prim;
            border-radius: 12px;
            line-height: 48px;
            text-align: center;
            font-size: 16px;
            font-weight: normal;
            color: #ffffff;
        }
    }
}
</style>
