<template>
    <view>
        <my-add-button
            @handleClick="addChange"
            :is-batch="true"
            :button-list="addButtonList"
        />
        <my-page
            :loadData="loadData"
            :swipe-action="true"
            @rowClick="handleDetail"
            :row-show-params="{ sbUseTypeMap }"
            :page-size="10"
            ref="myPage"
        >

            <template #pageRow="{ recordModel, rowShowParams }">
                <view
                    class="mx3 my2 p3 bg-color-white box p-relative"
                    @longpress="showMask(recordModel.id,$event)">
                    <common-mask
                        :item="recordModel"
                        :is-edit="true"
                        :maskId="maskId"
                        @hide="maskId = 0"
                        @edit="handleEdit(recordModel)"
                        @del="handleDel(recordModel)" />
                    <view class="d-flex j-between border-bottom pb-2 a-center">
                        <view class="text-color-b f-size-26">{{
                            rowShowParams.sbUseTypeMap[recordModel.sbModelUseType]
                        }}</view>
                        <view class="f-size-24 state-orange">{{
                            recordModel.updateTime
                        }}</view>
                    </view>
                    <view class="mt-2 d-flex j-between">
                        <view class="text-color-3 f-size-32 f-weight-bold">{{
                            recordModel.sbName || ''
                        }}</view>
                        <view class="text-color-3 f-size-32 f-weight-bold">{{
                            recordModel.zbh || ''
                        }}</view>
                    </view>
                    <view class="mt-2 d-flex j-between">
                        <view class="f-size-26 text-color-6 f-weight-4 mt-2">
                            {{ recordModel.sbNo||'' }}
                        </view>
                        <view class="f-size-26 text-color-6 f-weight-4 mt-2 pr-3">
                            {{ recordModel.fillDate || '' }}
                        </view>
                    </view>
                </view>
            </template>
        </my-page>
    </view>
</template>

<script>
export default {
    data() {
        return {
            sbUseTypeMap: {},
            maskId: 0,
            addButtonList: [
                {
                    text: '扫码',
                    index: 0
                },
                {
                    text: '新增',
                    index: 1
                },
                {
                    text: '批量复制',
                    index: 2
                },
                {
                    text: '批量修改',
                    index: 3
                }
            ]
        };
    },
    onPullDownRefresh() {
        this.$refs.myPage.pagePullDownRefresh();
    },
    onReachBottom() {
        this.$refs.myPage.pageReachBottom();
    },
    onLoad() {
        this.sbUseTypeMap = this.$DictCache.getLabelByValueMapByType(
            this.$DictCache.TYPE.SB_USE_TYPE
        );
        this.$nextTick(() => {
            this.$refs.myPage.initPage();
        });
    },
    onHide() {
        this.maskId = 0
    },
    methods: {
        addChange(select) {
            const index = select.index
            if (index === 0) {
                uni.scanCode({
                    success(e) {
                        console.log(e.result);
                        if (e.result) {
                            uni.navigateTo({
                                url: '/pages/running/running-edit?sbId=' + e.result
                            });
                        }
                    }
                });
            } else if (index === 1) {
                uni.navigateTo({
                    url: '/pages/running/running-edit'
                });
            } else if (index === 2) {
                this.$Http
                    .copySbRunFill({})
                    .then((res) => {
                        this.$refs.myPage.pagePullDownRefresh();
                    });
            } else {
                uni.navigateTo({
                    url: '/pages/running/running-batch-select'
                });
            }
        },
        showMask(id, e) {
            this.maskId = id
            // #ifdef H5
            e.preventDefault()
            // #endif
        },
        loadData(parameter) {
            return this.$Http
                .getSbRunFillPage({
                    ...parameter,
                    filter: 1
                })
                .then((res) => {
                    return res.data;
                });
        },
        handleEdit(item) {
            uni.navigateTo({
                url: '/pages/running/running-edit?id=' + item.id
            });
        },
        // 删除
        handleDel(item) {
            const that = this;
            const params = [];
            params.push(item.id);
            uni.showModal({
                title: '提示',
                content: '您确认删除吗?',
                success: (res) => {
                    if (res.confirm) {
                        this.$Http.deleteSbRunFills(params).then((res) => {
                            uni.showToast({ title: '删除成功' });
                            that.$refs.myPage.listData = [];
                            that.$refs.myPage.loadMore = true;
                            that.$refs.myPage.pageNum = 1;
                            that.$refs.myPage.initData();
                        });
                    } else if (res.cancel) {
                    }
                }
            });
        },
        // 跳转详情
        handleDetail(item) {
            if (this.maskId !== 0) {
                this.maskId = 0
                return
            }
            uni.navigateTo({
                url: '/pages/running/running-detail?id=' + item.id
            });
        }
    }
};
</script>

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

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

.p-top8 {
  padding-top: 0rpx;
}
</style>
