<template>
    <view>
        <common-tabs :list="list" :current="current" @change="change" />
        <view class="p-top8">
            <template v-if="listData.length > 0">
                <view
                    class="mx3 my2 p3 bg-color-white box p-relative"
                    v-for="(item, index) in listData"
                    :key="index"
                    @tap="handleDetail(item)"
                    @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.sbName }}</view>
                        <view class="f-size-24 state-orange">{{
                            statusMap[item.status]
                        }}</view>
                    </view>
                    <view class="mt-2 d-flex j-between">
                        <view class="text-color-3 f-size-32 f-weight-bold">{{
                            item.actualUser
                        }}-{{
                            item.sbCph
                        }}</view>
                    </view>
                    <view class="f-size-26 text-color-6 f-weight-4 mt-2">{{
                        item.applyTime
                    }}</view>
                    <view class="d-flex j-end edit-box">
                        <view
                            class="text-color-3 f-size-32 edit"
                            v-if="
                                item.status ===
                                    $DictCache.VALUE.REPAIR_APPLICATION_FORM_STATUS.NOT_ALLOCATED"
                        >
                            <view @click.stop="handleDealEdit(item)">接收</view>
                        </view>
                        <view
                            class="text-color-3 f-size-32 edit"
                           v-if="!item.fourRepairUser && item.repairUserId"
                        >
                            <view  @click.stop="handleDispatching(item)">转派</view>
                        </view>
                        <view
                            class="text-color-3 f-size-32 edit"
                            v-if="
                                [
                                    $DictCache.VALUE.REPAIR_APPLICATION_FORM_STATUS.PROCESSING,
                                    $DictCache.VALUE.REPAIR_APPLICATION_FORM_STATUS.REBACK,
                                ].includes(item.status)
                            "
                        >
                            <view @click.stop="handleEdit(item)">维修</view>
                        </view>
                        <view
                            class="text-color-3 f-size-32 edit"
                            v-if="
                                item.status ===
                                    $DictCache.VALUE.REPAIR_APPLICATION_FORM_STATUS.WAIT_SUBMIT ||
                                    item.status ===
                                    $DictCache.VALUE.REPAIR_APPLICATION_FORM_STATUS.REBACK
                            "
                        >
                            <view @click.stop="handleExamine(item)">提交审核</view>
                        </view>
                    </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: 1,
            // 加载更多
            status: 'loadmore',
            page: 1,
            limit: 10,
            sbId: '',
            type: 1,
            filter: -1,
            loadMore: true,
            listData: [],
            statusMap: {},
            sourceMap: {},
            maskId: 0
        };
    },
    onPullDownRefresh() {
        // 请求结束取消刷新
        this.page = 1;
        this.loadMore = true;
        this.listData = [];
        this.initData();
        uni.stopPullDownRefresh();
    },
    onReachBottom() {
        if (this.loadMore) {
            this.page++;
            this.initData();
        }
    },
    onLoad(options) {
        this.sbId = options.sbId
        this.filter = options.filter
        this.statusMap = this.$DictCache.getLabelByValueMapByType(
            this.$DictCache.TYPE.REPAIR_APPLICATION_FORM_STATUS
        );
        this.sourceMap = this.$DictCache.getLabelByValueMapByType(
            this.$DictCache.TYPE.REPAIR_APPLICATION_FORM_SOURCE
        );
        this.list = [{ label: '全部', value: '' }].concat(
            this.$DictCache.getChildrenList(this.$DictCache.TYPE.REPAIR_APPLICATION_FORM_STATUS)
        );
    },
    onShow() {
        this.page = 1;
        this.loadMore = true;
        this.listData = [];
        this.initData();
    },
    onHide() {
        this.maskId = 0
    },
    methods: {
        showMask(id, e) {
            this.maskId = id
            // #ifdef H5
            e.preventDefault()
            // #endif
        },
        // 提交审批验收
        handleExamine(item) {
            var that = this;
            uni.showModal({
                title: '提示',
                content: '确认提交审核？',
                success: function (res) {
                    if (res.confirm) {
                        that.$Http
                            .examine({
                                id: item.id
                            })
                            .then((res) => {
                                uni.showToast({
                                    title: '已提交，请等待审核'
                                });
                                that.initData();
                            });
                    }
                }
            });
        },
        handleDel(id) {
            const that = this;
            const params = [];
            params.push(id);
            uni.showModal({
                title: '提示',
                content: '您确认删除吗?',
                success: (res) => {
                    if (res.confirm) {
                        this.$Http.deleteRepairForms(params).then((res) => {
                            uni.showToast({ title: '删除成功' });
                            this.page = 1;
                            this.loadMore = true;
                            this.listData = [];
                            this.initData();
                        });
                    } else if (res.cancel) {
                    }
                }
            });
        },
        handleDealEdit(item) {
            const that = this;
            uni.showModal({
                title: '提示',
                content: '确认接收该维修任务？',
                success: function (res) {
                    if (res.confirm) {
                        that.$Http.repairFormDeal(item).then((res) => {
                            that.page = 1;
                            that.loadMore = true;
                            that.listData = [];
                            that.initData();
                        });
                    }
                }
            });
        },
        handleDispatching(item) {
      
                        uni.navigateTo({
                            url: '/pages/repair-detail/transfer?id=' + item.id
                        });
             
        },
        handleEdit(item) {
            uni.navigateTo({
                url: '/pages/service-add/service-add?id=' + item.id
            });
        },
        // tab
        change(index) {
            this.current = index;
            // type 做相应改变
            this.type = index;
            this.page = 1;
            this.loadMore = true;
            this.listData = [];
            this.initData();
        },
        // 跳转详情
        handleDetail(item) {
            // if (index == 0) {
            // 	uni.navigateTo({
            // 			url: '../service-add/service-add'
            // 	});
            // } else {
            // 	uni.navigateTo({
            // 			url: '../service-detail/service-detail'
            // 	});
            // }
            if (this.maskId != 0) return this.maskId = 0
            uni.navigateTo({
                url: '/pages/service-detail/service-detail?id=' + item.id
            });
        },
        initData() {
            const that = this;
            that.status = 'loading';

            this.$Http
                .getRepairApplicationFormPage({
                    pageNum: this.page,
                    pageSize: this.limit,
                    sbId: this.sbId,
                    searchType: 2,
                    filter: this.filter,
                    status: this.type,
                    dataScope: {
            sortBy: 'asc, desc',
            sortName: 'status, apply_time'
          }
                })
                .then((res) => {
                    const data = res.data.rows;
                    console.log(res);
                    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="scss">
page {
  background-color: #f4f4f4;
}

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

.edit-box {
  padding: 0 30rpx;
  margin-top: 40rpx;

  .edit {
    padding: 8rpx 40rpx;
    border: 1px solid #ddd;
    border-radius: 20rpx;
    font-size: 26rpx;
    color: #666;
    margin-left: 30rpx;
  }
}
</style>
