(function () {
    var keyValue = '';
    var $header = null;
    var titleText = '';
    var page = {
        isScroll: true,
        init: function ($page, param) {
            keyValue = param.keyValue;
            // 添加头部按钮列表 
            var _html = '\
                <div class="lr-form-header-cancel" > 取消</div >\
            <div class="lr-form-header-btnlist" >\
                    <div class="lr-form-header-more" ><i class="iconfont icon-more" ></i></div>\
                    <div class="lr-form-header-edit" ><i class="iconfont icon-edit" ></i></div>\
                </div>\
            <div class="lr-form-header-submit" >提交</div>';
            $header = $page.parents('.f-page').find('.f-page-header');
            $header.append(_html);
            // 取消 
            $header.find('.lr-form-header-cancel').on('tap', function () {
                learun.layer.confirm('确定要退出当前编辑？', function (_index) {
                    if (_index === '1') {
                        if (keyValue) {// 如果是编辑状态 
                            learun.formblur();
                            $header.find('.lr-form-header-cancel').hide();
                            $header.find('.lr-form-header-submit').hide();
                            $header.find('.lr-form-header-btnlist').show();
                            $header.find('.f-page-title').text(titleText);
                            $page.find('.lr-form-container').setFormRead();
                        }
                        else {// 如果是新增状态 关闭当前页面 
                            learun.nav.closeCurrent();
                        }
                    }
                }, 'learun敏捷框架提示', ['取消', '确定']);
            });
            // 编辑 
            $header.find('.lr-form-header-edit').on('tap', function () {
                $header.find('.lr-form-header-btnlist').hide();
                $header.find('.lr-form-header-cancel').show();
                $header.find('.lr-form-header-submit').show();
                titleText = $header.find('.f-page-title').text();
                $header.find('.f-page-title').text('编辑');
                $page.find('.lr-form-container').setFormWrite();
            });

            // 提交 
            $header.find('.lr-form-header-submit').on('tap', function () {
                // 获取表单数据 
                if (!$page.find('.lr-form-container').lrformValid()) {
                    return false;
                }
                var _postData = {}
                _postData.keyValue = keyValue;
                console.log($page.find('.lr-form-container').lrformGet());
                _postData.strEntity = JSON.stringify($page.find('.lr-form-container').lrformGet());
                learun.layer.loading(true, '正在提交数据');
                learun.httppostnoLogin(config.webapi + 'learun/EducationalAdministration/EmpRegister/save', _postData, (data) => {
                    learun.layer.loading(false);
                    if (data) {// 表单数据保存成功 

                        learun.layer.toast("注册成功");
                        learun.nav.closeCurrent();
                    }
                });


            });
            page.bind($page, param);

            $header.find('.lr-form-header-cancel').show();
            $header.find('.lr-form-header-submit').show();


            var inputNode = $page.find('#files');
            var welImgAdd = $page.find('.welImgAdd');
            welImgAdd.on('tap', function () {
                var evt = new MouseEvent("click", {
                    bubbles: false,
                    cancelable: true,
                    view: window
                });
                inputNode[0].dispatchEvent(evt);
            })

            inputNode.on('change', function () {
                previewFile(welImgAdd);
            });
        },
        bind: function ($page, param) {

            $page.find('#F_CompanyId').lrpickerex({
                type: 'sourceData',
                code: 'company',
                ivalue: 'f_companyid',
                itext: 'f_fullname',
                firstRead: '1'
            });


            $page.find('#F_DepartmentId').lrpickerex({
                type: 'sourceData',
                code: 'classdata',
                ivalue: 'f_departmentid',
                itext: 'f_fullname',
                firstRead: '1'
            });
            $page.find('#DeptNo').lrpickerex({
                type: 'sourceData',
                code: 'CdDeptInfo',
                ivalue: 'deptid',
                itext: 'deptname',
                firstRead: '1'
            });
            $page.find('#GenderNo').lrpickerex({
                code: 'usersexbit',
                type: 'dataItem',
                firstDateItem: '1',
            });
            //$page.find('#Photo').imagepicker();
            $page.find('#Birthday').lrdate({
                type: 'date'
            });
            $page.find('#NationalityNo').lrpickerex({
                type: 'sourceData',
                code: 'BCdNationality',
                ivalue: 'nationalityno',
                itext: 'nationality',
                firstRead: '1'

            });
            $page.find('#PartyFaceNo').lrpickerex({
                type: 'sourceData',
                code: 'BCdPartyFace',
                ivalue: 'partyfaceno',
                itext: 'partyface',
                firstRead: '1'
            });
            $page.find('#CultureDegreeNo').lrpickerex({
                type: 'sourceData',
                code: 'BCdCultureDegree',
                ivalue: 'culturedegreeno',
                itext: 'culturedegree',
                firstRead: '1'
            });
            $page.find('#DegreeNo').lrpickerex({
                type: 'sourceData',
                code: 'BCdDegree',
                ivalue: 'degreeno',
                itext: 'degreename',
                firstRead: '1'
            });
        }, destroy: function (pageinfo) {
            $header = null;
            keyValue = '';
        }
    };



    //上传照片
    function previewFile(obj) {
        var html = '';
        var file = document.querySelector('input[type=file]').files[0];
        var reader = new FileReader();
        reader.onloadend = function () {
            //$('#PhotoImg').attr('src', reader.result);
            var account = $("#EmpNo").val();
            if (!account) {
                learun.layer.toast("请填写职工编号");
                return;
            }
            console.log(reader);
            var params = {
                account: account,
                Base64Url: reader.result
            };
            var param = {};
            var type = learun.type(params);
            if (type === 'object' || type === 'array') {
                param.data = JSON.stringify(params);
            }
            else if (type === 'string') {
                param.data = params;
            }

            console.log(222);
            //learun.httppost(config.webapi + "learun/EducationalAdministration/EmpRegister/savePhoto", params, (data) => {
            //    if (data) {
            //        $('#Photo').val(data.AnnexesFileId);
            //        $('#PhotoImg').attr('src', config.web + data.Url);
            //    } else {
            //        learun.layer.toast('采集照片信息失败！');
            //    }
            //});

            $.ajax({
                url: config.webapi + "learun/EducationalAdministration/EmpRegister/savePhoto",
                data: param,
                type: "post",
                dataType: "json",
                async: false,
                success: function (res) {
                    if (res.code == 200) {
                        if (res.data) {
                            $('#Photo').val(res.data.AnnexesFileId);
                            var html = '<img id="PhotoImg" src="' + config.web + res.data.Url + '" alt="">'
                            obj.html(html)
                            //                          $('#PhotoImg').attr('src', config.web + res.data.Url);
                        } else {
                            learun.layer.toast('采集照片信息失败！');
                        }
                    }
                }


            })




        }
        if (file) {
            reader.readAsDataURL(file);
        }
    }

    return page;



})(); 