/* * 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园(http://www.learun.cn) 
 * Copyright (c) 2013-2018 北京泉江科技有限公司 
 * 创建人：超级管理员
 * 日  期：2020-02-03 14:37
 * 描  述：疫情记录
 */
(function () {
    function translatePoint(position) {
        learun.layer.loading(false);
        var currentLat = position.coords.latitude;
        var currentLon = position.coords.longitude;
        // 创建地理编码实例      
        var myGeo = new BMap.Geocoder();
        // 根据坐标得到地址描述    
        myGeo.getLocation(new BMap.Point(currentLon, currentLat), function (result) {
            if (result) {
                $("#HomeAddress").val(result.address);
            }
        });
    }
    function error(err) {
        learun.layer.loading(false);
        alert("定位失败，请打开位置服务或手动填写地址");
    }
    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();
                        }
                    }
                }, '智慧校园提示', ['取消', '确定']);
            });
            // 编辑
            $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-more').on('tap', function () {
                learun.actionsheet({
                    id: 'more',
                    data: [
                        {
                            text: '删除',
                            mark: true,
                            event: function () {// 删除当前条信息
                                learun.layer.confirm('确定要删除该笔数据吗？', function (_index) {
                                    if (_index === '1') {
                                        learun.layer.loading(true, '正在删除该笔数据');
                                        learun.httppost(config.webapi + 'learun/PersonnelManagement/EpidemicSituationCopy/delete', keyValue, (data) => {
                                            learun.layer.loading(false);
                                            if (data) {// 删除数据成功
                                                learun.nav.closeCurrent();
                                                var prepage = learun.nav.getpage('PersonnelManagement/EpidemicSituationCopy');
                                                prepage.grid.reload();
                                            }
                                        });
                                    }
                                }, '智慧校园提示', ['取消', '确定']);
                            }
                        }

                    ],
                    cancel: function () {
                    }
                });
            });
            // 提交
            $header.find('.lr-form-header-submit').on('tap', function () {
                // 获取表单数据
                if (!$page.find('.lr-form-container').lrformValid()) {
                    return false;
                }
                var _postData = {}
                _postData.keyValue = keyValue;

                var pageForm = $page.find('[data-table="EpidemicSituation"]').lrformGet();
                var allArea = $page.find('#ProvinceArea').lrpickerGet();
                if (allArea) {
                    var list = allArea.split(',');

                    pageForm.Provice = list[0] || '';
                    pageForm.OutArea = list[1] || '';
                    pageForm.City = list[2] || '';
                }

                console.log('是否接触家人' + $page.find('#HasFamily').lrpickerGet());
                _postData.strEntity = JSON.stringify(pageForm);
                if ($page.find('#HasFamily').lrpickerGet() == '1') {

                    var data1 = $page.find('#ContactsDetails').lrgridGet();
                    $.each(data1,
                        function (i, item) {
                            if (item.ProvinceArea == 'undefined') return;
                            var areaInfo = item.ProvinceArea.split(',');
                            item.Province = areaInfo[0];
                            item.City = areaInfo[1];
                            item.Area = areaInfo[2];
                        })
                    _postData.strcontactsDetailsList = JSON.stringify(data1);
                } else {
                    _postData.strcontactsDetailsList = '[]';
                }
                if ($page.find('#IsOut').lrpickerGet() == '1') {

                    var data2 = $page.find('#PassPlaces').lrgridGet();
                    $.each(data2,
                        function (i, item) {
                            if (item.ProvinceArea == 'undefined') return;
                            var areaInfo = item.ProvinceArea.split(',');
                            item.Province = areaInfo[0];
                            item.City = areaInfo[1];
                            item.Area = areaInfo[2];
                        })
                    _postData.strpassPlaceList = JSON.stringify(data2);
                } else {
                    _postData.strpassPlaceList = '[]';
                }

                if ($page.find('#HasPeople').lrpickerGet() == '1') {
                    var data3 = $page.find('#PeopleDetails').lrgridGet();
                    $.each(data3,
                        function (i, item) {
                            if (item.ProvinceArea == 'undefined') return;
                            var areaInfo = item.ProvinceArea.split(',');
                            item.Province = areaInfo[0];
                            item.City = areaInfo[1];
                            item.Area = areaInfo[2];
                        })

                    _postData.strpeopleDetailsList = JSON.stringify(data3);
                } else {

                    _postData.strpeopleDetailsList = '[]';
                }
                learun.layer.loading(true, '正在提交数据');
                learun.httppost(config.webapi + 'learun/PersonnelManagement/EpidemicSituationCopy/save', _postData, (data) => {
                    learun.layer.loading(false);
                    if (data) {// 表单数据保存成功
                        if (keyValue) {
                            learun.layer.toast('保存数据成功!');
                            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();
                        }
                        var prepage = learun.nav.getpage('PersonnelManagement/EpidemicSituationCopy');
                        prepage.grid.reload();
                    }
                });
            });
            page.bind($page, param);
            if (keyValue) {
                // 添加编辑按钮
                $page.find('.lr-form-container').setFormRead();
                $header.find('.lr-form-header-btnlist').show();
                // 获取表单数据
                learun.layer.loading(true, '获取表单数据');
                learun.httpget(config.webapi + 'learun/PersonnelManagement/EpidemicSituationCopy/form', keyValue, (data) => {
                    if (data) {
                        //省市区数据处理
                        $.each(data.ContactsDetails, function (i, item) {
                            item.ProvinceArea = item.Province + ',' + item.City + ',' + item.Area;
                        })
                        $.each(data.PassPlaces, function (i, item) {
                            item.ProvinceArea = item.Province + ',' + item.City + ',' + item.Area;
                        })
                        $.each(data.PeopleDetails, function (i, item) {
                            item.ProvinceArea = item.Province + ',' + item.City + ',' + item.Area;
                        })
                        for (var id in data) {
                            if (data[id].length) {
                                $page.find('#' + id).lrgridSet(data[id]);
                            }
                            else {
                                $page.find('[data-table="' + id + '"]').lrformSet(data[id]);
                            }
                        }


                    }
                    learun.layer.loading(false);
                });
            }
            else {
                $header.find('.lr-form-header-cancel').show();
                $header.find('.lr-form-header-submit').show();
            }
        },
        bind: function ($page, param) {

            $page.find('#Creater').lrpickerex({
                code: 'BaseUser',
                type: 'sourceData',
                ivalue: 'f_userid',
                itext: 'f_realname'
            }).lrpickerSet(0);

            $page.find('#Type').lrpickerex({
                code: 'EUserType',
                type: 'dataItem'
            }).lrpickerSet(0);

            $page.find('#Department').lrpickerex({
                type: 'sourceData',
                code: 'classdata',
                ivalue: 'f_departmentid',
                itext: 'f_fullname'
            });
            $page.find('#UnitName').lrpickerex({
                type: 'sourceData',
                code: 'company',
                ivalue: 'f_companyid',
                itext: 'f_fullname'
            });
            $page.find('#HealthSituation').lrpickerex({
                code: 'EpiHealth',
                type: 'dataItem'
            }).lrpickerSet(0);
            $page.find('#Gender').lrpickerex({
                code: 'usersex',
                type: 'dataItem'
            }).lrpickerSet(0);
            $page.find('#IsOut').lrpickerex({
                code: 'YesOrNoInt',
                type: 'dataItem',
                change: function (aa) {
                    if (aa == 1) {
                        $('#PassPlaces').show();
                    } else {
                        $('#PassPlaces').hide();
                    }
                }
            }).lrpickerSet(0);
            $page.find('#HasFamily').lrpickerex({
                code: 'YesOrNoInt',
                type: 'dataItem',
                change: function (aa) {
                    if (aa == 1) {
                        $('#ContactsDetails').show();
                    } else {
                        $('#ContactsDetails').hide();
                    }
                }
            }).lrpickerSet(0);
            $page.find('#HasPeople').lrpickerex({
                code: 'YesOrNoInt',
                type: 'dataItem',
                change: function (aa) {
                    if (aa == 1) {
                        $('#PeopleDetails').show();
                    } else {
                        $('#PeopleDetails').hide();
                    }
                }
            }).lrpickerSet(0);

            $page.find('#CreateTime').lrdate({
                type: 'date'
            });
            $page.find('#ContactsDetails').lrgrid({
                title: '接触家人',
                componts: [
                    { name: '名字', field: 'Name', type: 'input' },
                    { name: '性别', field: 'Gender', type: 'select', code: 'usersex', datatype: 'dataItem' },
                    { name: '联系方式', field: 'Mobile', type: 'input' },
                    { name: '隔离方式', field: 'QTType', type: 'select', code: 'QuarantineType', datatype: 'dataItem' },
                    { name: '省市区', field: 'ProvinceArea', type: 'picker', data: cityData, level: 3, placeholder: "请选择(必填)" },
                    { name: '隔离地点', field: 'QTAddress', type: 'input' },
                    { name: '隔离时间', field: 'QTTime', type: 'datetime', datetime: '' },
                    { name: '接触隔离时间', field: 'QTContactTime', type: 'datetime', datetime: '' },
                    { name: '健康状况', field: 'Fever', type: 'select', code: 'EpiHealth', datatype: 'dataItem' },
                ]
            });

            $page.find('#PeopleDetails').lrgrid({
                title: '接触人',
                componts: [
                    { name: '名字', field: 'Name', type: 'input' },
                    { name: '性别', field: 'Gender', type: 'select', code: 'usersex', datatype: 'dataItem' },
                    { name: '联系方式', field: 'Mobile', type: 'input' },
                    { name: '隔离方式', field: 'QTType', type: 'select', code: 'QuarantineType', datatype: 'dataItem' },
                    { name: '省市区', field: 'ProvinceArea', type: 'picker', data: cityData, level: 3, placeholder: "请选择(必填)" },
                    { name: '隔离地点', field: 'QTAddress', type: 'input' },
                    { name: '隔离时间', field: 'QTTime', type: 'datetime', datetime: '' },
                    { name: '接触隔离时间', field: 'QTContactTime', type: 'datetime', datetime: '' },
                    { name: '健康状况', field: 'Fever', type: 'select', code: 'EpiHealth', datatype: 'dataItem' },
                ]
            });

            $page.find('#PassPlaces').lrgrid({
                title: '经过地点记录',
                componts: [
                    { name: '类别', field: 'PassType', type: 'select', code: 'EpiPassType', datatype: 'dataItem' },
                    { name: '省市区', field: 'ProvinceArea', type: 'picker', data: cityData, level: 3, placeholder: "请选择(必填)" },
                    { name: '详细地址', field: 'Address', type: 'input' },
                    { name: '时间', field: 'Time', type: 'datetime', datetime: '' },
                    { name: '交通工具', field: 'Vehicle', type: 'select', code: 'Vehicle', datatype: 'dataItem' },
                    { name: '班次号', field: 'VehicleNo', type: 'input' },
                ]
            });

            //新建时获取登陆人信息
            var userInfo = learun.storage.get('userinfo');
            console.log(userInfo);
            if (!keyValue) {


                $page.find('#Creater').lrpickerSet(userInfo.baseinfo.userId);
                $page.find('#UnitName').lrpickerSet(userInfo.baseinfo.companyId);
                $page.find('#Department').lrpickerSet(userInfo.baseinfo.departmentId);
                var time = new Date();
                $page.find('#CreateTime').lrdateSet(time);
                $page.find('#Gender').lrpickerSet(userInfo.baseinfo.gender);
                $page.find('#Mobile').val(userInfo.baseinfo.mobile);
                if (userInfo.baseinfo.Description == "学生") {
                    $page.find('#Type').lrpickerSet(5);
                    learun.httpget(config.webapi + 'learun/PersonnelManagement/EpidemicSituationCopy/getStuSource',
                        userInfo.baseinfo.account,
                        (data) => {
                            if (data) {
                                $page.find('#HomeAddress').val(data.Address);
                                $page.find('#StudentSource').val(data.res);
                            }
                        });
                } else {
                    learun.httpget(config.webapi + 'learun/PersonnelManagement/EpidemicSituationCopy/getUserSource',
                        userInfo.baseinfo.userId,
                        (data) => {
                            if (data) {
                                $page.find('#HomeAddress').val(data.Address);
                            }
                        });
                }
            }

            if (userInfo.baseinfo.Description == "学生") {
                $page.find('#Department').parent().hide();
                $page.find('#StudentSource').parent().show();
            } else {
                $page.find('#Department').parent().show();
                $page.find('#StudentSource').parent().hide();
            }
            navigator.geolocation.getCurrentPosition(translatePoint, error);
            learun.layer.loading(true, '定位中...');
        }, destroy: function (pageinfo) {
            $header = null;
            keyValue = '';
        }

    };
    return page;
})();

