<template>
    <view>
        <view v-if="(data_base || null) != null">
            <!-- 列表 -->
            <scroll-view :scroll-y="true" class="scroll-box scroll-box-ece-nav plugins-binding-data-list" @scrolltolower="scroll_lower" lower-threshold="60">
                <view v-if="(data_list || null) != null && data_list.length > 0" class="data-list padding-horizontal-main padding-top-main oh">
                    <block v-for="(item, index) in data_list" :key="index">
                        <view class="item border-radius-main bg-white padding-main oh spacing-mb">
                            <navigator :url="'/pages/plugins/binding/detail/detail?id=' + item.id" hover-class="none">
                                <image :src="item.images" mode="aspectFit" class="images fl dis-block border-radius-main"></image>
                                <view class="base-right fr bs-bb">
                                    <view class="fw-b text-size cr-base single-text">{{item.title}}</view>
                                    <view class="cr-grey margin-top-xs text-size-xs multi-text">{{item.describe}}</view>
                                    <view class="sales-price margin-top-sm single-text">{{currency_symbol}}{{item.estimate_price}}</view>
                                    <view v-if="item.estimate_original_price != 0" class="original-price margin-top-xs single-text">{{currency_symbol}}{{item.estimate_original_price}}</view>
                                    <view v-if="(item.estimate_discount_price || 0) != 0" class="margin-top-sm single-text">
                                        <text class="discount-icon cr-white text-size-xs">节省</text>
                                        <text class="cr-green">{{currency_symbol}}{{item.estimate_discount_price}}</text>
                                    </view>
                                </view>
                            </navigator>
                        </view>
                    </block>
                </view>
                <view v-else>
                    <!-- 提示信息 -->
                    <component-no-data :propStatus="data_list_loding_status" :propMsg="data_list_loding_msg"></component-no-data>
                </view>

                <!-- 结尾 -->
                <component-bottom-line :propStatus="data_bottom_line_status"></component-bottom-line>

                <!-- 回到店铺 -->
                <view v-if="(shop || null) != null && shop.length > 0" class="bottom-fixed padding-main">
                    <button class="bg-main br-main cr-white round dis-block" type="default" hover-class="none" size="mini" @tap="shop_event" :data-value="shop.url">
                        <view class="dis-inline-block va-m">
                            <uni-icons type="shop" size="16" color="#fff"></uni-icons>
                        </view>
                        <text class="va-m margin-left-sm">回到店铺</text>
                    </button>
                </view>
            </scroll-view>
        </view>
    </view>
</template>
<script>
    const app = getApp();
    import componentNoData from "../../../../components/no-data/no-data";
    import componentBottomLine from "../../../../components/bottom-line/bottom-line";

    export default {
        data() {
            return {
                data_list_loding_status: 1,
                data_list_loding_msg: '',
                data_bottom_line_status: false,
                currency_symbol: app.globalData.data.currency_symbol,
                data_list: [],
                data_total: 0,
                data_page_total: 0,
                data_page: 1,
                params: null,
                data_base: null,
                shop: null,
                // 自定义分享信息
                share_info: {}
            };
        },

        components: {
            componentNoData,
            componentBottomLine
        },

        onLoad(params) {
            this.setData({
                params: params
            });
            
            // 初始化配置
            this.init_config();
                    
            // 获取数据
            this.get_data();
        },

        // 下拉刷新
        onPullDownRefresh() {
            this.setData({
                data_page: 1
            });
            this.get_data_list(1);
        },

        methods: {
            // 初始化配置
            init_config(status) {
                if ((status || false) == true) {
                    this.setData({
                        currency_symbol: app.globalData.get_config('currency_symbol'),
                    });
                } else {
                    app.globalData.is_config(this, 'init_config');
                }
            },

            // 初始化
            get_data() {
                uni.request({
                    url: app.globalData.get_request_url("index", "index", "binding"),
                    method: 'POST',
                    data: this.params,
                    dataType: 'json',
                    success: res => {
                        uni.stopPullDownRefresh();
                        if (res.data.code == 0) {
                            var data = res.data.data;
                            this.setData({
                                data_base: data.base || null,
                                shop: data.shop || null
                            });
                            
                            if ((this.data_base || null) != null) {
                                // 基础自定义分享
                                this.setData({
                                    share_info: {
                                        title: this.data_base.seo_title || this.data_base.application_name,
                                        desc: this.data_base.seo_desc,
                                        path: '/pages/plugins/binding/index/index'
                                    }
                                });

                                // 导航名称
                                if((this.data_base.application_name || null) != null) {
                                    uni.setNavigationBarTitle({
                                        title: this.data_base.application_name
                                    });
                                }
                            }

                            // 获取列表数据
                            this.get_data_list(1);
                        } else {
                            this.setData({
                                data_list_loding_status: 0,
                                data_list_loding_msg: res.data.msg
                            });
                            app.globalData.showToast(res.data.msg);
                        }
                        
                        // 分享菜单处理
                        app.globalData.page_share_handle(this.share_info);
                    },
                    fail: () => {
                        uni.stopPullDownRefresh();
                        this.setData({
                            data_list_loding_status: 2
                        });
                        app.globalData.showToast('服务器请求出错');
                    }
                });
            },

            // 获取数据列表
            get_data_list(is_mandatory) {
                // 分页是否还有数据
                if ((is_mandatory || 0) == 0) {
                    if (this.data_bottom_line_status == true) {
                        uni.stopPullDownRefresh();
                        return false;
                    }
                }

                // 加载loding
                uni.showLoading({
                    title: '加载中...'
                });

                // 获取数据
                uni.request({
                    url: app.globalData.get_request_url("datalist", "index", "binding"),
                    method: 'POST',
                    data: {
                        page: this.data_page,
                        shop_id: this.params.spid || 0
                    },
                    dataType: 'json',
                    success: res => {
                        uni.hideLoading();
                        uni.stopPullDownRefresh();
                        if (res.data.code == 0) {
                            var data = res.data.data;
                            if (data.data.length > 0) {
                                if (this.data_page <= 1) {
                                    var temp_data_list = data.data;
                                } else {
                                    var temp_data_list = this.data_list || [];
                                    var temp_data = data.data;
                                    for (var i in temp_data) {
                                        temp_data_list.push(temp_data[i]);
                                    }
                                }
                                this.setData({
                                    data_list: temp_data_list,
                                    data_total: data.total,
                                    data_page_total: data.page_total,
                                    data_list_loding_status: 3,
                                    data_page: this.data_page + 1
                                });
                                
                                // 是否还有数据
                                this.setData({
                                    data_bottom_line_status: (this.data_page > 1 && this.data_page > this.data_page_total)
                                });
                            } else {
                                this.setData({
                                    data_list_loding_status: 0
                                });
                                if (this.data_page <= 1) {
                                    this.setData({
                                        data_list: [],
                                        data_bottom_line_status: false
                                    });
                                }
                            }
                        } else {
                            this.setData({
                                data_list_loding_status: 0,
                                data_list_loding_msg: res.data.msg
                            });
                            app.globalData.showToast(res.data.msg);
                        }
                    },
                    fail: () => {
                        uni.hideLoading();
                        uni.stopPullDownRefresh();
                        this.setData({
                            data_list_loding_status: 2
                        });
                        app.globalData.showToast('服务器请求出错');
                    }
                });
            },

            // 滚动加载
            scroll_lower(e) {
                this.get_data_list();
            },

            // 店铺事件
            shop_event(e) {
                var prev_url = app.globalData.prev_page();
                if(prev_url != null && prev_url.indexOf('pages/plugins/shop/detail/detail') != -1) {
                    uni.navigateBack();
                } else {
                    app.globalData.url_event(e);
                }
            }
        }
    };
</script>
<style>
    @import './index.css';
</style>