<template>
    <view class="page-css">
        <u-navbar v-if="planOrderId" title="巡检任务" back-icon-color="rgb(0, 0, 0)" back-icon-size="54" :title-bold="true"
            :title-size="38" title-color="rgb(0, 0, 0)"></u-navbar>
        <view class="protocol-content">
            <u-tabs-swiper ref="tabs" :list="subsectionList" :current="subsectionCurrent" :is-scroll="false"
                @change="subsectionChange" v-if="dataTypes != 2"></u-tabs-swiper>

        </view>
        <view class="page-search-top">
            <betone-new-input v-model="searchInfo.requirementCode" placeholder="搜索巡检单单号" icon="search"
                style="margin-bottom: 32rpx" @changevalue="searchData()" />
            <u-tabs v-if="subsectionCurrent == '0'" :list="tabList" :is-scroll="true" :current="current"
                @change="changeTab"></u-tabs>
            <u-tabs v-if="subsectionCurrent == '1'" :list="tasktabList" :is-scroll="true" :current="current"
                @change="subsectionChangeTab"></u-tabs>
        </view>
        <view class="order-box">
            <view class="order-item" v-for="(item, index) in orderData" :key="index">
                <view class="item-top">
                    <view class="item-left">
                        <text class="item-orderNo">单号： {{ item.taskCode }}</text>
                    </view>
                    <view class="item-right">
                        <u-tag v-if="subsectionCurrent == '0'" :text="statusName(item.taskStatus)" type="success"
                            mode="dark" shape="circle" :bg-color="tagColor[statusName(item.taskStatus)]" />
                        <u-tag v-if="subsectionCurrent == '1'" :text="taskStatusName(item.repairStatus)" type="success"
                            mode="dark" shape="circle" :bg-color="tagColor[taskStatusName(item.repairStatus)]" />
                    </view>
                </view>
                <view class="item-con">
                    <view class="con-right">
                        <text class="con-title">单位：{{ item.deptName }}</text>
                        <text class="con-title">巡检说明：{{ item.taskContent }}</text>
                        <text class="con-title">计划开始时间：{{ item.startTime }}</text>
                        <!-- <text class="con-title">计划开始时间：{{ item.startTime }}</text> -->
                        <view class="con-btn">
                            <u-button size="mini" plain style="margin-right: 32rpx"
                                @click="taskDetails(item)">查看</u-button>

                            <!-- 巡检任务  客户 确认-->
                            <block v-if="subsectionCurrent == '0' && dataTypes == 1 && item.taskStatus == 3">
                                <u-button size="mini" plain style="margin-right: 32rpx"
                                    @click.stop="openWorkOrder(item, 'tsak')">确认</u-button>
                            </block>
                            <!--巡检任务 维修人员    接单  提交-->
                            <block v-if="subsectionCurrent == '0' && dataTypes == 4">
                                <u-button v-if="item.taskStatus == 1" size="mini" plain style="margin-right: 32rpx"
                                    @click="openWorkOrder(item, 'tsak')">接单</u-button>
                                <u-button v-if="item.taskStatus == 2" size="mini" plain style="margin-right: 32rpx"
                                    @click="openWorkOrder(item, 'tsak')">巡检</u-button>
                            </block>
                            <!--巡检任务 维修主管  指派 -->
                            <block v-if="subsectionCurrent == '0' && dataTypes == 3">
                                <u-button v-if="item.taskStatus == 0" size="mini" plain style="margin-right: 32rpx"
                                    @click="assign(item)">指派</u-button>
                            </block>



                            <!--巡检维修 客户 -->
                            <block v-if="subsectionCurrent == '1' && dataTypes == 1">
                                <u-button v-if="item.repairStatus == 3" size="mini" plain style="margin-right: 32rpx"
                                    @click="openWorkOrder(item, 'repair')">审批</u-button>
                                <u-button v-if="item.repairStatus == 5" size="mini" plain style="margin-right: 32rpx"
                                    @click="openEvaluate(item)">评价</u-button>
                            </block>
                            <!--巡检维修 客服 -->
                            <block v-if="subsectionCurrent == '1' && dataTypes == 2">
                                <u-button v-if="item.repairStatus == 6" size="mini" plain style="margin-right: 32rpx"
                                    @click.stop="openModel(item, 'paymentInfo')">确认付款</u-button>
                            </block>
                            <!--巡检维修 维修主管 -->
                            <block v-if="subsectionCurrent == '1' && dataTypes == 3">
                                <u-button v-if="item.repairStatus == 2" size="mini" plain style="margin-right: 32rpx"
                                    @click="openWorkOrder(item, 'repair')">审批</u-button>
                            </block>
                            <!--巡检维修 维修人员 -->
                            <block v-if="subsectionCurrent == '1' && dataTypes == 4">
                                <u-button v-if="item.repairStatus == 1" size="mini" plain style="margin-right: 32rpx"
                                    @click="openWorkOrder(item, 'repair')">提交</u-button>
                                <u-button v-if="item.repairStatus == 4" size="mini" plain style="margin-right: 32rpx"
                                    @click="openModel(item, 'repairComplete')">维修完成</u-button>
                            </block>

                        </view>
                    </view>
                </view>
            </view>
        </view>
        <u-loadmore margin-top="10" margin-bottom="40" :status="loadingState" />


        <!-- 分派人员-->
        <u-select v-model="sendOrdersShow" :list="sendOrdersList" @confirm="sendOrdersConfirm" :isShowsearch="true"
            value-name="id" title="选择维修人员"></u-select>

        <!-- 维修人员 确认维修是否完成 -->
        <betone-modal v-model="repairCompleteShow" :title="'提示'" @confirm="repairCompleteFn"
            @cancel="repairCompleteShow = false">
            维修是否完成？
        </betone-modal>

        <!-- 客服确认付款 -->
        <betone-modal v-model="paymentShow" :title="'提示'" @confirm="paymentFn" @cancel="paymentShow = false">
            <view class="modal-text">
                请确认付款是否已经完成？
            </view>
        </betone-modal>

        <betone-loading ref="BetLoading" />
        <betone-tabbar ref="tabbarRef" v-if="!planOrderId" />


    </view>
</template>

<script>
export default {

    data() {
        return {
            type: '',//展示类型
            searchInfo: {
                requirementCode: "",
                type: '',//状态
                reportUnit: '',//提报单位
                statusSearch: '',//巡检任务状态
            },
            userInfo: {},//用户信息
            rowInfo: {},//选中行数据
            tagColor: {
                待巡检: "#67c23a",
                巡检中: "#e6a23",
                待确认: '#67c23a',
                待派单: "#409eff",
                待接单: "#67c23a",
                已完成: "#CECECE",
                待提交: "#67c23a",
                待审批 :"#409eff",
                维修中:"#e6a23",
                待确认:"#67c23a",
                待评价:"#67c23a",
                维修完成:"#67c23a",
                待付款:"#67c23a",
            },
            tabList: [],
            tasktabList: [],
            current: 0,
            loadingState: "loadmore",
            isNomore: false,
            noDataFlag: false,

            orderData: [],
            page: {
                currentPage: 1,
                pageSize: 10,
            },
            subsectionList: [
                {
                    name: '巡检任务'
                },
                {
                    name: '巡检维修'
                },

            ],
            subsectionCurrent: '0',//
            planOrderId: '',//巡检计划id

            sendOrdersShow: false,//派单弹框
            sendOrdersList: [],
            rowInfo: {},//获取列表数据

            repairCompleteShow: false,//维修完成  
            paymentShow: false,//确认付款是否完成
        };
    },
    computed: {
        noData() {
            return this.orderData && this.orderData.length == 0;
        },
        dataTypes() {
            return this.$store.state.dataType
        },
    },
    onLoad(options) {
        if (options.id) {
            this.planOrderId = options.id
        }
        if (options.type) {
            this.type = options.type
        }

    },
    onShow() {
        this.$nextTick(() => {
            this.statusList()
            this.getTasktabList()
            this.getRepairPersons()
            if (this.dataTypes == 2) {
                this.subsectionCurrent = '1'
            }
            this.getRecordsList()

            // 实验室巡检  跳转进来
            if (!this.planOrderId) {
                this.$refs.tabbarRef.getPermission();
            }

        });
    },
    onReachBottom() {
        if (this.isNomore) {
            return;
        }
        this.page.currentPage++;
        this.getRecordsList();
    },
    onPullDownRefresh() {
        this.isNomore = false
        this.page.currentPage = 1;
        this.getRecordsList();
        uni.stopPullDownRefresh();
    },

    methods: {
        // 获取列表
        async getRecordsList() {
            this.BetLoading.show();
            this.loadingState = "loading";
            let query = {
                current: this.page.currentPage,				          //页数
                size: this.page.pageSize,				            //条数             
            }
            if (this.planOrderId) {
                query.planId = this.planOrderId
            }
            let res = {}
            if (this.subsectionCurrent == '0') {
                query.statusSearch = this.searchInfo.statusSearch
                res = await this.$u.api.getTaskPage(query)
            } else {
                query.statusSearch = this.searchInfo.statusSearch
                res = await this.$u.api.getRepairPage(query)
            }
            if (res.code == 200) {
                var totalPage = res.data.total;
                var newOrders = res.data.records;
                if (this.page.currentPage == 1) {
                    this.orderData = [];
                }

                this.orderData = [...this.orderData, ...(res.data.records || [])];
                if (newOrders.length == 0) {
                    this.loadingState = "nomore";
                    this.isNomore = true;
                }
                if (totalPage == this.orderData.length) {
                    this.loadingState = "nomore";
                    this.isNomore = true;
                }
                this.BetLoading.hide();
            }

        },
        // 搜索 单号
        searchData() {
            if (this.dataTypes != 1) {
                // 非管理员和客户    不传提报单位
                this.searchInfo.reportUnit = this.searchInfo.requirementCode
            } else {
                this.searchInfo.reportUnit = ''
            }
            this.isNomore = false
            this.page.currentPage = 1;
            this.getRecordsList()
        },
        // 巡检任务 tab切换
        changeTab(index) {
            this.current = index;
            this.searchInfo.statusSearch = this.tabList[index].key == '9999' ? '' : this.tabList[index].key
            this.isNomore = false
            this.page.currentPage = 1;
            this.getRecordsList()
        },
        // 巡检维修  tab切换
        subsectionChangeTab(index) {
            this.current = index;
            this.searchInfo.statusSearch = this.tasktabList[index].key == '9999' ? '' : this.tasktabList[index].key
            this.isNomore = false
            this.page.currentPage = 1;
            this.getRecordsList()
        },

        // 查看详情  
        taskDetails(item) {
            let param = {
                id: item.id,
                type: 'view'
            }
            uni.navigateTo({
                url: `/pages/inspection/questDetails${this.$u.queryParams(param)}`,
            });
        },
        //跳转工单
        openWorkOrder(item, type) {
          
            let param = {
                id: item.id,
                type: 'view',
                details: type
            }
            uni.navigateTo({
                url: `/pages/inspection/questOrder${this.$u.queryParams(param)}`,
            });
        },
        subsectionChange(e) {
            this.subsectionCurrent = e;
            this.searchInfo.statusSearch = ''
            this.getRecordsList()
        },
        // 指派
        assign(item) {
            this.rowInfo = item
            this.sendOrdersShow = true
        },
        // 维修人员
        getRepairPersons() {
            this.$u.api.getRepairPersons().then(res => {
                if (res.code == 200) {
                    let data_ = res.data
                    data_.forEach(i => {
                        this.sendOrdersList.push({ ...i, label: i.name })
                    })
                }
            })
        },
        // 确认指派人员
        sendOrdersConfirm(e) {
            let query = {
                servicemanId: e[0].value,
                id: this.rowInfo.id
            }
            this.$u.api.taskSupervisorconfirm(query).then(res => {
                if (res.code == 200) {
                    uni.showToast({ title: "指派成功", icon: "none" });
                    this.sendOrdersShow = false
                    // 指派成功   刷新列表
                    this.getRecordsList()
                }
            })
        },
        openModel(item, type) {
            if (type == 'repairComplete') {
                this.repairCompleteShow = true
            }
            if (type == 'paymentInfo') {
                this.paymentShow = true
            }
            this.rowInfo = item
        },
        // 维修完成 弹框
        repairCompleteFn() {
            let query = {
                id: this.rowInfo.id
            }
            this.$u.api.repairServicemanSubmit(query).then(res => {
                if (res.code == 200) {
                    uni.showToast({ title: "维修完成", icon: "none" });
                    this.repairCompleteShow = false
                    // 指派成功   刷新列表
                    this.getRecordsList()
                }
            })
        },
        // 确认付款是否完成
        paymentFn() {
            let query = {
                id: this.rowInfo.id,
            }
            this.BetLoading.show();

            this.$u.api.confirmPayment(query).then(res => {
                this.paymentShow = false
                this.BetLoading.hide();
                if (res.code == 200) {
                    uni.showToast({ title: "确认成功", icon: "none" });
                    this.getRecordsList()
                }
            })
        },
        // 评价
        openEvaluate(item) {
            let param = {
                id: item.id,
                istask: true
            }
            uni.navigateTo({
                url: `/pages/submission/evaluate${this.$u.queryParams(param)}`,
            });
        },

        // 获取状态信息
        statusName(item) {
            if ((this.dataTypes == 1 && (item == 0 || item == 1)) || (this.dataTypes == 3 && item == 1)) {
                return '待巡检'
            }
            if ((this.dataTypes == 1 && item == 2) || (this.dataTypes == 3 && (item == 2 || item == 3)) || (this.dataTypes == 4 && item == 2)) {
                return '巡检中'
            }
            if ((this.dataTypes == 1 && item == 3) || (this.dataTypes == 4 && item == 3)) {
                return '待确认'
            }
            if ((this.dataTypes == 3 && item == 0) || (this.dataTypes == 4 && item == 0)) {
                return '待派单'
            }

            if (this.dataTypes == 4 && item == 1) {
                return '待接单'
            }

            if (item == 4) {
                return '已完成'
            }
        },
        taskStatusName(item) {
            if (item == 1) {
                return '待提交'
            }
            if (item == 7 || item == 8 || (this.dataTypes == 4 && item == 6)) {
                return '已完成'
            }
            if (item == 2) {
                return '待审批'
            }
            if (item == 4) {
                return '维修中'
            }
            if (((this.dataTypes == 4 || this.dataTypes == 2 || this.dataTypes == 1) && item == 3)) {
                return '待审批'
            }
            if (item == 3 && this.dataTypes == 3) {
                return '待确认'
            }
            if ((item == 5 && this.dataTypes == 1) || (item == 5 && this.dataTypes == 2)) {
                return '待评价'
            }
            if ((item == 5 && this.dataTypes == 3) || (item == 5 && this.dataTypes == 4)) {
                return '维修完成'
            }
            if ((item == 6 && this.dataTypes == 3) || (item == 6 && this.dataTypes == 2) || (item == 6 && this.dataTypes == 1)) {
                return '待付款'
            }
            if (item == 7||item == 8) {
                return '已完成'
            }
        },
        // 根据角色获取tablist
        statusList() {
            // 巡检计划
            if (this.dataTypes == 1) {
                this.tabList = [
                    {
                        name: "全部",
                        key: "9999",
                    },
                    {
                        name: "待巡检",
                        key: "0,1",
                    },
                    {
                        name: "巡检中",
                        key: "2",
                    },
                    {
                        name: "待确认",
                        key: "3",
                    },
                    {
                        name: "已完成",
                        key: "4",
                    },
                ]
            }
            if (this.dataTypes == 3) {//主管
                this.tabList = [
                    {
                        name: "全部",
                        key: "9999",
                    },
                    {
                        name: "待指派",
                        key: "0",
                    },
                    {
                        name: "待巡检",
                        key: "1",
                    },
                    {
                        name: "巡检中",
                        key: "2,3",
                    },
                    {
                        name: "已完成",
                        key: "4",
                    },
                ]
            }
            if (this.dataTypes == 4) {
                this.tabList = [
                    {
                        name: "全部",
                        key: "9999",
                    },

                    {
                        name: "待接单",
                        key: "1",
                    },
                    {
                        name: "巡检中",
                        key: "2",
                    },
                    {
                        name: "待确认",
                        key: "3",
                    },
                    {
                        name: "已完成",
                        key: "4",
                    },
                ]
            }
        },
        // 修改巡检维修列表的tab列表
        getTasktabList() {
            if (this.dataTypes == 1 || this.dataTypes == 2) {
                this.tasktabList = [
                    {
                        name: "全部",
                        key: "9999",
                    },
                    // {
                    //     name: "不需要维修",
                    //     key: "0",
                    // },
                    {
                        name: "待提交",
                        key: "1",
                    },
                    {
                        name: "待审批",
                        key: "2,3",
                    },
                    {
                        name: "维修中",
                        key: "4",
                    },
                    {
                        name: "待评价",
                        key: "5",
                    },
                    {
                        name: "待付款",
                        key: "6",
                    },
                    {
                        name: "已完成",
                        key: "7,8",
                    },
                ]
            }
            if (this.dataTypes == 3) {
                this.tasktabList = [
                    {
                        name: "全部",
                        key: "9999",
                    },
                    // {
                    //     name: "不需要维修",
                    //     key: "0",
                    // },
                    {
                        name: "待提交",
                        key: "1",
                    },
                    {
                        name: "待审批",
                        key: "2",
                    },
                    {
                        name: "待确认",
                        key: "3",
                    },
                    {
                        name: "维修中",
                        key: "4",
                    },
                    {
                        name: "维修完成",
                        key: "5",
                    },
                    {
                        name: "待付款",
                        key: "6",
                    },
                    {
                        name: "已完成",
                        key: "7,8",
                    },
                ]
            }
            if (this.dataTypes == 4) {
                this.tasktabList = [
                    {
                        name: "全部",
                        key: "9999",
                    },
                    // {
                    //     name: "不需要维修",
                    //     key: "0",
                    // },
                    {
                        name: "待提交",
                        key: "1",
                    },
                    {
                        name: "待审批",
                        key: "2,3",
                    },

                    {
                        name: "维修中",
                        key: "4",
                    },
                    {
                        name: "维修完成",
                        key: "5",
                    },
                    {
                        name: "待付款",
                        key: "6",
                    },
                    {
                        name: "已完成",
                        key: "7,8",
                    },
                ]
            }

        },

    },
};
</script>

<style lang="scss" scoped>
.page-css {
    padding: 0;

    .page-search-top {
        height: 220rpx;
        background-color: #fff;
        padding: 0 40rpx 0;
    }

    .protocol-content {
        padding: 40rpx 0rpx 0rpx 0rpx;
        background-color: #fff;
    }

    .order-box {
        margin-top: 20rpx;

        .order-item {
            min-height: 414rpx;
            margin: 0 24rpx 20rpx;
            border-radius: 20rpx;
            background: #fff;
            display: flex;
            flex-direction: column;

            .item-top {
                height: 114rpx;
                border-bottom: 1px solid rgba(239, 239, 239, 1);
                display: flex;
                flex-direction: row;
                width: 100%;

                .item-left {
                    flex: 1;

                    text {
                        display: block;
                    }

                    .item-orderNo {
                        color: rgba(51, 51, 51, 1);
                        font-size: 28rpx;
                        padding: 36rpx 0 0 26rpx;
                    }

                    .item-time {
                        color: rgba(190, 190, 190, 1);
                        font-size: 24rpx;
                        padding: 12rpx 0 0 26rpx;
                    }
                }

                .item-right {
                    min-width: 132rpx;
                    height: 48rpx;
                    margin: 36rpx 24rpx 0 0;
                }
            }

            .item-con {
                flex: 1;
                display: flex;
                flex-direction: row;

                .con-left {
                    width: 150rpx;
                    height: 150rpx;
                    margin: 24rpx 28rpx 0 28rpx;

                    image {
                        width: 100%;
                        height: 100%;
                    }
                }

                .con-right {
                    flex: 1;
                    margin-right: 24rpx;
                    display: flex;
                    flex-direction: column;
                    margin-left: 32rpx;

                    text {
                        display: block;
                    }

                    .con-title {
                        margin-top: 12rpx;
                        line-height: 60rpx;
                        color: rgba(0, 0, 0, 1);
                        font-size: 28rpx;
                    }

                    .con-content {

                        color: rgba(108, 108, 108, 1);
                        font-size: 24rpx;
                        // line-height: 36rpx;
                        margin-top: 14rpx;
                        flex: 1;

                        text {
                            display: -webkit-box;
                            -webkit-box-orient: vertical;
                            -webkit-line-clamp: 2;
                            overflow: hidden;
                            text-overflow: ellipsis;
                            line-height: 1.5em;
                            /* 这个值根据你的需求调整，这里假设为1.5倍行高 */
                            max-height: 3em;
                            /* 3行的高度 */
                            width: 100%;
                            /* 根据需要设置宽度 */
                            word-wrap: break-word;
                            /* 允许在单词内换行 */
                            word-break: break-all;
                            /* 允许在任意字符处换行 */
                        }
                    }

                    .con-btn {
                        margin-top: 18rpx;
                        margin-bottom: 32rpx;
                        display: inline-block;
                        text-align: right;
                    }
                }
            }
        }
    }
}
</style>