import {
    getToken
} from '../../../utils/auth'
import {
    findDictCache
} from '../../../api/publics'
import {
    subdeptsList,
    findSubdeptsGroupById,
    registerSubDeptsGroupGL
} from '../../../api/project'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        flowNodes: [{
            text: '信息登记'
        }, {
            text: '信息审核'
        }, {
            text: '班组入场'
        }],
        form: {
            subDeptId: "",
            subDeptType: "",
            subDeptName: "",
            subDeptCode: "",
            groupName: "",
            craftType: "1",
            craftPost: ""
        },
        subDeptList: [],
        craftPostList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (!getToken()) {
            wx.redirectTo({
                url: '../../../login/login',
            })
        }
        if (options && options.id) {
            //查询数据回填...
            this.initData(options.id);
            this.getDictCache(options.id);
            this.getAllSubDepts(options.id);
        } else {
            this.getDictCache(null);
            this.getAllSubDepts(null);
        }
        this.setData({
            "form.projectId": app.globalData.useProjectId,
            "form.projectName": app.globalData.useProjectName
        });

    },

    /**
     * 获取单位
     */
    getAllSubDepts(optId) {
        let params = "pageNum=1&pageSize=1000&useStatus=0&activeTags=finished&projectId=" + app.globalData.useProjectId;
        subdeptsList(params).then(res => {
            let list = [];
            res.rows.forEach(item => {
                list.push({
                    "id": item.id,
                    "text": item.subDeptName,
                    "type": item.subDeptType,
                    "code": item.subDeptCode,
                });
            });
            this.setData({
                subDeptList: list
            });
            if (!optId) {
                this.setData({
                    "form.subDeptId": list.length > 0 ? list[0].id : null,
                    "form.subDeptType": list.length > 0 ? list[0].type : null,
                    "form.subDeptName": list.length > 0 ? list[0].text : null,
                    "form.subDeptCode": list.length > 0 ? list[0].code : null
                });
            }
        });
    },

    /**
     * 获取字典缓存数据
     */
    getDictCache(optId) {
        /**
         * 初始化工种类型
         * (普通工种)
         */
        findDictCache("pro_craft_post").then(res => {
            if (res.code == 200) {
                let list = [];
                res.data.forEach(item => {
                    if (item.cssClass == "1") {
                        list.push({
                            "id": item.dictValue,
                            "text": item.dictLabel
                        });
                    }
                });
                this.setData({
                    craftPostList: list
                });
                if (!optId) {
                    this.setData({
                        "form.craftPost": list[0].id
                    });
                }
            }
        });
    },

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

    /**
     * 提交班组信息
     */
    submitValues() {
        let {
            form
        } = this.data;
        //数据效验
        if (!form.projectId) {
            app.toast("数据异常,请刷新页面重试!")
            return false;
        }
        //数据效验
        if (!form.craftPost) {
            app.toast("请选择工种类型!");
            return false;
        }
        if (!form.groupName) {
            app.toast("请填写班组名称!");
            return false;
        }
        let that = this;
        //弹出确认
        wx.showModal({
            title: '提示',
            content: '是否确认保存单位班组信息?',
            success: function (sm) {
                if (sm.confirm) {
                    that.submitSubDeptForm();
                }
            }
        })
    },

    /**
     * 提交单位班组信息
     */
    submitSubDeptForm() {
        let _form = {
            ...this.data.form
        };
        this.setData({
            loadShow: true
        })
        registerSubDeptsGroupGL(_form).then(res => {
            this.setData({
                loadShow: false
            });
            if (res.code == 200) {
                app.toast("保存数据成功!")
                setTimeout(() => {
                    wx.redirectTo({
                        url: `../list/index`,
                    })
                }, 200)
            }
        });
    },

    //选择单位
    onSubDept(e) {
        let _list = this.data.subDeptList;
        _list.forEach(item => {
            if (item.id == e.detail.id) {
                this.setData({
                    "form.subDeptId": item.id,
                    "form.subDeptType": item.type,
                    "form.subDeptName": item.text,
                    "form.subDeptCode": item.code
                })
            }
        });
    },

    /**
     * 选择工种类型
     * @param {*} e 
     */
    onCraftPost(e) {
        this.setData({
            "form.craftPost": e.detail.id
        })
    },

    //填写班组名称
    onGroupName(e) {
        this.setData({
            "form.groupName": e.detail.value
        })
    },

    returnToPage: function () {
        wx.redirectTo({
            url: `../list/index`
        })
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})