<template>
    <div class="container">
        <!--<div class="search-input-wrap">-->
        <!--<md-input-item-->
        <!--class="search-input"-->
        <!--placeholder="请输入申请名称..."-->
        <!--&gt;-->
        <!--<md-icon name="search" slot="left"></md-icon>-->
        <!--</md-input-item>-->
        <!--</div>-->

        <md-tab-bar
                v-model="current"
                :items="items"
                @change="onTabChange"
        >
            <template slot="item" slot-scope="{ item }">
                <div class="tab-bar-item">
                    <md-tag
                            size="large"
                            shape="bubble"
                            fill-color="linear-gradient(90deg, #FF5B60 0%, #F98472 100%)"
                            type="fill"
                            v-show="item.isShowTag && item.notifications !== 0"
                    >{{item.notifications}}
                    </md-tag>
                    <div class="text">
                        <span v-text="item.label"></span>
                    </div>
                </div>
            </template>
        </md-tab-bar>

        <md-swiper
                ref="swiper"
                :autoplay="0"
                :is-prevent="false"
                :is-loop="false"
                :has-dots="false"
                @before-change="onSwiperChange"
        >
            <md-swiper-item>
                <md-scroll-view
                        ref="typeFormList"
                        :scrolling-x="false"
                        :bouncing="false"
                        :auto-reflow="true"
                        @end-reached="$_onEndReached(0)"
                >
                    <template v-if="typeFormList.length > 0">
                        <div
                                v-for="(typeFormItem,index) in typeFormList"
                                :key="index"
                                class="approval-type"
                                @click="navToAddJobTicketPage(typeFormItem.id, 1)"
                        >
                            <div class="left">
                                <img class="type-img"
                                     :src="typeFormItem.formIcon"
                                     alt="">
                            </div>
                            <div class="center">
                                <p class="title">{{typeFormItem.formName}}</p>
                                <p class="info">{{typeFormItem.formInfo}}</p>
                            </div>
                            <div class="right">
                                <md-button class="btn">申请</md-button>
                            </div>
                        </div>
                        <md-scroll-view-more
                                slot="more"
                                :is-finished="typeFormListQuery.isFinished"
                        >
                        </md-scroll-view-more>
                    </template>
                    <template v-else>
                        <empty-data-page></empty-data-page>
                    </template>
                </md-scroll-view>
            </md-swiper-item>
            <md-swiper-item>
                <md-scroll-view
                        ref="applyList"
                        :scrolling-x="false"
                        :bouncing="false"
                        :auto-reflow="true"
                        @end-reached="$_onEndReached(1)">
                    <template v-if="applyList.length > 0">
                        <div
                                v-for="(applyItem,index) in applyList"
                                :key="index"
                                class="task-info"
                                @click="applyItem.taskName === '调整申请' ? navToAddJobTicketPage(applyItem.id,2) : navToJobTicketDetailPage(applyItem.id,true,applyItem.formName)">
                            <div class="task-info-content">
                                <div class="head">
                                    <p class="title">{{applyItem.formName}}</p>
                                    <div class="tag tag-green"
                                         v-if="applyItem.status == 1 && applyItem.taskName !== '调整申请'">
                                        审批中
                                    </div>

                                    <div class="tag tag-red"
                                         v-if="applyItem.status == 1 && applyItem.taskName === '调整申请'">
                                        被驳回
                                    </div>

                                    <div class="tag tag-gray" v-if="applyItem.status == 2">
                                        已完成
                                    </div>

                                    <div class="tag tag-red" v-if="applyItem.status == 0">
                                        未开始
                                    </div>
                                </div>
                                <!--<div class="content">-->
                                <!--<p>{{applyItem.json}}</p>-->
                                <!--</div>-->
                                <div class="info">
                                    <div class="left">
                                        <p>当前流程：{{applyItem.taskName}}</p>
                                        <p>发起人：{{applyItem.userName}}</p>
                                        <p>发起时间：{{applyItem.createdTime}}</p>
                                    </div>
                                    <!--<div class="right">-->
                                    <!--<template v-if="applyItem.taskName === '调整申请'">-->
                                    <!--<md-button class="btn" @click="navToAddJobTicketPage(applyItem.id,2)">修改申请</md-button>-->
                                    <!--</template>-->
                                    <!--<template v-else>-->
                                    <!--<md-button class="btn" @click="navToJobTicketDetailPage(applyItem.id,true)">查看</md-button>-->
                                    <!--</template>-->
                                    <!--</div>-->
                                </div>
                            </div>
                        </div>
                        <md-scroll-view-more
                                slot="more"
                                :is-finished="applyListQuery.isFinished"
                        >
                        </md-scroll-view-more>
                    </template>
                    <template v-else>
                        <empty-data-page></empty-data-page>
                    </template>
                </md-scroll-view>
            </md-swiper-item>
            <md-swiper-item>
                <md-scroll-view
                        ref="approvalList"
                        :scrolling-x="false"
                        :bouncing="false"
                        :auto-reflow="true"
                        @end-reached="$_onEndReached(2)">
                    <template v-if="approvalList.length > 0">
                        <div
                                v-for="(approvalItem,index) in approvalList"
                                :key="index"
                                class="task-info"
                                @click="navToJobTicketDetailPage(approvalItem.id,false,approvalItem.formName)">
                            <div class="task-info-content">
                                <div class="head">
                                    <p class="title">{{approvalItem.formName}}</p>
                                </div>
                                <!--<div class="content">-->
                                <!--<p>家里有事，我需要请几天假，望领导审批。我将准时回 公司。手头事已交接完毕。家里有事，我需要请几天假，望领导审批。我将准时回 公司。手头事已交接完毕。</p>-->
                                <!--</div>-->
                                <div class="info">
                                    <div class="left">
                                        <p>当前流程：{{approvalItem.taskName}}</p>
                                        <p>发起人：{{approvalItem.userName}}</p>
                                        <p>发起时间：{{approvalItem.createdTime}}</p>
                                    </div>
                                    <!--<div class="right">-->
                                    <!--<md-button class="btn" @click="navToJobTicketDetailPage(approvalItem.id)">审批</md-button>-->
                                    <!--</div>-->
                                </div>
                            </div>
                        </div>
                        <md-scroll-view-more
                                slot="more"
                                :is-finished="approvalListQuery.isFinished"
                        >
                        </md-scroll-view-more>
                    </template>
                    <template v-else>
                        <empty-data-page></empty-data-page>
                    </template>
                </md-scroll-view>
            </md-swiper-item>
            <md-swiper-item>
                <md-scroll-view
                        ref="approvedList"
                        :scrolling-x="false"
                        :bouncing="false"
                        :auto-reflow="true"
                        @end-reached="$_onEndReached(3)">
                    <template v-if="approvedList.length > 0">
                        <div
                                v-for="(approvedItem,index) in approvedList"
                                :key="index"
                                class="task-info"
                        >
                            <!--@click="navToJobTicketDetailPage(approvedItem.formId,true)">-->
                            <div class="task-info-content">
                                <div class="head">
                                    <p class="title">{{approvedItem.taskName}}</p>
                                </div>
                                <!--<div class="content">-->
                                <!--<p>家里有事，我需要请几天假，望领导审批。我将准时回 公司。手头事已交接完毕。家里有事，我需要请几天假，望领导审批。我将准时回 公司。手头事已交接完毕。</p>-->
                                <!--</div>-->
                                <div class="info">
                                    <div class="left">
                                        <p>处理人：{{approvedItem.handleUserName}}</p>
                                        <p>审批意见：{{approvedItem.taskMessage}}</p>
                                        <p>处理时间：{{approvedItem.handleTime}}</p>
                                    </div>
                                    <!--<div class="right">-->
                                    <!--<md-button class="btn" @click="navToJobTicketDetailPage(approvedItem.formId,true)">查看</md-button>-->
                                    <!--</div>-->
                                </div>
                            </div>
                        </div>
                        <md-scroll-view-more
                                slot="more"
                                :is-finished="approvedListQuery.isFinished"
                        >
                        </md-scroll-view-more>
                    </template>
                    <template v-else>
                        <empty-data-page></empty-data-page>
                    </template>
                </md-scroll-view>
            </md-swiper-item>
        </md-swiper>
    </div>
</template>

<script>
    import {
        TabBar,
        Swiper,
        SwiperItem,
        ScrollView,
        ScrollViewRefresh,
        ScrollViewMore,
        Button,
        InputItem,
        Icon,
        Tag,
        Toast,
    } from 'mand-mobile'

    import {HeadBar, EmptyDataPage} from '@/components'

    import {getTypeFormList, getApplyList, getApprovedList} from '@/api/job_ticket'

    import {mapGetters} from 'vuex'

    var _$store;

    export default {
        name: "index",
        components: {
            [TabBar.name]: TabBar,
            [Swiper.name]: Swiper,
            [SwiperItem.name]: SwiperItem,
            [ScrollView.name]: ScrollView,
            [ScrollViewRefresh.name]: ScrollViewRefresh,
            [ScrollViewMore.name]: ScrollViewMore,
            [Button.name]: Button,
            [InputItem.name]: InputItem,
            [Icon.name]: Icon,
            [Tag.name]: Tag,
            [HeadBar.name]: HeadBar,
            [EmptyDataPage.name]: EmptyDataPage,
        },
        data() {
            return {
                current: 0,
                items: [
                    {name: 0, label: '作业申请', notifications: 0, isShowTag: true},
                    {name: 1, label: '我发起的', notifications: 0, isShowTag: true},
                    {name: 2, label: '待审批的', notifications: 0, isShowTag: true},
                    {name: 3, label: '已审批的', notifications: 0, isShowTag: true},
                ],
                list: 10,
                isFinished: false,

                typeFormList: [],   // 表单列表
                typeFormListQuery: {
                    page: 0,
                    limit: 10,
                    isFinished: false,
                },
                applyList: [], // 申请列表
                applyListQuery: {
                    page: 0,
                    limit: 10,
                    isFinished: false,
                },
                approvalList: [], // 审批列表
                approvalListQuery: {
                    page: 0,
                    limit: 10,
                    isFinished: false,
                },
                approvedList: [], // 已审批列表
                approvedListQuery: {
                    page: 0,
                    limit: 10,
                    isFinished: false,
                },
            }
        },
        mounted() {
            _$store = this.$store;
            if (this.job_ticket_tabBarIndex !== null) {
                this.reloadTabBarIndex(this.job_ticket_tabBarIndex)
            }
            if (this.$route.query.fromUrl) {
                this.$store.dispatch('request_config/setBaseUrl', this.$route.query.fromUrl)
            } else {
                this.$store.dispatch('request_config/setBaseUrl', location.host)
            }
            // 所以接口传 aId=294，cId=1
            if (this.$route.query.aId && this.$route.query.cId) {
                this.$store.dispatch('job_ticket/setReqConfig', {
                    // aId: 294,
                    // aId: 1352,
                    // aId: 48160,
                    // cId: 1
                    aId: this.$route.query.aId,
                    cId: this.$route.query.cId
                })
                this.$_onEndReached(0)
            } else {
                this.$store.dispatch('job_ticket/setReqConfig', {
                    aId: '',
                    cId: ''
                })
            }

        },
        watch: {
            current: (v, ov) => {
                _$store.dispatch('job_ticket/updateTabBarIndex', v)
            },
        },
        computed: {
            ...mapGetters([
                'job_ticket_aId',
                'job_ticket_cId',
                'job_ticket_tabBarIndex'
            ])
        },
        methods: {
            /**
             * @Author: NanKeYang
             * @Description: 重新加载标签下标
             * @Data: 2020/1/9 10:22
             */
            reloadTabBarIndex(index) {
                this.current = index
                this.$nextTick(() => {
                    this.$refs.swiper.goto(index)
                })
            },
            /**
             * @Author: NanKeYang
             * @Description: 工作表详情页面
             * @Data: 2019/12/28 11:32
             */
            navToJobTicketDetailPage(id, isLoad = false, formName = '') {
                this.$router.push({
                    path: '/job_ticket_detail',
                    query: {
                        id: id,
                        isLog: isLoad,
                        formName: formName
                    }
                })
            },
            /**
             * @Author: NanKeYang
             * @Description: 申请工作表页面
             * @Data: 2019/12/28 11:32
             * @param type: 1 => 新增，2=> 修改
             * @param id type 1=> 的时候为formID，2的时候为记录ID
             */
            navToAddJobTicketPage(id, type) {
                this.$router.push({
                    path: '/add_job_ticket',
                    query: {
                        id: id,
                        type: type
                    }
                })
            },
            /**
             * @Author: NanKeYang
             * @Description: 滑动区域切换
             * @Data: 2020/1/9 10:21
             */
            onSwiperChange(from, to) {
                this.current = to
            },
            /**
             * @Author: NanKeYang
             * @Description: 标签切换
             * @Data: 2020/1/9 10:21
             */
            onTabChange(item, index) {
                this.$refs.swiper.goto(index)
            },
            /**
             * @Author: NanKeYang
             * @Description:
             * @param type 获取数据 0=>作业审批 1=>我发起 2=>待审批 3=>已审批
             * @param ref 引用下标
             * @Data: 2019/12/30 10:24
             */
            async $_onEndReached(type) {
                let ref = '';
                let flag = false;
                Toast.loading('加载中...')
                switch (type) {
                    case 0:
                        if (!this.typeFormListQuery.isFinished) {
                            this.typeFormListQuery.page += 1;
                            this.typeFormListQuery.aId = this.job_ticket_aId;
                            this.typeFormListQuery.cId = this.job_ticket_cId;
                            this.typeFormListQuery.formType = 1;
                            await getTypeFormList(this.typeFormListQuery).then(res => {
                                const {code, data, count, limit, page} = res;
                                if (code === 200) {
                                    const lastPage = Math.ceil(count / limit);
                                    if (page >= lastPage) {
                                        this.typeFormListQuery.isFinished = true
                                    }
                                    this.typeFormList = this.typeFormList.concat(data);
                                    this.items[0].notifications = count;
                                }
                            }).catch(err => {
                            })
                        } else {
                            flag = true;
                        }
                        ref = 'typeFormList';
                        break;
                    case 1:
                        if (!this.applyListQuery.isFinished) {
                            this.applyListQuery.page += 1;
                            this.applyListQuery.aId = this.job_ticket_aId;
                            this.applyListQuery.cId = this.job_ticket_cId;
                            await getApplyList(this.applyListQuery).then(res => {
                                const {code, data, count, limit, page} = res;
                                if (code === 200) {
                                    const lastPage = Math.ceil(count / limit);
                                    if (page >= lastPage) {
                                        this.applyListQuery.isFinished = true
                                    }
                                    this.applyList = this.applyList.concat(data);
                                    this.items[1].notifications = count;
                                }
                            }).catch(err => {
                            })
                        } else {
                            flag = true;
                        }
                        ref = 'applyList';
                        break
                    case 2:
                        if (!this.approvalListQuery.isFinished) {
                            this.approvalListQuery.page += 1;
                            this.approvalListQuery.aId = this.job_ticket_aId;
                            this.approvalListQuery.cId = this.job_ticket_cId;
                            this.approvalListQuery.handleUserId = this.job_ticket_aId;
                            this.approvalListQuery.status = 1;
                            this.approvalListQuery.type = 1;
                            await getApplyList(this.approvalListQuery).then(res => {
                                const {code, data, count, limit, page} = res;
                                if (code === 200) {
                                    const lastPage = Math.ceil(count / limit);
                                    if (page >= lastPage) {
                                        this.approvalListQuery.isFinished = true
                                    }
                                    this.approvalList = this.approvalList.concat(data);
                                    this.items[2].notifications = count;
                                }
                            }).catch(err => {
                            })
                        } else {
                            flag = true;
                        }
                        ref = 'approvalList';
                        break;
                    case 3:
                        if (!this.approvedListQuery.isFinished) {
                            this.approvedListQuery.page += 1;
                            this.approvedListQuery.aId = this.job_ticket_aId;
                            this.approvedListQuery.cId = this.job_ticket_cId;
                            await getApprovedList(this.approvedListQuery).then(res => {
                                const {code, data, count, limit, page} = res;
                                if (code === 200) {
                                    const lastPage = Math.ceil(count / limit);
                                    if (page >= lastPage) {
                                        this.approvedListQuery.isFinished = true
                                    }
                                    this.approvedList = this.approvedList.concat(data);
                                    this.items[3].notifications = count;
                                }
                            }).catch(err => {
                            })
                        } else {
                            flag = true;
                        }
                        ref = 'approvedList';
                        break;
                }
                setTimeout(function () {
                    Toast.hide()
                }, 300)
                if (ref && !flag) {
                    this.$refs[ref].finishLoadMore()
                }
            },
        }
    }
</script>

<style lang="scss" scoped>
    .container {
        height: 100vh;
        background: #F2F2F2;

        /*.search-input-wrap {*/
        /*padding: 15px;*/
        /*.search-input {*/
        /*border-radius: 10px;*/
        /*background: #FFF;*/
        /*padding: 0 15px;*/
        /*}*/
        /*}*/

        /*.md-swiper {*/
        /*height: calc(100vh - 230px);*/
        /*font-size: 14px;*/
        /*}*/

        .md-swiper {
            height: calc(100vh - 100px);
            font-size: 14px;
        }

        .md-scroll-view {
            height: 100%;
        }

        .md-tab-bar {
            padding: 0;
            .tab-bar-item {
                position: relative;
                .md-tag {
                    position: absolute;
                    top: -35px;
                    right: -35px;
                }
            }

        }

        // 审批类型
        .approval-type {
            overflow: hidden;
            padding: 15px;
            background: #FFF;
            margin-bottom: 20px;
            .left, .center, .right {
                float: left;
                height: 150px;
            }

            .left {
                .type-img {
                    width: 100px;
                    height: 100px;
                    border-radius: 50%;
                    position: relative;
                    top: 50%;
                    transform: translateY(-50%);
                }
            }

            .center {
                width: calc(500px - 30px);
                margin: 0 15px;
                .title {
                    color: #222222;
                    font-size: 36px;
                    font-weight: bold;
                    margin: 15px 0;
                }
                .info {
                    font-size: 28px;
                    color: #999999;
                    height: 70px;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    line-height: 1.4em;
                }
            }

            .right {
                .btn {
                    background: #15CD85;
                    box-shadow: 3px 3px 10px #0ABC88;
                    color: #FFF;
                    padding: 0;
                    height: 50px;
                    line-height: 50px;
                    margin: 0 auto;
                    display: block;
                    align-items: center;
                    position: relative;
                    top: 50%;
                    transform: translateY(-50%);
                    padding: 0 10px;
                    border-radius: 30px;
                    border: none;
                    width: 120px;
                    font-size: 28px;
                }
                .btn:after {
                    border: none;
                }
            }

        }

        // 任务信息
        .task-info {
            background: #FFF;
            padding: 20px 30px;
            .task-info-content {
                font-size: 28px;
                &:after {
                    content: "";
                    display: block;
                    height: 1px;
                    width: 100%;
                    background: #CECECE;
                    margin-top: 20px;
                }
                .head {
                    padding-bottom: 15px;
                    .title {
                        font-size: 36px;
                        font-weight: bold;
                        color: #222222;
                        display: inline-block;
                        margin-right: 15px;
                        border: 1px;
                    }

                    .tag {
                        position: relative;
                        top: 50%;
                        transform: translateY(-15%);
                        display: inline-block;
                        padding: 8px 15px 5px;
                        border-radius: 30px;
                        color: #FFF;
                        margin: 0 5px;
                    }

                    .tag-green {
                        background: #0DC187;
                        box-shadow: 3px 3px 10px #2FDCA4;
                    }

                    .tag-gray {
                        background: #B1BABC;
                        box-shadow: 3px 3px 10px #84898A;
                    }

                    .tag-red {
                        background: #FF5252;
                        box-shadow: 3px 3px 10px #FF4D4D;
                    }
                }

                .content {
                    font-size: 28px;
                    color: #555555;
                    line-height: 1.4em;
                    height: 70px;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    margin-bottom: 30px;
                }

                .info {
                    &:after {
                        content: "";
                        display: block;
                        clear: both;
                    }
                    .left {
                        float: left;
                        p {
                            margin: 10px 0 0;
                            color: #909090;
                        }
                    }

                    .right {
                        float: right;
                        padding-top: 10px;
                        .btn {
                            background: #15CD85;
                            box-shadow: 3px 3px 10px #0ABC88;
                            color: #FFF;
                            padding: 0;
                            height: 50px;
                            line-height: 50px;
                            margin: 0 auto;
                            display: block;
                            align-items: center;
                            padding: 0 10px;
                            border-radius: 30px;
                            border: none;
                            width: 120px;
                            font-size: 28px;
                        }
                        .btn:after {
                            border: none;
                        }
                    }
                }
            }

        }

    }
</style>