<template>
    <view>
        <!-- pages/oneselfCenter/page/batchSchedullingHandle/batchSchedullingHandle.wxml -->
        <nav-bar :navbarData="navbarData"></nav-bar>
        <view class="scheduleHandle" :style="'margin-top: ' + (height * 2 + 10) + 'rpx; height: calc(100vh - ' + (height * 2 + 10) + 'rpx)'">
            <view class="handleDate">此次批量设置您已选择{{ chooseDays.length }}天</view>
            <view class="handleContent" style="">
                <block v-if="timeRange.length > 0">
                    <van-collapse :value="activeNames" @change="onChange">
                        <block v-for="(item, index) in timeRange" :key="index">
                            <van-collapse-item :title="item.descripts" :name="item.id">
                                <schedulingDetail
                                    :timeRange="item"
                                    :resourceTypeID="resourceTypeID"
                                    :resourceTypeDesc="resourceTypeDesc"
                                    @getTimeRange="getTimeRange"
                                    @getResourceTime="getResourceTime"
                                />
                            </van-collapse-item>
                        </block>
                    </van-collapse>
                </block>
            </view>
            <button class="handle-btn" @tap="addScheduling" :disabled="saveflag">{{ saveflag && saveflag ? '保存中' : '保存排班' }}</button>
        </view>
    </view>
</template>

<script>
import Cache from '../../../../utils/cache';
import navBar from '@/pages/components/navbar/index';
import schedulingDetail from '../../components/schedulingDetail/schedulingDetail';
// pages/oneselfCenter/page/batchSchedullingHandle/batchSchedullingHandle.js
import { $http } from '../../../../config/https.js';
const app = getApp();
import util from '../../../../utils/util.js';
export default {
    components: {
        navBar,
        schedulingDetail
    },
    data() {
        return {
            activeNames: ['5'],
            timeRange: [],

            //大时段
            height: app.globalData.height,

            saveflag: false,
            chooseDays: 0,
            resourceTypeID: '',
            resourceTypeDesc: '',
            schedulingTypeArr: [],

            //  排班类型
            resData: [],

            //  排班明细数据
            //  排班时段对应职称
            regTitleID: '',

            dateList: '',
            navbarData: {}
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let { resourceTypeID, resourceTypeDesc } = options;
        let dateList = [];
        dateList = JSON.parse(options.dateList);
        this.setData({
            dateList,
            resourceTypeID,
            resourceTypeDesc,
            chooseDays: dateList,
            navbarData: {
                background: '',
                title: resourceTypeDesc !== '' ? `批量排班(${resourceTypeDesc})` : '批量排班',
                //导航栏 中间的标题
                height: '100',
                bottom: true,
                isGoBack: true
            }
        });
        this.getSchedulingType();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        /** 排班类型 */
        getSchedulingType: function () {
            let userData = Cache.get('userData');
            let { userID, groupID, locID, hospID } = userData;
            let { resourceTypeDesc, resourceTypeID } = this;
            let data = {
                params: [
                    {
                        userID: userID,
                        groupID: groupID,
                        locID: locID,
                        hospID: hospID,
                        resTypeID: resourceTypeID
                    }
                ]
            };
            $http.post('urlDeault', this, {
                code: '03100130',
                data: data,
                success: function (res) {
                    if (res.errorCode == '0') {
                        let regTitleID;
                        let titleData = res.result.Data.filter((item) => item.descripts == resourceTypeDesc);
                        if (titleData.length > 0) {
                            regTitleID = titleData[0].id;
                        }
                        this.setData(
                            {
                                schedulingTypeArr: res.result.Data,
                                regTitleID
                            },
                            () => {
                                this.getSchedulingInfo();
                            }
                        );
                    }
                }
            });
        },

        /**获取职称ID */
        getRegTitleId: function () {
            let array = this.schedulingTypeArr;
            if (array.length > 0) {
                return array[this.schedulingTypeIndex].id;
            }
        },

        /**获取出诊班次大时段 */
        getTimeRange: function (data) {
            let { timeIntervalID, timeRangeID } = data.detail;
            let { resData, regTitleID } = this;
            let flag = true;
            resData.map((item, index) => {
                if (item.timeRangeID === timeRangeID) {
                    item.timeIntervalID = timeIntervalID;
                    flag = false;
                }
            });
            if (flag) {
                resData.push({
                    regTitleID,
                    timeIntervalID,
                    timeRangeID
                });
            }
        },

        /**获取分时段号源设置数据 */
        getResourceTime: function (data) {
            let resTimeData = [...data.detail];
            if (resTimeData.length <= 0) {
                return;
            }
            let timeIntervalID = resTimeData[0].timeIntervalID;
            let { resData } = this;
            resData.map((item, index) => {
                if (item.timeIntervalID === timeIntervalID) {
                    item['resTime'] = resTimeData;
                }
            });
        },

        // 保存排班
        addScheduling: function () {
            let that = this;
            if (that.saveflag == true) {
                throw SyntaxError();
            }
            // that.setData({saveflag:true}); //请求前禁用标记改为true,防止重复请求
            let userData = Cache.get('userData');
            let { userID, locID, hospID } = userData;
            let { timeDetailArr, chooseDays, resourceTypeID, resData, resourceTypeDesc } = this;
            //过滤完全没有填写的大时段
            resData = resData.filter((item, index) => item.hasOwnProperty('resTime'));
            let resDataNew = resData.map((data) => {
                let newObj = {
                    ...data
                };
                let resTimeNew = newObj.resTime.map((item) => {
                    let itemNew = {
                        ...item
                    };
                    if (itemNew.registNum == '') {
                        itemNew.registNum = 0;
                    }
                    return itemNew;
                });
                newObj['resTime'] = resTimeNew;
                return newObj;
            });
            let data = {
                params: [
                    {
                        userID: userID,
                        locID: locID,
                        hospID: hospID,
                        resourceTypeID,
                        dateList: chooseDays,
                        resData: resDataNew
                    }
                ]
            };
            $http.post('urlDeault', this, {
                code: '03100151',
                data: data,
                success: function (res) {
                    if (res.errorCode == '0') {
                        let pages = getCurrentPages();
                        let prevPage = pages[pages.length - 2];
                        prevPage.onShowCall({
                            resourceTypeID,
                            resourceTypeDesc
                        });
                        uni.navigateBack({
                            delta: 1
                        });
                    }
                    setTimeout(() => {
                        that.setData({
                            saveflag: false
                        }); //请求成功禁用标记改回false
                    }, 1000);
                },
                fail: function () {
                    setTimeout(() => {
                        that.setData({
                            saveflag: false
                        }); //请求虽然失败,禁用标记也改回false
                    }, 1000);
                }
            });
        },

        onChange: function (event) {
            this.setData({
                activeNames: event.detail
            });
        },

        // 点击日历获取排班信息
        getSchedulingInfo: function (lookDate) {
            let userData = Cache.get('userData');
            let { hospID, userID, locID } = userData;
            let obj = {
                params: [
                    {
                        // userID: userID,
                        // locID: locID,
                        hospID: hospID
                        // resourceTypeID: this.getIndexId(), //出诊类型ID
                        // date: lookDate
                    }
                ]
            };

            $http.post('urlDeault', this, {
                code: '01040010',
                data: obj,
                success: function (res) {
                    if (res.errorCode == '0') {
                        let activeNames = [];
                        if (res.timeRange.length > 0) {
                            activeNames.push(res.timeRange[0].id);
                        }
                        this.setData({
                            timeRange: res.timeRange,
                            activeNames: activeNames
                            // rangeSchedulingsArr: [],
                        });
                    }
                }
            });
        }
    }
};
</script>
<style>
.scheduleHandle {
    display: flex;
    flex-direction: column;
}
.handleContent {
    overflow: auto;
    flex: 1;
}
.handle-btn {
    background-color: #0079fe;
    color: #ffffff;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 100rpx;
    line-height: 100rpx;
}
</style>
