<template>
    <view>
        <div class="bg-color-white py2">
        <u-search placeholder="请输入" clearabled v-model="keyword" @search="search" @custom="search" @clear="search"></u-search>
        </div>
        <common-tabs :list="list" :current="current" @change="change" />
        <view class="main p-top8">
            <template v-if="listData.length > 0">
                <view
                    class="mx3 my2 p3 bg-color-white box p-relative"
                    v-for="item in listData"
                    :key="item.id"
                    @tap="handleDetail(item.id)"
                    @longpress="showMask(item.id,$event)">
                    <!--  -->
                    <common-mask :item="item" :maskId="maskId" @hide="maskId = 0" @del="handleDel(item.id)" />
                    <!--  -->
                    <view class="d-flex j-between border-bottom pb-2 a-center">
                        <view class="text-color-b f-size-26">{{ item.name }}</view>
                        <my-badge :text="statusMap[item.status]" :status="dict[item.status]" />
                    </view>
                    <view class="mt-2 d-flex j-between">
                        <view class="text-color-3 f-size-32 f-weight-bold">{{
                             item.no
                        }}</view>
                    </view>
                    <view class="f-size-26 text-color-6 f-weight-4 mt-2">{{
                        item.model
                    }}</view>
                </view>
                <view class="py3">
                    <u-loadmore :status="status" />
                </view>
            </template>
            <template v-else>
                <view class="empty">
                    <u-empty text="暂无数据" mode="list"></u-empty>
                </view>
            </template>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            list: [],
            current: 0,
            // 加载更多
            status: 'loadmore',
            keyword:'',
            page: 1,
            limit: 10,
            type: '',
            loadMore: true,
            listData: [],
            statusMap: {},
            dict: null,
            maskId: 0,
            maskState: false
        };
    },
    onPullDownRefresh() {
        // 请求结束取消刷新
        this.page = 1;
        this.loadMore = true;
        this.listData = [];
        this.initData();
        uni.stopPullDownRefresh();
    },
    onReachBottom() {
        if (this.loadMore) {
            this.page++;
            this.initData();
        }
    },
    onLoad() {
        this.statusMap = this.$DictCache.getLabelByValueMapByType(
            this.$DictCache.TYPE.SB_INFO_STATUS
        );
        this.list = [{
            label: '全部',
            value: ''
        }].concat(
            this.$DictCache.getChildrenList(this.$DictCache.TYPE.SB_INFO_STATUS)
        );
        this.dict = this.$DictCache.COLOR.SB_INFO_STATUS;
        this.initData();
    },
    onHide() {
        this.maskId = 0
    },
    methods: {
        showMask(id, e) {
            this.maskId = id
            // #ifdef H5
            e.preventDefault()
            // #endif
        },
        handleDel(id) {
            console.log(id);
            const that = this;
            const params = [];
            params.push(id);
            uni.showModal({
                title: '提示',
                content: '您确认删除吗?',
                success: (res) => {
                    if (res.confirm) {
                        this.$Http.deleteSbInfos(params).then((res) => {
                            uni.showToast({
                                title: '删除成功'
                            });
                            this.page = 1;
                            this.loadMore = true;
                            this.listData = [];
                            this.initData();
                        });
                    } else if (res.cancel) {}
                }
            });
        },
        // 跳转详情
        handleDetail(id) {
            if (this.maskId != 0) return this.maskId = 0
            if (this.maskState) return this.maskState = false
            uni.navigateTo({
                url: '/pages/equipment-detail/equipment-detail?detailId=' + id
            });
        },
        // 点击 tab
        change(index) {
            this.current = index;
            if (index == 0) {
                this.type = '';
            } else {
                this.type = index;
            }
            this.page = 1;
            this.loadMore = true;
            this.listData = [];
            this.initData();
        },
        search(){
               this.page = 1;
            this.loadMore = true;
            this.listData = [];
            this.initData();
        },
        initData() {
            const that = this;
            that.status = 'loading';
            this.$Http
                .getSbInfoPage({
                    pageNum: this.page,
                    pageSize: this.limit,
                    keyword:this.keyword,
                    filter: -1,
                    status: this.type,
                    dataScope: {
                        sortBy: 'asc',
                        sortName: 'no'
                    }
                })
                .then((res) => {
                    const data = res.data.rows;
                    if (data && data.length > 0 && data.length < that.limit) {
                        that.status = 'nomore';
                        that.loadMore = false;
                    } else if (data.length === that.limit) {
                        that.status = 'loadmore';
                    } else {
                        that.status = 'nomore';
                    }
                    that.listData = [...that.listData, ...data];
                });
        }
    }
};
</script>

<style lang="less">
	page {
		background-color: #f4f4f4;
	}

	.main {
		.box {
			border-radius: 20rpx;
			padding-right: 0;
		}
	}

</style>
