import config from '../../../config'
import {
    getToken,
    getUserInfo
} from '../../../utils/auth'
import {
    editSubUsersPhone,
    editSubUsersUseStatus,
    findProSubUsersInfoById
} from '../../../api/project'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 100,
        flowNodes: [{
            text: '信息登记'
        }, {
            text: '信息审核'
        }, {
            text: '人员入场'
        }],
        form: {},
        newUserPhone:"",
        editFlag:false,
        isChange: false,
        imgBaseUrl: config.baseImgUrl
    },

    /**
     * 生命周期函数--监听页面加载
     */
    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) {
        findProSubUsersInfoById(id).then(userRes => {
            if (userRes.code == 200 && userRes.data != null) {
                if (userRes.data.userInfos) {
                    let userInfosJSON = JSON.parse(userRes.data.userInfos);
                    userRes.data.nativePlace = userInfosJSON.nativePlace;
                    userRes.data.nation = userInfosJSON.nation;
                    userRes.data.address = userInfosJSON.address;
                    userRes.data.emergencyContact = userInfosJSON.emergencyContact;
                    userRes.data.contactPhone = userInfosJSON.contactPhone;
                    userRes.data.bankName = userInfosJSON.bankName;
                    userRes.data.bankOffice = userInfosJSON.bankOffice;
                    userRes.data.bankCardNo = userInfosJSON.bankCardNo;
                    userRes.data.cardImgPos = userInfosJSON.cardImgPos;
                    userRes.data.cardImgInv = userInfosJSON.cardImgInv;
                }
                if (userRes.data.cardImgPos) {
                    userRes.data.cardImgPos = (this.data.imgBaseUrl + userRes.data.cardImgPos).split(',');
                }
                if (userRes.data.cardImgInv) {
                    userRes.data.cardImgInv = (this.data.imgBaseUrl + userRes.data.cardImgInv).split(',');
                }
                if (userRes.data.userPicture) {
                    userRes.data.userPicture = (this.data.imgBaseUrl + userRes.data.userPicture).split(',');
                }
                if (userRes.data.subDeptPowerPath) {
                    userRes.data.subDeptPowerPath = (this.data.imgBaseUrl + userRes.data.subDeptPowerPath).split(',');
                }
                this.setData({
                    active: 100,
                    form: userRes.data
                });
            }
        });
    },

    /**
     * 返回上页
     */
    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(url);
        });
        wx.previewImage({
            urls: path,
            current: path[0]
        })
    },

    /**
     * 下载并打开文档
     * @param {*} e 
     */
    downFile: function (e) {
        let path = this.data.form.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){
        editSubUsersUseStatus(this.data.form.id,status).then(res =>{
            if(res.code==200){
                app.toast("操作成功!")
                setTimeout(() => {
                    wx.redirectTo({
                        url: `../list/index`,
                    })
                }, 200)
            }
        });
    },

    /**
     * 输入联系电话
     * @param {*} e 
     */
    inputUserPhone(e) {
        this.setData({
            newUserPhone: e.detail.value
        })
    },

        /**
     * 变更手机号
     */
    changeUserPhone() {
        this.setData({
            isChange: !this.data.isChange
        });
        setTimeout(() => {
            wx.pageScrollTo({
                scrollTop: 99999, // 滚动到内容区域的高度,即最底部
                duration: 100 // 滚动的动画持续时间
            });
        }, 500)
    },

    /**
     * 
     */
    submitChangePhone(){
        let {
            form,
            newUserPhone
        } = this.data;
        if(!newUserPhone){
            app.toast("请输入新手机号码!")
            return false;
        }else{
            const phonePattern = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/;
            if (!phonePattern.test(newUserPhone)) {
                app.toast("手机号码不正确!");
                return false;
            }
        }
        let that = this;
        //弹出确认
        wx.showModal({
            title: '提示',
            content: '是否确认变更人员手机号?',
            success: function (sm) {
                if (sm.confirm) {
                    that.submitChangePhoneForm();
                }
            }
        })
    },

    /**
     * 确定
     * 变更手机号
     */
    submitChangePhoneForm(){
        let _form = {
            ...this.data.form
          };
        let {
            newUserPhone
        } = this.data;
        _form.userPhone = newUserPhone;
        _form.cardImgPos = "";
        _form.cardImgInv = "";
        _form.userPicture = "";
        _form.subDeptPowerPath = "";
        editSubUsersPhone(_form).then(res =>{
            if(res.code==200){
                app.toast("变更成功!")
                this.setData({
                    isChange: !this.data.isChange
                })
                this.onLoad({id: _form.id});
            }
        });
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})