<template>
    <view class="page-layout" :style="{ backgroundColor: backgroundColor, minHeight: minHeight }">
        <view
            class="page-header"
            :class="[textColor]"
            :style="{
                height: header.height + 'px',
                paddingTop: header.pt + 'px',
                backgroundColor: headerBackgroundColor,
            }"
        >
            <!-- 首页选择地址 -->
            <block v-if="btnType === 'city'">
                <view class="page-index-btn change-city" :style="{ height: header.height + 'px' }" @click="changeCity">
                    <text class="iconfont icon-31dingwei"></text>
                    <text class="city">成都市</text>
                </view>
            </block>
            <!-- 正常返回按钮 -->
            <block v-if="btnType === 'back'">
                <view class="page-index-btn back" :style="{ height: header.height + 'px' }" @click="onClick">
                    <text class="iconfont icon-fanhui"></text>
                </view>
            </block>
            <view class="page-title">
                <text class="title-text" :style="{ lineHeight: header.height + 'px' }">{{ title }}</text>
            </view>
        </view>
        <view class="page-body" :style="{ paddingTop: bodyPt + 'px' }">
            <slot></slot>
        </view>
        <view class="page-footer"></view>
    </view>
</template>

<script>
import { mapState } from "vuex";
export default {
    name: "component-layout",
    data() {
        return {
            header: {
                height: 0,
                pt: 0,
            },
            bodyPt: 0,
        };
    },
    components: {},
    props: {
        minHeight: {
            type: String,
            default: "100vh",
        },
        backgroundColor: {
            type: String,
            default: "unset",
        },
        headerBackgroundColor: {
            type: String,
            default: "unset",
        },
        customBtn: {
            type: Boolean,
            default: false,
        },
        btnType: {
            type: String,
            default: "back",
        },
        title: {
            type: String,
            default: "",
        },
        textColor: {
            type: String,
            default: "dark",
        },
    },
    computed: {
        ...mapState({
            config: (state) => state.system.config,
        }),
    },
    mounted() {
        const { statusBarHeight, headerHeight } = this.config;
        // #ifndef H5
        this.header.pt = statusBarHeight;
        this.header.height = headerHeight;
        // #endif
        // #ifdef H5
        this.header.pt = 0;
        this.header.height = this.$utils.rpx2px(headerHeight * 2);
        // #endif
        this.bodyPt = this.header.pt + this.header.height;
    },
    methods: {
        onClick() {
            if (this.customBtn) {
                this.$emit("onClickBtn");
            } else {
                uni.navigateBack({ delta: 1 });
            }
        },
        changeCity() {
            console.log("修改城市");
        },
    },
};
</script>

<style scoped lang="less">
.page-layout {
    position: relative;
    max-width: 750px;
    width: 100%;
    margin: 0 auto;
    /* #ifdef H5 */
    // 修复自定义tabBar后的遮挡问题
    // padding-bottom: calc(@tabBarHeight - 50px);
    /* #endif */
}
.page-header.dark {
    .page-title,
    .page-index-btn {
        color: #2d2d2d;
    }
}
.page-header.light {
    .page-title,
    .page-index-btn {
        color: #ffffff;
    }
}
.page-header {
    z-index: 20;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    max-width: 750px;
    width: 750rpx;
    text-align: center;
    margin: 0 auto;
    .page-title {
        width: 100%;
        height: auto;
        line-height: 0;
        .title-text {
            font-size: 30rpx;
            font-weight: bold;
        }
    }
    .page-index-btn {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 180rpx;
        padding-left: 30rpx;
        line-height: 0;
        display: flex;
        align-items: center;
        justify-content: flex-start;
    }
    .page-index-btn.change-city {
        font-size: 24rpx;
        .icon-31dingwei {
            font-size: 40rpx;
        }
        .city {
            margin-left: 5rpx;
        }
    }
    .page-index-btn.back {
        .icon-fanhui {
            font-size: 40rpx;
            font-weight: bold;
        }
    }
}
.page-body {
    width: 100%;
}
.page-footer {
    width: 100%;
}
</style>