(function () {
    var colDataItemArr = ['GenderNo', 'PartyFaceNo'];
    var colDataItemCode = ['usersex', 'PolityStatus'];

    var page = {
        isScroll: false,
        init: function ($page) {

            //增加联系人
            $page.find('.welT i').on('tap', function () {
                var FamilyHtml = '<div class="welCon"><form class="welConForm">' +
                    '<div class="welInput">' +
                    '<span>姓名</span>' +
                    '<input type="text" name="Name" placeholder="请填写姓名" />' +
                    '</div>' +
                    '<div class="welInput">' +
                    '<span>与本人关系</span>' +
                    '<div name="Relation" value=""></div>' +
                    '</div>' +
                    '<div class="welInput">' +
                    '<span>性别</span>' +
                    '<div name="Sex" value=""></div>' +
                    '</div>' +
                    '<div class="welInput">' +
                    '<span>年龄</span>' +
                    '<input type="text" name="Age" placeholder="请填写年龄" />' +
                    '</div>' +
                    '<div class="welInput">' +
                    '<span>政治面貌</span>' +
                    '<div name="Politicy" value=""></div>' +
                    '</div>' +
                    '<div class="welInput">' +
                    '<span>身份证号</span>' +
                    '<input type="text" name="IdCard" placeholder="请填写身份证号" />' +
                    '</div>' +
                    '<div class="welDel"> <i class="iconfont icon-move"></i> </div>' +
                    '</form></div>';
                var EmergePeopleHtml = '<div class="welCon"><form class="welConForm">' +
                    '<div class="welInput">' +
                    '<span>姓名</span>' +
                    '<input type="text" name="Name" placeholder="请填写姓名" />' +
                    '</div>' +
                    '<div class="welInput">' +
                    '<span>与本人关系</span>' +
                    '<input type="text" name="Relation"  placeholder="请填写与本人关系" />' +
                    '</div>' +
                    '<div class="welInput">' +
                    '<span>手机号码</span>' +
                    '<input type="text" name="Telephone"  placeholder="请填写手机号码" />' +
                    '</div>' +
                    '<div class="welInput">' +
                    '<span>座机号码</span>' +
                    '<input type="text" name="Mobile" placeholder="请填写座机号码" />' +
                    '</div>' +
                    '<div class="welInput">' +
                    '<span>联系地址</span>' +
                    '<input type="text" name="Address" placeholder="请填写联系地址" />' +
                    '</div>' +
                    '<div class="welDel"> <i class="iconfont icon-move"></i> </div>' +
                    '</form></div>';


                var obj = $(this).parent().next();
                if ($(this).attr('title') == '紧急联系人') {
                    obj.append(EmergePeopleHtml);
                } else {
                    obj.append(FamilyHtml);
                }


                page.bind($page);
            })
            //删除联系人
            $page.find('.welBox').on('tap', '.welDel i', function () {
                var parents = $(this).parents('.welCon')
                learun.layer.confirm('是否删除此联系人？', function (_index) {
                    _index == 1 ? parents.remove() : ''
                }, '提示', ['否', '是']);
            })
            //上传照片
            var inputNode = $page.find('#files');
            $page.find('#welImgBtn').on('tap', function () {
                var evt = new MouseEvent("click", {
                    bubbles: false,
                    cancelable: true,
                    view: window
                });
                inputNode[0].dispatchEvent(evt);
            })
            inputNode.on('change', function () {
                previewFile();
            });
            //数据绑定 
            learun.httpget(config.webapi + "StuInfoFresh/stuInfoFreshEntity", {}, (data) => {
                if (data) {
                    //基本信息
                    for (var id in data['StuInfoFreshEntity']) {
                        var dom = $('#' + id)[0];
                        if (dom) {
                            if (dom.tagName.toLowerCase() == "span") {
                                $('#' + id).html(data['StuInfoFreshEntity']['' + id + '']);
                                //数据字典
                                if ($.inArray(id, colDataItemArr) >= 0) {
                                    learun.clientdata.get('dataItem', {
                                        code: colDataItemCode[$.inArray(id, colDataItemArr)],
                                        key: data['StuInfoFreshEntity']['' + id + ''],
                                        callback: function (data, op) {
                                            $('#' + id).html(data.text);
                                        }
                                    });
                                }

                            } else if (dom.tagName.toLowerCase() == "input") {
                                $('#' + id).val(data['StuInfoFreshEntity']['' + id + '']);
                            }
                        }
                    }
                    //头像
                    $('#PhotoImg').attr('src', config.web + data['Url']);
                    //家庭成员
                    $('#StuInfoFreshFamily').html("");
                    $.each(data['StuInfoFreshFamilyList'], function (i, item) {
                        var html = '<div class="welCon"><form class="welConForm">' +
                            '<div class="welInput">' +
                            '<span>姓名</span>' +
                            '<input type="text" name="Name" placeholder="请填写姓名" value="' + item.Name + '"/>' +
                            '</div >' +
                            '<div class="welInput">' +
                            '<span>与本人关系</span>' +
                            //'<input type="text" name="Relation" placeholder="请填写与本人关系" value="' + item.Relation + '"/>' +
                            '<div name="Relation" value="' + item.Relation + '"></div>' +
                            '</div>' +
                            '<div class="welInput">' +
                            '<span>性别</span>' +
                            //'<input type="text" name="Sex" placeholder="请填写性别" value="' + item.Sex + '"/>' +
                            '<div name="Sex" value="' + item.Sex + '"></div>' +
                            '</div>' +
                            '<div class="welInput">' +
                            '<span>年龄</span>' +
                            '<input type="text" name="Age" placeholder="请填写年龄" value="' + item.Age + '"/>' +
                            '</div>' +
                            '<div class="welInput">' +
                            '<span>政治面貌</span>' +
                            //'<input type="text" name="Politicy" placeholder="请填写政治面貌" value="' + item.Politicy + '"/>' +
                            '<div name="Politicy" value="' + item.Politicy + '"/>' +
                            '</div>' +
                            '<div class="welInput">' +
                            '<span>身份证号</span>' +
                            '<input type="text" name="IdCard" placeholder="请填写身份证号" value="' + item.IdCard + '"/>' +
                            '</div>' +
                            '<div class="welDel"> <i class="iconfont icon-move"></i> </div>' +
                            '</form></div> ';
                        $('#StuInfoFreshFamily').append(html);

                    });

                    //紧急联系人
                    $('#StuInfoFreshEmergePeople').html("");
                    $.each(data['StuInfoFreshEmergePeopleList'], function (i, item) {
                        var html = '<div class="welCon"><form class="welConForm">' +
                            '<div class="welInput">' +
                            '<span>姓名</span>' +
                            '<input type="text" name="Name" placeholder="请填写姓名" value="' + item.Name + '"/>' +
                            '</div >' +
                            '<div class="welInput">' +
                            '<span>与本人关系</span>' +
                            '<input type="text" name="Relation" placeholder="请填写与本人关系" value="' + item.Relation + '"/>' +
                            '</div>' +
                            '<div class="welInput">' +
                            '<span>手机号码</span>' +
                            '<input type="text" name="Telephone" placeholder="请填写手机号码" value="' + item.Telephone + '"/>' +
                            '</div>' +
                            '<div class="welInput">' +
                            '<span>座机号码</span>' +
                            '<input type="text" name="Mobile" placeholder="请填写座机号码" value="' + item.Mobile + '"/>' +
                            '</div>' +
                            '<div class="welInput">' +
                            '<span>联系地址</span>' +
                            '<input type="text" name="Address" placeholder="请填写联系地址" value="' + item.Address + '"/>' +
                            '</div>' +
                            '<div class="welDel"> <i class="iconfont icon-move"></i> </div>' +
                            '</form></div > ';
                        $('#StuInfoFreshEmergePeople').append(html);

                    });

                    page.bind($page);


                }

            });

            //保存
            $page.find('#saveBtn').on('tap', function () {
                var postdata = {
                    ID: $('#ID').val(),
                    RegionNo: $('#RegionNo').val(),
                    FamilyAddress: $('#FamilyAddress').val(),
                    telephone: $('#telephone').val(),
                    Photo: $('#Photo').val(),
                    StuInfoFreshFamilyEntities: family(),
                    StuInfoFreshEmergePeopleEntities: emergePeople()
                };
                learun.httpget(config.webapi + "StuInfoFresh/saveStuInfoFresh", postdata, (data) => {
                    if (data) {
                        learun.layer.toast('保存成功');
                        location.reload();
                    }
                });

            });


        },
        bind: function ($page) {
            //性别
            $.each($page.find('[name="Sex"]'), function (i, item) {
                $page.find(item).lrpickerex({
                    code: 'usersex',
                    type: 'dataItem'
                });
                //赋值
                $(item).lrpickerSet($(item).attr('value'));
            });
            //政治面貌
            $.each($page.find('[name="Politicy"]'), function (i, item) {
                $page.find(item).lrpickerex({
                    code: 'PolityStatus',
                    type: 'dataItem'
                });
                //赋值
                $(item).lrpickerSet($(item).attr('value'));
            });
            //与本人关系
            $.each($page.find('#StuInfoFreshFamily').find('[name="Relation"]'), function (i, item) {
                $page.find(item).lrpickerex({
                    code: 'FamilyRelation',
                    type: 'dataItem'
                });
                //赋值
                $(item).lrpickerSet($(item).attr('value'));
            });
        }
    };
    //数据处理
    function family() {
        var list = new Array();
        $.each($('#StuInfoFreshFamily').children('.welCon'), function (i, item) {
            var o = $(item).children(".welConForm").serializeObject();
            o.Sex = $(item).find('[name="Sex"]').lrpickerGet();
            o.Politicy = $(item).find('[name="Politicy"]').lrpickerGet();
            o.Relation = $(item).find('[name="Relation"]').lrpickerGet();
            list.push(o);
        });
        return list;
    }
    function emergePeople() {
        var list = new Array();
        $.each($('#StuInfoFreshEmergePeople').children('.welCon'), function (i, item) {
            var o = $(item).children(".welConForm").serializeObject();
            list.push(o);
        });
        return list;
    }

    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    }

    //上传照片
    function previewFile() {
        var html = '';
        var file = document.querySelector('input[type=file]').files[0];
        var reader = new FileReader();
        reader.onloadend = function () {
            //$('#PhotoImg').attr('src', reader.result);
            var postData = {
                Base64Url: reader.result
            }
            learun.httppost(config.webapi + "StuInfoFresh/savePhoto", postData, (data) => {
                if (data) {
                    $('#Photo').val(data.AnnexesFileId);
                    $('#PhotoImg').attr('src', config.web + data.Url);
                } else {
                    learun.layer.toast('采集照片信息失败！');
                }
            });
        }
        if (file) {
            reader.readAsDataURL(file);
        }
    }

    //产生随机数
    function GenerateRandom() {
        var retstr = "";
        var date = new Date();
        retstr = date.getFullYear().toString() + "-" + date.getMonth().toString() + "-" + date.getDate().toString() + "-" + date.getUTCHours().toString() + "-" + date.getUTCMinutes() + "-" + date.getUTCSeconds().toString();
        var Num = "";
        for (var i = 0; i < 6; i++) {
            Num += Math.floor(Math.random() * 10);
        }
        return retstr + Num;
    }

    return page;
})();
