<template>
    <view
        class="good-item"
        :class="extendClass ? 'good-item__' + extendClass : ''"
        @tap="onClick"
    >
        <!-- 图片 -->
        <view
            class="good-item__img"
            :style="{
                'border-radius': radius,
            }"
        >
            <view v-if="extendClass === 'column2' && data.operateTag" class="good-item__img__tag">
                <text class="good-item__img__tag__text">{{ data.operateTag }}</text>
            </view>
            <view class="good-item__img__inner">
                <c-image
                    has-compress
                    class="good-item__img__inner__image"
                    :src="data.headImage"
                    width="100%"
                    height="auto"
                    mode="widthFix"
                    :extra="{column: 6}"
                    :hack-white-stroke="false"
                    :is-round="false"
                />
                <view
                    v-if="data.explode"
                    class="good-item__img__left-icon c-font-normal c-font-wb"
                >
                    {{ data.explode }}
                </view>
                <view
                    v-if="data.heat"
                    class="good-item__img__bottom-icon c-line-clamp1 c-font-13"
                >
                    <view class="good-item__img__bottom-icon__text">{{ data.heat }}</view>
                </view>
                <view class="good-item__img__inner__imgback"/>
            </view>
        </view>
        <!-- 内容 -->
        <view class="good-item__content">
            <view v-if="extendClass === 'column2' && data.label" class="label-wrap">
                <c-label
                    v-for="(item, index) in data.label"
                    :key="index"
                    color="#F3283B"
                    :text="item"
                    :font-size="13"
                    :height="19"
                    bg-color="#FFF0EF"
                    border-color="#F3283B"
                    custom
                />
            </view>
            <!-- 标题 -->
            <view
                v-if="data.commonName || data.titleName"
                :class="'c-line-clamp' + titleLine"
                :style="{
                    'height': (titleSize + 6) * titleLine + 'px',
                    'font-size': titleSize + 'px',
                    'line-height': titleSize + 6 + 'px',
                }"
                class="c-gap-top-small-px good-item__content__name"
            >
                {{ data.commonName || data.titleName }}
            </view>
            <!-- 推荐语 -->
            <view v-if="extendClass === 'column1' && data.persuade" class="good-item__content__recommend">
                <view class="good-item__content__recommend__tit c-font-13">推荐语</view>
                <view
                    class="good-item__content__recommend__info c-line-clamp1 c-font-normal"
                >
                    {{ data.persuade || '' }}
                </view>
            </view>
            <!-- 标签 -->
            <view v-if="extendClass !== 'column1' && extendClass !== 'column2' && data.label" class="label-wrap">
                <c-label
                    v-for="(item, index) in data.label"
                    :key="index"
                    color="#FD5030"
                    :text="item"
                    :font-size="10"
                    :height="16"
                    bg-color="rgba(253,80,62,0.10)"
                    border-color="#FD503E"
                    class="c-gap-bottom-small"
                    custom
                />
            </view>
            <view v-if="extendClass === 'column1' && data.label" class="label-wrap">
                <c-label
                    v-for="(item, index) in data.label"
                    :key="index"
                    color="#ff6600"
                    :text="item"
                    :font-size="13"
                    :height="16"
                    bg-color="rgba(255,102,0,0.10)"
                    border-color="#ff6600"
                    custom
                />
            </view>
            <view
                v-if="data.price"
                class="good-item__price minw-price c-font-13 c-line-clamp1"
            >
                <view v-if="extendClass !== 'column1' && disableCart" class="good-item__price__right">
                    <trolleys-mini
                        :disabled="parseInt(data.status, 10) !== 1"
                        :background-image="'linear-gradient(125deg, #FE7429 0%, #FF2C54 100%)'"
                        @trolleysTap="addTrolleys(data, index)"
                    />
                </view>
                <view class="good-item__price__real">
                    <view v-if="data.price" :style="{'display': 'inline-block'}">
                        <price
                            v-if="data.price"
                            :price="data.price / 100"
                            :u-size="extendClass === 'column1' ? '.13rem' : '.14rem'"
                            :i-size="extendClass === 'column1' ? '.27rem' : '.19rem'"
                            :d-size="extendClass === 'column1' ? '.19rem' : '.14rem'"
                            :p-style="extendClass === 'column1'
                                ? {'color': '#fff', 'margin-left': '.1rem', 'position': 'relative', 'top': '1px'}
                                : extendClass === 'column2' ? {'color': '#FFFDDF'} : {}"
                        />
                    </view>
                    <text v-if="disableCouponText && data.priceText" class="text">{{ data.priceText }}</text>
                </view>
                <view v-if="extendClass === 'column2'" class="good-item__price__text">
                    <view style="width: 27px;">立即抢购</view>
                </view>
                <view
                    v-if="extendClass !== 'column2'"
                    class="c-color-gray good-item__price__line"
                    :class="{'z-line c-gap-left-small': extendClass === 'column1'}"
                >
                    <text v-if="data.orgPrice">￥{{ data.orgPrice / 100 }}</text>

                </view>
                <view
                    v-if="extendClass === 'column1'"
                    class="good-item__price__cart"
                    @tap.stop="addTrolleys(data, index)"
                />
            </view>
        </view>
    </view>
</template>
<script>
/**
* @file 商品
* @author jiaxiangju
* @categories 业务组件
* @tags [component]
*/
import CImage from '@components/common/image';
import Price from '@commonComponents/Price';
import CLabel from '@baidu/med-ui/components/Label/Label';
import TrolleysMini from '../../../components/gy/trolleys-mini';
import * as TrolleysUtil from '@utils/newTrolleys';
import {ubcSend} from '@utils/log';
import {navigate} from '@utils/navigate';
export default {
    config: {
        component: true
    },
    options: {
        addGlobalClass: true
    },
    props: {
        /**
         * 商品数据
         */
        data: {
            type: Object,
            default() {
                return {};
            }
        },
        /**
         * 加车接口url
         */
        asyncAddUrl: {
            type: String,
            default: ''
        },
        /**
         * 商品顺序号
         */
        index: {
            type: Number,
            default: 0
        },
        /**
         * 扩展类名
         */
        extendClass: {
            type: String,
            default: ''
        },
        /**
         * 商品标题字号
         */
        titleSize: {
            'type': Number,
            'default': 15
        },
        /**
         * 商品标题几行显示
         */
        titleLine: {
            'type': Number,
            'default': 2
        },
        /**
         * 商品图片圆角值
         */
        radius: {
            'type': String,
            'default': '.09rem'
        },
        /**
         * 禁用购物车
         */
        disableCart: {
            'type': Boolean,
            'default': true
        },
        /**
         * 禁用券后价文案
         */
        disableCouponText: {
            'type': Boolean,
            'default': true
        },
        /**
         * 打点
         */
        log: {
            type: Object,
            default: () => {
                return {
                    page: '',
                    value: '',
                    ext: {
                        pd: 'act'
                    }
                };
            }
        }
    },
    components: {
        Price,
        CLabel,
        TrolleysMini,
        CImage
    },
    created() {
    },
    computed: {},
    data() {
        return {};
    },
    watch: {},
    methods: {
        onClick() {
            if (this.data && this.data.targetUrl) {
                navigate({
                    url: this.data.targetUrl,
                    log: {
                        page: this.log.page,
                        type: 'clk',
                        value: this.log.value,
                        ext: Object.assign({
                            pd: 'act',
                            value_type: 'drug',
                            value_id: this.data.drugId
                        }, this.log.ext || {})
                    }
                });
            }
        },
        addTrolleys(drug, index) {
            ubcSend({
                page: this.log.page,
                type: 'clk',
                value: this.log.value,
                ext: Object.assign({
                    pd: 'act',
                    value_type: 'addcart',
                    value_id: this.data.drugId
                }, this.log.ext || {})
            });
            if (!this.asyncAddUrl) {
                return;
            }
            if (drug) {
                if (+drug.status !== 1) {
                    // 无库存
                    this.$api.showToast({
                        icon: 'none',
                        title: drug.statusToast || '没有库存了，看看其他药品吧'
                    });
                    return;
                }
            }
            TrolleysUtil.asyncAddTrolleys({url: this.asyncAddUrl, drug: drug})
                .then(({data}) => {
                    const drugNum = +data.drugNum || 0;
                    /** 购物车数量改变 */
                    this.$emit('changeCartNum', drugNum);
                });
        }
    }
};
</script>
<style lang="less" scoped>
.good-item {
    position: relative;

    &__img {
        overflow: hidden;
        position: relative;

        &__tag {
            padding: 0 0.078rem;
            height: 0.21rem;
            position: absolute;
            top: 0;
            left: 0;
            border-radius: 0.08rem 0 0.1rem 0;
            background-image: linear-gradient(150deg, #ff9450 0%, #f3283b 100%);
            z-index: 2;

            &__text {
                background: linear-gradient(to right, #fff0d7, #ffd9a6);
                -webkit-background-clip: text;
                color: transparent;
                font-weight: bold;
                font-size: 0.14rem;
                line-height: 0.21rem;
            }
        }

        &__inner {
            height: auto;
            padding-top: calc(100%);
            position: relative;
            overflow: hidden;

            &__image {
                width: 100%;
                height: auto;
                position: absolute;
                left: 0;
                top: 0;
                border-radius: 0.09rem;
            }

            &__imgback {
                width: 100%;
                height: 100%;
                position: absolute;
                top: 0;
                bottom: 0;
                left: 0;
                right: 0;
                z-index: 1;
                border-radius: 0.09rem;
                background: radial-gradient(rgba(0, 0, 0, 0) 30%, rgba(0, 0, 0, 0.08) 100%);
            }
        }

        &__left-icon {
            position: absolute;
            top: 0;
            left: .086666rem;
            width: .213333rem;
            height: .23rem;
            background: url(https://medactivity.cdn.bcebos.com/act618/crowd/hot.png);
            background-size: cover;
            background-repeat: no-repeat;
            color: #fffdf8;
            text-align: center;
        }

        &__bottom-icon {
            position: absolute;
            bottom: 0;
            width: 100%;
            height: .26rem;
            color: #fff;
            background: url(https://medactivity.cdn.bcebos.com/act618/crowd/hot-num-bg.png);
            background-size: 100% auto;
            background-repeat: no-repeat;
            display: flex;
            // justify-content: flex-end;
            align-items: center;

            &__text {
                // padding-right: 4px;
                text-align: left;
                margin-left: .285rem;
            }
        }
    }

    &__content {
        &__name {
            color: #1f1f1f;
            margin-bottom: 0;
            margin-bottom: .05rem;
        }
    }

    &__name,
    &__price {
        color: #fd503e;
        line-height: .23rem;
        font-size: .12rem;
        margin-bottom: 0;
        // padding-top: .03rem;
        &__line {
            height: .14rem;
            line-height: .14rem;
            text-decoration: line-through;
            margin: .03rem 0;
        }

        &__right {
            position: absolute;
            bottom: 0;
            right: 0;
        }
    }

    .c-gap-top-small-px {
        margin-top: .06333rem;
    }

    .z-line {
        display: inline;
    }

    // 一列显示时样式
    &__column1 {
        display: flex;

        .good-item__img {
            flex: 1;
            border-radius: 0.09rem;
            // width: 1.173rem;
            // height: 1.173rem;
            .good-item__img__inner {
                border-radius: .09rem;
            }
        }

        .good-item__content {
            width: 66%;
            margin-left: .08rem;

            .good-item__content__name {
                margin-top: 0;
                margin-bottom: .05rem;
            }

            .label-wrap {
                margin-top: .08rem;
            }

            .good-item__content__recommend {
                display: flex;
                // margin-top: .08rem;
                padding-top: 0.02rem;

                .good-item__content__recommend__tit {
                    background: url(https://medactivity.cdn.bcebos.com/act618/crowd/recommend-bg.png);
                    background-size: 100% auto;
                    background-repeat: no-repeat;
                    width: .49rem;
                    height: .19rem;
                    line-height: .19rem;
                    color: #fff;
                    text-align: center;
                }

                .good-item__content__recommend__info {
                    flex: 1;
                    color: #fd503e;
                    height: .19rem;
                    line-height: .19rem;
                    margin-left: .08rem;
                }
            }
        }

        .good-item__price {
            position: relative;
            background: url(https://medactivity.cdn.bcebos.com/act618/crowd/cart-bg1.png);
            background-size: 100% auto;
            background-repeat: no-repeat;
            color: #fff;
            height: 42px;
            display: flex;
            align-items: baseline;
            padding-top: 6px;
            box-sizing: border-box;
            margin-top: .08rem;

            .good-item__price__real {
                .text {
                    margin-left: 0.05rem;
                }
            }

            .good-item__price__line {
                color: #fff;
                font-size: .13rem;
                margin: 0 0 0 .05rem;
            }

            .good-item__price__cart {
                position: absolute;
                right: 0;
                top: 0;
                width: .6833rem;
                height: 100%;
            }
        }
    }

    &__column2 {
        .good-item__img {
            // width: 1.173rem;
            // height: 1.173rem;
            .good-item__img__inner {
                border-radius: 0;
            }
        }

        .good-item__content {
            width: 1.67rem;
            position: relative;
            transform: translateY(-0.095rem);
            margin: 0 auto;

            .label-wrap {
                display: flex;
                justify-content: center;
            }

            .good-item__content__name {
                margin-top: 0.04rem;
                margin-bottom: .05rem;
                font-size: 0.14rem;
                line-height: 0.14rem;
            }

            .good-item__content__recommend {
                display: flex;
                // margin-top: .08rem;
                padding-top: 0.02rem;

                .good-item__content__recommend__tit {
                    background: url(https://medactivity.cdn.bcebos.com/act618/crowd/recommend-bg.png);
                    background-size: 100% auto;
                    background-repeat: no-repeat;
                    width: .49rem;
                    height: .19rem;
                    line-height: .19rem;
                    color: #fff;
                    text-align: center;
                }

                .good-item__content__recommend__info {
                    flex: 1;
                    color: #fd503e;
                    height: .19rem;
                    line-height: .19rem;
                    margin-left: .08rem;
                }
            }
        }

        .good-item__price {
            position: relative;
            background: url(https://medactivity.cdn.bcebos.com/22new_year_day/prima/good_recommand_btn.png);
            background-size: 100% auto;
            background-repeat: no-repeat;
            color: #fff;
            height: 42px;
            display: flex;
            box-sizing: border-box;
            margin-top: .08rem;
            justify-content: space-between;
            align-items: center;

            &__real {
                color: #fff;
                font-size: .13rem;
                margin: 0;
                display: flex;
                justify-content: center;
                align-items: center;
                flex-wrap: wrap;

                .text {
                    width: 100%;
                    text-align: center;
                    margin-top: -0.08rem;
                    margin-left: 0.05rem;
                    color: #fffddf;
                }
            }

            &__text {
                white-space: pre-wrap;
                line-height: 0.15rem;
                font-size: 0.12rem;
                color: #fffddf;
                font-weight: bold;
                margin-right: 0.153rem;
                letter-spacing: 0;
            }

            &__line {
                color: #858585;
                font-size: .13rem;
            }
        }
    }
}
@media only screen and (max-width: 375px) {
    .good-item.column1 .good-item__price {
        padding-top: 4px;
        padding-bottom: 0;
        // min-height: initial;
    }

    .good-item__price.minw-price {
        padding-bottom: 6px;
        // min-height: 37px;
    }

    .good-item__img__bottom-icon__text {
        margin-left: .27rem;
        font-size: 11px;
    }
}
@media only screen and (max-width: 393px) {
    .good-item__price.minw-price {
        padding-bottom: 6px;
        // min-height: 37px;
    }

    .good-item__img__bottom-icon__text {
        margin-left: .27rem;
        font-size: 11px;
    }
}
@media only screen and (max-width: 320px) {
    .good-item.column1 .good-item__price {
        padding-top: 3px;
    }

    .good-item__img__bottom-icon__text {
        margin-left: .20rem;
        font-size: 10px;
    }
}
</style>
