<template>
    <view class="afterSales">
        <view class="service-content">
            <view class="service"> 本次售后服务将由<span>DT生态平台</span>为您提供服务 </view>
        </view>
        <!-- 商品信息 -->
        <view class="goods-item">
            <image :src="goodsInfo.cover" mode="aspectFill" class="good-img" />
            <view class="item--content">
                <view class="title">{{goodsInfo.goods_name}}</view>
                <view class="sub_title" v-if="goodsInfo.shop">{{goodsInfo.shop.name}}</view>
                <view class="price">{{goodsInfo.price || '0'}}
                    <view class="price-type">
                        <text> DT积分</text>
                    </view>
                </view>
            </view>
        </view>
        <!-- 申请数量 -->
        <view class="apply-number">
            <view class="apply-number-title">
                申请数量<text>（最多可申请{{goodsInfo.qty}}个）</text>
            </view>
            <uni-section type="line" padding>
                <uni-number-box :value="goodsInfo.qty" :disabled="true" />
            </uni-section>
        </view>
        <!--  退货方式 -->
        <view class="block info-box">
            <view class="info-item">
                <view class="label">申请原因</view>
                <view class="nowrap info-item-title" @click="changeReason">{{ttext}}
                    <uni-icons class="icons" type="more-filled" color="grey" />
                </view>
            </view>
            <view class="info-item">
                <view class="label">商品状态</view>
                <view class="nowrap">
                    <label class="radio" @click="radioValue = 'r1'">
                        <radio value="r1" color="#34CE98" :checked="radioValue === 'r1'" style="transform:scale(0.7)" />
                        已破损
                    </label>
                    <label class="radio " @click="radioValue = 'r2'" style="padding-left: 20rpx;">
                        <radio value="r2" color="#34CE98" :checked="radioValue === 'r2'" style="transform:scale(0.7)" />
                        未破损
                    </label>
                </view>
            </view>
            <view class="info-item">
                <view class="label">备注</view>
                <textarea class="info-textarea" v-model="remark" placeholder="请描述申请售后服务的具体原因"></textarea>
            </view>
        </view>

        <view class="block info-box">
            <view class="info-item">
                <view class="label">联系电话</view>
                <view class="nowrap info-item-title">{{tel}}</view>
            </view>
        </view>

        <view class="applyBtn" @click="refund(id,'post')">申请{{this.type === 'refund'?'退货':'换货'}}</view>


    </view>
</template>

<script>
    import eventBus from '../../utils/eventBus.js';
    import {
        refund
    } from '@/apis/interfaces/order.js'
    export default {
        data() {
            return {
                remark: '',
                type: '',
                id: '',
                title: [],
                ttext: '请选择申请原因',
                tel: '',
                goodsInfo: {},
                radioValue: ''
            }
        },
        onLoad() {
            let type = this.$Route.query.type;
            let title = '';
            if (type === 'refund') {
                title = '退货申请'
            } else {
                title = '换货申请'
            }
            uni.setNavigationBarTitle({
                title: title
            })
            this.type = this.$Route.query.type;
            this.id = this.$Route.query.id;
            this.refund(this.$Route.query.id, 'get');
        },
        methods: {
            refund(id, method) {
                let params = {};
                if (method === 'post') {
                    params = {
                        title: this.ttext,
                        remark: this.remark,
                        pictures: [],
                        type: this.type === 'refund' ? 2 : 3,
                    };
                    if (this.ttext === '请选择申请原因') {
                        uni.showToast({
                            title: '请选择申请原因',
                            icon: 'none',
                            mask: true,
                        })
                        return;
                    }
                }
                refund(this.$Route.query.id, method, params).then(res => {
                    if (method === 'get') {
                        this.title = res.title;
                        this.money = res.order.amount;
                        this.tel = res.user.username;
                        this.goodsInfo = res.order.items[0].sku;
                        this.goodsInfo.shop = res.order.shop;
                        this.goodsInfo.qty = res.order.items[0].qty;
                    } else {
                        uni.showToast({
                            title: '申请退货成功, 请去退换货中查看订单',
                            icon: 'none',
                            mask: true,
                            duration: 3000,
                        })
                        setTimeout(() => {
                            eventBus.$emit('applyRefundMoney', this.$Route.query.id);
                            uni.navigateBack({
                                delta: 2
                            })
                        }, 3000)
                    }

                }).catch(err => {
                    uni.showToast({
                        title: err.message,
                        icon: 'none',
                        mask: true,
                    })
                })
            },
            changeReason() {
                uni.showActionSheet({
                    title: '选择申请原因',
                    itemList: this.title,
                    success: (res) => {
                        this.ttext = this.title[res.tapIndex]
                    },
                    fail: (res) => {
                        console.log(res.errMsg);
                    }
                });
            },
        }
    }
</script>

<style lang="scss">
    .applyBtn {
        width: 70%;
        background-color: $main-color;
        border-radius: 50rpx;
        text-align: center;
        padding: 20rpx $padding;
        color: #fff;
        position: relative;
        left: 10%;
        font-size: 32rpx;
        margin-top: $margin * 2;
        font-weight: bold;
    }

    .afterSales {
        background-color: #f9f9f9;
        min-height: 100vh;

        .service-content {
            background-color: #Fff;
            display: flex;
            flex-direction: row;
            align-items: center;
            justify-content: center;
            box-sizing: border-box;

            .service {
                font-size: 24rpx;
                margin: $margin;
                padding: 4rpx 20rpx;
                border-radius: 30rpx;
                background-color: #f9f9f9;
                display: inline-;
                text-align: center;
                color: #666;

                span {
                    color: $text-price;
                }
            }
        }

        .goods-item {
            background-color: #Fff;
            width: 100%;
            display: inline-block;
            // border-bottom: solid 1rpx #eee;

            // padding: $padding;
            padding-left: $padding;
            padding-top: $padding;
            padding-bottom: $padding - 15;
            display: flex;
            flex-direction: row;
            align-items: center;
            justify-content: flex-start;
            box-sizing: border-box;

            .good-img {
                width: 160rpx;
                height: 160rpx;
                border-radius: 10rpx;
            }

            .item--content {
                flex: 1;
                padding: $padding - 10;

                &>.title {
                    font-size: 30rpx;
                    color: #333;
                    font-weight: bold;
                    // @extend .ellipsis-1;
                }

                &>.sub_title {
                    color: #a05f0c;
                    font-size: 26rpx;
                    padding-top: 6rpx;
                    // @extend .ellipsis-1;
                }

                &>.price {
                    padding-top: 10rpx;
                    display: flex;
                    flex-direction: row;
                    align-items: flex-end;
                    justify-content: flex-end;
                    box-sizing: border-box;
                    font-weight: bold;
                    font-size: 36rpx;
                    color: $text-price;

                    // @extend .ellipsis-1;
                    .price-type {
                        display: flex;
                        flex-direction: row;
                        align-items: center;
                        justify-content: space-between;
                        box-sizing: border-box;
                        flex: 1;

                        text {
                            margin-right: $margin/2;
                            padding-left: 10rpx;
                            font-size: 60%;
                            padding-bottom: 4rpx;
                        }

                        .kucun {
                            color: $text-gray;
                            font-weight: normal;
                        }
                    }

                }
            }

        }
    }

    .apply-number {
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        box-sizing: border-box;
        background-color: #fff;
        padding: $padding;
        border-radius: 0 0 30rpx 30rpx;

        .apply-number-title {
            font-size: 30rpx;
            font-weight: bold;

            text {
                font-size: 26rpx;
                color: grey;
                font-weight: normal;
            }
        }
    }

    .block {
        background: white;
        margin: $margin;
        border-radius: $radius;
    }

    // 订单信息
    .info-box {
        .info-item {
            position: relative;
            padding: 40rpx $padding 40rpx 200rpx;
            font-size: $title-size-m;
            min-height: 40rpx;
            text-align: right;

            .radio {
                font-size: 28rpx;
                font-weight: bold;
            }

            .label {
                position: absolute;
                left: 40rpx;
                top: 40rpx;
                color: $text-gray;
            }

            .info-textarea {
                height: 120rpx;
                width: 100%;
                text-align: left;
                font-size: $title-size-m;
            }

            &::after {
                position: absolute;
                left: $margin;
                right: $margin;
                content: " ";
                height: 1rpx;
                bottom: 0;
                background: $border-color;
            }

            &:last-child::after {
                display: none;
            }

            .info-item-title {
                display: flex;
                flex-direction: row;
                align-items: center;
                justify-content: flex-end;
                box-sizing: border-box;
                font-size: 28rpx;
                font-weight: bold;

                .icons {
                    padding-top: 2rpx;
                    font-weight: normal;
                    margin-left: 10rpx;
                }
            }
        }
    }



    // 订单信息
    .info-box {
        .info-item {
            position: relative;
            padding: 40rpx $padding 40rpx 200rpx;
            font-size: $title-size-m;
            min-height: 40rpx;
            text-align: right;

            .radio {
                font-size: 28rpx;
                font-weight: bold;
            }

            .label {
                position: absolute;
                left: 40rpx;
                top: 40rpx;
                color: $text-gray;
            }

            .info-textarea {
                height: 120rpx;
                width: 100%;
                text-align: left;
                font-size: $title-size-m;
            }

            .info-textarea-m {
                height: 120rpx;
                width: 100%;
                text-align: left;
                font-size: 60rpx;
                font-weight: bold;
            }

            &::after {
                position: absolute;
                left: $margin;
                right: $margin;
                content: " ";
                height: 1rpx;
                bottom: 0;
                background: $border-color;
            }

            &:last-child::after {
                display: none;
            }

            .info-item-title {
                display: flex;
                flex-direction: row;
                align-items: center;
                justify-content: flex-end;
                box-sizing: border-box;
                font-size: 28rpx;
                font-weight: bold;

                .icons {
                    padding-top: 2rpx;
                    font-weight: normal;
                    margin-left: 10rpx;
                }
            }
        }
    }
</style>
