import config from '../../../config'
import {
    getToken
} from '../../../utils/auth'
import {
    editSubDeptsUseStatus,
    findProSubDeptsInfoById
} from '../../../api/project'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 100,
        flowNodes: [{
            text: '信息登记'
        }, {
            text: '信息审核'
        }, {
            text: '单位入场'
        }],
        form: {
            subDeptInfos: {}
        },
        subDeptUserData: {
            userInfos: {},
        },
        imgBaseUrl: config.baseImgUrl
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (!getToken()) {
            wx.redirectTo({
                url: '../../../login/login',
            })
        }
        if (options && options.id) {
            //查询数据回填...
            this.initData(options.id);
        }
    },

    /**
     * 初始化数据
     * @param {*} id 
     */
    initData(id) {
        findProSubDeptsInfoById(id).then(deptRes => {
            if (deptRes.code == 200 && deptRes.data) {
                if (deptRes.data.proProjectInfoSubdeptsUsers) {
                    deptRes.data.leaderDegreeGrade = deptRes.data.proProjectInfoSubdeptsUsers.degreeGrade;
                    deptRes.data.eduFilePath = deptRes.data.proProjectInfoSubdeptsUsers.eduFilePath;
                    deptRes.data.subStep = deptRes.data.proProjectInfoSubdeptsUsers.subStep;
                    if (deptRes.data.proProjectInfoSubdeptsUsers.userPicture) {
                        deptRes.data.leaderUserPicture = deptRes.data.proProjectInfoSubdeptsUsers.userPicture.split(',');
                    }
                    if (deptRes.data.proProjectInfoSubdeptsUsers.subDeptPowerPath) {
                        deptRes.data.subDeptLeaderPowerPath = deptRes.data.proProjectInfoSubdeptsUsers.subDeptPowerPath.split(',');
                    }
                    if (deptRes.data.proProjectInfoSubdeptsUsers.userInfos) {
                        let userInfosJSON = JSON.parse(deptRes.data.proProjectInfoSubdeptsUsers.userInfos);
                        if (userInfosJSON.cardImgPos) {
                            deptRes.data.leaderCardImgPos = userInfosJSON.cardImgPos.split(',');
                        }
                        if (userInfosJSON.cardImgInv) {
                            deptRes.data.leaderCardImgInv = userInfosJSON.cardImgInv.split(',');
                        }
                        deptRes.data.proProjectInfoSubdeptsUsers.userInfos = userInfosJSON;
                        deptRes.data.nativePlace = userInfosJSON.nativePlace;
                        deptRes.data.nation = userInfosJSON.nation;
                        deptRes.data.address = userInfosJSON.address;
                        deptRes.data.emergencyContact = userInfosJSON.emergencyContact;
                        deptRes.data.contactPhone = userInfosJSON.contactPhone;
                    }
                }
                if (deptRes.data.businessLicensePath) {
                    deptRes.data.businessLicensePath = deptRes.data.businessLicensePath.split(',');
                }
                if (deptRes.data.subDeptInfos) {
                    let subDeptInfosJSON = JSON.parse(deptRes.data.subDeptInfos);
                    deptRes.data.subDeptInfos = subDeptInfosJSON;
                    if (deptRes.data.subDeptInfos.legalPersonCardImgPos) {
                        deptRes.data.subDeptInfos.legalPersonCardImgPos = deptRes.data.subDeptInfos.legalPersonCardImgPos.split(',');
                    }
                    if (deptRes.data.subDeptInfos.legalPersonCardImgInv) {
                        deptRes.data.subDeptInfos.legalPersonCardImgInv = deptRes.data.subDeptInfos.legalPersonCardImgInv.split(',');
                    }
                }
                let _subDeptUserData = {
                    userInfos: {}
                };
                if (deptRes.data.proProjectInfoSubdeptsUsers) {
                    _subDeptUserData = deptRes.data.proProjectInfoSubdeptsUsers;
                }
                this.setData({
                    active: 100,
                    form: deptRes.data,
                    subDeptUserData: _subDeptUserData
                });
            }
        });
    },

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

    /**
     * 展示图片
     * @param {*} e 
     */
    showImg: function (e) {
        let paths = e.target.dataset.set;
        let path = [];
        paths.split(',').forEach(url => {
            path.push(config.baseImgUrl + url);
        });
        wx.previewImage({
            urls: path,
            current: path[0]
        })
    },

    /**
     * 下载并打开文档
     * @param {*} e 
     */
    downFile: function (e) {
        let path = this.data.subDeptUserData.eduFilePath;
        wx.downloadFile({
            // 示例 url,并非真实存在
            url: config.baseUrl + '/file/download?fileName=' + path,
            header: {
                'Authorization': 'Bearer ' + getToken()
            },
            success: function (res) {
                const filePath = res.tempFilePath
                let fpt = path.split(".");
                wx.openDocument({
                    filePath: filePath,
                    fileType: fpt[fpt.length - 1],
                    success: function (res) {
                        console.log('打开文档成功')
                    },
                    fail: function (res) {
                        console.log(res)
                    }
                })
            }
        })
    },

    /**
     * 单位入场
     */
    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){
        editSubDeptsUseStatus(this.data.form.id,status).then(res =>{
            if(res.code==200){
                app.toast("操作成功!")
                setTimeout(() => {
                    wx.redirectTo({
                        url: `../list/index`,
                    })
                }, 200)
            }
        });
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})