import {
    getToken,
    getUserInfo
} from '../../../utils/auth'
import {
    updateGroupLeader,
    fileGroupLeaderUsers,
    findSubdeptsGroupById,
    editSubGroupsUseStatus
} from '../../../api/project'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 100,
        flowNodes: [{
            text: '信息登记'
        }, {
            text: '信息审核'
        }, {
            text: '班组入场'
        }],
        form: {},
        editFlag:false,
        isChange: false,
        leaderUserList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (!getToken()) {
            wx.redirectTo({
                url: '../../../login/login',
            })
        }
        const proUserInfo = getUserInfo();
        this.setData({
            editFlag: proUserInfo.projectUserInfo.subDeptType=='1'?true:false
        });
        if (options && options.id) {
            //查询数据回填...
            this.initData(options.id);
        }
    },

    /**
     * 初始化数据
     * @param {*} id 
     */
    initData(id) {
        findSubdeptsGroupById(id).then(res => {
            if (res.code == 200) {
                this.setData({
                    active: 100,
                    form: res.data
                });
            }
        });
    },

    /**
     * 初始化班组长
     */
    initLeaderUser(subDeptId, craftPost) {
        fileGroupLeaderUsers(subDeptId, craftPost).then(res => {
            if (res.code == 200) {
                let list = [];
                res.data.forEach(item => {
                    let _pst = "";
                    if (item.userPost == "2") {
                        _pst = " [项目经理]"
                    } else if (item.userPost == "6") {
                        _pst = " [材料员]"
                    } else if (item.userPost == "8") {
                        _pst = " [安全员]"
                    }
                    item.text = item.userName + _pst;
                    list.push(item);
                });
                this.setData({
                    leaderUserList: list
                });
                if (!this.data.form.groupLeaderPhone && list.length > 0) {
                    this.setData({
                        "form.groupLeaderName": list[0].userName,
                        "form.groupLeaderCode": list[0].userCode,
                        "form.groupLeaderPhone": list[0].id,
                    });
                }
            }
        });
    },

    /**
     * 返回上页
     */
    returnToPage: function () {
        wx.redirectTo({
            url: `../list/index`
        })
    },

    /**
     * 班组入场
     */
    submitSubDeptsIn() {
        let that = this;
        //弹出确认
        wx.showModal({
            title: '提示',
            content: '是否确认单位班组入场?',
            success: function (sm) {
                if (sm.confirm) {
                    that.submitSubDeptsUseStatus(0);
                }
            }
        })
    },

    /**
     * 班组离场
     */
    submitSubDeptsOut() {
        let that = this;
        //弹出确认
        wx.showModal({
            title: '提示',
            content: '是否确认单位班组离场?',
            success: function (sm) {
                if (sm.confirm) {
                    that.submitSubDeptsUseStatus(1);
                }
            }
        })
    },

    /**
     * 表单提交
     */
    submitSubDeptsUseStatus(status) {
        editSubGroupsUseStatus(this.data.form.id, status).then(res => {
            if (res.code == 200) {
                app.toast("操作成功!")
                setTimeout(() => {
                    wx.redirectTo({
                        url: `../list/index`,
                    })
                }, 200)
            }
        });
    },

    /**
     * 变更班组长
     */
    changeGroupLeader() {
        if (this.data.form.groupCode == 'DEFAULT' || this.data.form.groupCode == 'SPECIAL') {
            app.toast("系统默认班组不能修改班组长信息!")
            return false;
        }
        this.initLeaderUser(this.data.form.subDeptId, this.data.form.craftPost);
        this.setData({
            isChange: !this.data.isChange
        });
        setTimeout(() => {
            wx.pageScrollTo({
                scrollTop: 99999, // 滚动到内容区域的高度,即最底部
                duration: 100 // 滚动的动画持续时间
            });
        }, 500)
    },

    /**
     * 修改班组长
     */
    onLeaderUser(e) {
        let _list = this.data.leaderUserList;
        _list.forEach(item => {
            if (item.id == e.detail.id) {
                this.setData({
                    "form.groupLeaderName": item.userName,
                    "form.groupLeaderCode": item.userCode,
                    "form.groupLeaderPhone": item.id,
                })
            }
        });
    },

    /**
     * 保存班组长信息
     */
    submitSubGroupLeader(){
        let {
            form
        } = this.data;
        //数据效验
        if (!form.projectId) {
            app.toast("数据异常,请刷新页面重试!")
            return false;
        }
        //数据效验
        if (!form.groupLeaderPhone) {
            app.toast("请选择班组组长!");
            return false;
        }
        let that = this;
        //弹出确认
        wx.showModal({
            title: '提示',
            content: '是否确认变更班组组长?',
            success: function (sm) {
                if (sm.confirm) {
                    that.submitSubGroupLeaderForm(1);
                }
            }
        })
    },

    /**
     * 确认变更
     */
    submitSubGroupLeaderForm(){
        updateGroupLeader(this.data.form).then(res =>{
            if (res.code == 200) {
                app.toast("变更成功!")
                this.setData({
                    isChange: !this.data.isChange
                })
                this.onLoad({id: this.data.form.id});
            }
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})