<template>
    <view
        class="pack-item c-gap-inner-top-large"
        :style="itemStyle"
        @tap="onItemTap(cPackageInfo)"
    >
        <view class="pack-item-cont">
            <view
                class="pack-item-img"
                :style="{
                    'background-image': 'url(' + cPackageInfo.img + ')'
                } | imgCompress('background-image', {column: 3})"
            />
            <!-- 体检角标列表 -->
            <view
                v-if="packageInfo.actList"
                class="pack-item-name__jb"
                :style="{
                    'background-image': 'url(' + packageInfo.actList + ')'
                } | imgCompress('background-image')"
            />
            <view class="pack-item-right">
                <view class="pack-desc">
                    <view class="pack-item-name c-font-medium c-line-clamp1">{{ cPackageInfo.name }}</view>
                    <view class="pack-address c-color c-font-normal">
                        <view class="pack-address-name c-gap-inner-right-small c-line-clamp1">{{
                            cPackageInfo.address
                        }}</view>
                        <view>{{ cPackageInfo.distance }}</view>
                    </view>
                    <view v-if="cPackageInfo.newTags && cPackageInfo.newTags.length" class="pack-tag-wrapper">
                        <c-label
                            v-for="(item, index) in cPackageInfo.newTags"
                            :key="index"
                            :font-size="12"
                            :height="14"
                            :line-height="14"
                            :text="item.text"
                            :color="item.color"
                            class="new-gap-right-small c-gap-bottom-small"
                        />
                    </view>
                    <view v-if="cPackageInfo.sales" class="pack-item-sales c-font-normal">{{
                        cPackageInfo.sales
                    }}</view>
                </view>
                <view class="pack c-font-normal">
                    <!-- 价格 -->
                    <view class="pack-price" :class="button && 'pack-price__fix'">
                        <!-- 券后价样式 -->
                        <view v-if="cPackageInfo.couponPrice && couponPriceStyle === 'sigma'" class="act-price">
                            <view class="c-font-normal c-color-gray">
                                <view class="price c-gap-inner-left-small">¥{{ cPackageInfo.price }}</view>
                            </view>
                            <view class="act-price-img">
                                <view class="c-inline pack-price-lowest price">
                                    <span class="c-font-normal pack-price-symbol-color">￥</span>
                                    <span v-if="price.int" class="pack-price-now">
                                        <text>{{ price.int }}</text>
                                        <text
                                            v-if="price.dec"
                                            class="c-font-normal pack-price-after"
                                        >.{{ price.dec }}</text>
                                    </span>
                                </view>
                            </view>
                            <view class="c-font-normal act-price-row">
                                <view class="c-inline c-color-gray">全网低价</view>
                                <view class="c-inline pack-price-symbol-color act-price-text">补贴价</view>
                            </view>
                        </view>
                        <view v-else class="pack-price-wrapper">
                            <view v-if="cPackageInfo.price" class="c-inline pack-price-now c-font-wb">
                                <text class="c-font-small pack-price-color">¥</text>
                                <text v-if="price.int">
                                    <text>{{ price.int }}</text>
                                    <text
                                        v-if="price.dec"
                                        class="c-font-normal pack-price-after"
                                    >.{{ price.dec }}</text>
                                </text>
                                <text
                                    v-if="couponPriceStyle === 'normal' && cPackageInfo.couponPrice"
                                    class="pack-price-normal c-font-13 c-gap-left-middle"
                                >券后价</text>
                                <text
                                    v-else-if="cPackageInfo.flashPrice && source !== 'flash'"
                                    class="pack-price-normal c-font-13 c-gap-left-middle"
                                >限时价</text>
                            </view>
                            <!-- 暂时去掉，待团检需求统一改造
                            <template v-if="couponPriceStyle === 'normal' && cPackageInfo.couponPrice">
                                <view
                                    v-if="cPackageInfo.price"
                                    class="c-inline c-font-13 c-gap-left-small pack-price-store"
                                >¥{{ cPackageInfo.price }}</view>
                            </template> -->
                        </view>
                        <!-- 限时购按钮 -->
                        <login-btn
                            v-if="button"
                            class="pack-price-btn pack-price-btn__big c-font-normal"
                            :style="[
                                {
                                    border: '1px solid ' + button.color,
                                    background: button.solid ? button.color : '#fff',
                                    color: button.solid ? '#fff' : button.color,
                                },
                            ]"
                            @onTapStart="onTapStart"
                            @onLoginSuccess="onTapButton"
                        >
                            <view>{{ button.text }}</view>
                            <view v-if="button.status === 3">
                                <view class="pack-price-btn__big__bar c-inline-block">
                                    <view class="real" :style="{width: button.percent + '%'}"/>
                                </view>
                                <text
                                    class="c-gap-left-small"
                                    :style="{
                                        color: 'rgba(255, 255, 255, 0.6)',
                                    }"
                                >{{ button.lastText }}</text>
                            </view>
                        </login-btn>
                        <!--default 按钮-->
                        <view
                            v-else
                            class="pack-prices-btn"
                            :style="{bottom: cPackageInfo.couponPrice && couponPriceStyle === 'sigma' ? '10px' : 0}"
                        >
                            <view
                                class="pack-price-btn"
                                :style="{
                                    bottom: cPackageInfo.couponPrice && couponPriceStyle === 'sigma' ? '10px' : 0,
                                }"
                            >
                                <c-button
                                    type="primary"
                                    :background="btnColor"
                                    border-radius="100px"
                                    :height="28"
                                    :font-size="14"
                                    font-weight="bold"
                                    auto
                                    :disabled="btnDisabled"
                                >
                                    立即预约
                                </c-button>
                            </view>
                        </view>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
/**
 * @file 会员体检Item
 * @author jiaxiangju
 * @categories 业务组件
 * @tags [component]
 */

/* global Page swan  getApp */
import CLabel from '@baidu/med-ui/components/Label/Label';
import CButton from '@baidu/med-ui/components/Button/Button';
import LoginBtn from '@commonComponents/login-btn';

import * as conf from '@utils/conf';
import {request} from '@utils/request';
import {navigate} from '@utils/navigate';
import * as login from '@utils/login';
import {ubcSend} from '@utils/log';

const BUTTON = [
    {
        text: '提醒我',
        solid: true,
        color: '#00C8C8',
        status: 1
    },
    {
        text: '取消提醒',
        solid: false,
        color: '#00C8C8',
        status: 2
    }
];

export default {
    config: {
        component: true
    },
    options: {
        addGlobalClass: true
    },
    components: {
        CLabel,
        CButton,
        LoginBtn
    },
    props: {
        /**
         * 套餐信息
         */
        packageInfo: {
            type: Object,
            default: () => {}
        },
        /**
         * 自定义套餐item颜色
         */
        itemStyle: {
            type: Object,
            default: () => ({
                'box-shadow': '0 10px 50px 0 rgba(0,0,0,0.06)',
                'margin-bottom': '20px'
            })
        },
        /**
         * 日志
         */
        log: {
            type: Object,
            default: () => {
                return {
                    value: '',
                    ext: {}
                };
            }
        },
        /**
         * 券后价样式sigma=>强样式， normal=>弱样式，体检2.0统一改为弱样式
         */
        couponPriceStyle: {
            type: String,
            default: 'normal'
        },
        /**
         * 登录态
         */
        isLogin: {
            type: Number,
            default: 0
        },
        /**
         * 数据位
         */
        index: {
            type: Number,
            default: 0
        },
        /**
         * 按钮颜色
         */
        btnColor: {
            type: String,
            default: '#FD503E'
        },
        /**
         * 控制按钮disabled状态
         */
        btnDisabled: {
            type: Boolean
        },
        /**
         * 来源
         */
        source: {
            type: String,
            default: 'normal'
        }
    },
    computed: {
        cPackageInfo() {
            // 老组件字段兼容
            const {nowPrice, storePrice, couponPrice, flashPrice} = this.packageInfo.price || {};
            let packageObj = {};
            Object.assign(packageObj, {
                address: this.packageInfo.address || (this.packageInfo.agency && this.packageInfo.agency.name),
                // 划线价
                lineationPrice: this.packageInfo.beforePrice || storePrice,
                distance: this.packageInfo.distance || (this.packageInfo.agency && this.packageInfo.agency.distance),
                id: this.packageInfo.id,
                img: this.packageInfo.img,
                name: this.packageInfo.name,
                // 原价
                price: nowPrice || this.packageInfo.price,
                // 券后价
                couponPrice: this.packageInfo.couponPrice || couponPrice,
                // 限时价
                flashPrice: this.packageInfo.flashPrice || flashPrice,
                sales: this.packageInfo.sales,
                url: this.packageInfo.url,
                oldTags: this.packageInfo.packageTags,
                newTags: this.packageInfo.tags || this.packageInfo.packageTagsNew
            });
            return packageObj;
        },
        price() {
            let price = this.cPackageInfo.couponPrice || this.cPackageInfo.price;
            if (price) {
                const priceArr = price.toString().split('.');
                const int = priceArr[0];
                const dec = priceArr[1];
                return {
                    int,
                    dec
                };
            }
            return {};
        },
        button() {
            return this.packageInfo.button;
        }
    },
    lifetimes: {
        detached() {
            // this.$set(this, 'button', null);
        }
    },
    methods: {
        onTapStart() {
            if (process.env.MARS_ENV === 'h5') {
                if (!this.isLogin && this.button.status < 3) {
                    login.toH5LoginPage({isReplaceUrl: true});
                } else {
                    this.onTapButton();
                }
            }
        },
        // 点击按钮
        onTapButton() {
            let button = this.button;
            let status = button.status;
            if (button.toast && status > 2) {
                this.$api.showToast({
                    icon: 'none',
                    title: button.toast
                });
                return;
            }
            if (button.url) {
                if (button.type && button.type === 'api') {
                    request({
                        url: conf.apiHost + button.url,
                        // url: conf.apiHost + button.url,
                        method: 'POST',
                        isNeedLogin: true,
                        data: Object.assign({}, {status: status}),
                        header: {
                            'content-type': 'application/x-www-form-urlencoded'
                        }
                    })
                        .then(res => {
                            let {data} = res;
                            this.$api.showToast({
                                icon: 'none',
                                title: data.toast
                            });
                            status = status === 1 ? 2 : 1;
                            // this.$set(this, 'button', Object.assign({}, button, BUTTON[status - 1]));
                            /**
                             * item数据变化
                             */
                            this.$emit('onBtnChange', Object.assign({}, button, BUTTON[status - 1]));
                        })
                        .catch(err => {
                            this.$api.showToast({
                                icon: 'none',
                                title: err
                            });
                        });
                } else if (button.type && button.type === 'link') {
                    navigate({url: button.url});
                }
            }
        },
        onItemTap(item) {
            if (item.url) {
                ubcSend({
                    type: 'clk',
                    value: this.log.value,
                    ext: Object.assign(
                        {
                            pd: 'member',
                            value_type: item.id
                        },
                        this.log.ext || {}
                    )
                });
                navigate({
                    url: item.url
                    // log: {
                    //     type: 'clk',
                    //     value: this.log.value,
                    //     ext: Object.assign({
                    //         pd: 'member',
                    //         value_type: 'tj',
                    //         value_id: item.id
                    //     }, this.log.ext || {})
                    // }
                });
            }
        }
    }
};
</script>
<style lang="less" scoped>
.pack-item {
    padding: 10px 13px;
    box-sizing: border-box;
    background-color: #fff;
    border-radius: 8px;

    &-address {
        color: #858585;
        margin-top: 0.01rem;
    }

    &-cont {
        display: flex;
        position: relative;
    }

    &-right {
        flex: 1;
        overflow: hidden;
        display: flex;
        flex-direction: column;
    }

    &-img {
        margin: 4px 0;
        width: 28.34vw;
        height: 28.34vw;
        border-radius: 6px;
        background-size: 100% 100%;
        flex-shrink: 0;
        margin-right: 8px;
    }

    &-name {
        color: #1f1f1f;

        &__jb {
            background-repeat: 'no-repeat';
            width: '28.34vw';
            height: '28.34vw';
            position: 'absolute';
            left: 0;
            top: 0;
            border-radius: '6px';
            background-size: 'cover';
        }
    }

    &-sales {
        color: #858585;
    }
}

.pack-tag-wrapper {
    display: flex;
    flex-wrap: wrap;
    margin: 0.02rem 0 0.03rem;
}

.pack-tag {
    display: inline;
    border-radius: 8px;
    margin-right: 5px;
    padding: 0 5px;
    word-break: keep-all;
    margin-bottom: 4px;
    line-height: 14px;
    border: 1px solid #f1f1f1;
    color: #999;
}

.pack-desc {
    flex: 1;
}

.pack {
    &-price {
        flex: 1;
        display: flex;
        justify-content: space-between;
        align-items: flex-end;

        &__fix {
            max-height: 0.27rem;
        }

        &-symbol {
            font-size: 13px;
            color: #fd503e;
            line-height: 10px;
            font-weight: bold;
        }

        &-now {
            font-weight: bold;
            font-size: 19px;
            color: #fd503e;
            line-height: 16px;
        }

        &-store {
            font-size: 13px;
            color: #858585;
            text-decoration: line-through;
            margin-left: 4px;
        }

        &-normal {
            color: #fd503e;
            margin-left: 4px;
        }

        &-symbol-color {
            color: #fd503e;
        }

        &-after {
            font-weight: bold;
        }

        .act-price {
            width: 1.58rem;

            .price {
                margin-top: 0.01rem;
                line-height: 20px;
            }
        }

        .act-price-row {
            display: flex;
            justify-content: space-between;
            line-height: 20px;
            padding-bottom: 2px;
        }

        .act-price-img {
            width: 100%;
            height: 5.6vw;
            background: url('https://med-fe.cdn.bcebos.com/vaccine/dc572f0656ed56daacc645ccdf2aa948.png');
            background-size: 100% 100%;
            background-repeat: no-repeat;
            text-align: right;
        }

        .act-price-text {
            width: 58px;
            text-align: center;
            padding-right: 4px;
        }

        &-btn {
            flex-shrink: 0;
            position: relative;
            bottom: 5px;

            &__big {
                width: 23.51vw;
                height: 39px;
                line-height: 19px;
                text-align: center;
                font-family: PingFangSC-Medium;
                font-weight: 500;
                border: 1px;
                border-radius: 4px;
                display: flex;
                flex-direction: column;
                justify-content: center;

                &__bar {
                    width: 37px;
                    height: 3px;
                    border-radius: 1px;
                    background: rgba(255, 255, 255, 0.32);
                    position: relative;

                    .real {
                        height: 3px;
                        border-radius: 1px;
                        background: #fff;
                        position: absolute;
                        left: 0;
                        top: 0;
                    }
                }
            }
        }

        &-lowest {
            position: relative;
            bottom: 9px;
            display: inline-block;
            min-width: 70px;
            text-align: center;
            padding-right: 4px;
        }
    }

    &-address {
        display: flex;
        justify-content: space-between;
        color: #858585;

        &-name {
            flex: 1;
            overflow: hidden;
        }
    }
}
@media only screen and (max-width: 360px) {
    .act-price-row {
        font-size: 0.13rem;
    }
}

.new-gap-right-small {
    margin-right: 0.04rem;
}
</style>
