<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>
<script>
    (function ($, win) {

        /* ************************** 变量定义 ******************************** */

        // 通用工具类库
        var Util = $.Util;

        // 页面主模块，对应于用户信息表区域
        var patientInfo = null;

        var dialog = frameElement.dialog;

        var dataModel = $.DataModel.init();
        /* ************************** 变量定义结束 ******************************** */

        /* *************************** 函数定义 ******************************* */
        function pageInit() {
            patientInfo.init();
        }

        /* *************************** 函数定义结束******************************* */

        /* *************************** 模块初始化 ***************************** */
        patientInfo = {
            $form: $("#div_patient_info_form"),
            $realName: $("#inp_realName"),
            $idCardNo: $("#inp_idCardNo"),
            $gender: $('input[name="gender"]', this.$form),
            $patientNation: $("#inp_patientNation"),
            $patientNativePlace: $("#inp_patientNativePlace"),
            $patientMartialStatus: $("#inp_select_patientMartialStatus"),
            $patientBirthday: $("#inp_patientBirthday"),
            $birthPlace: $("#inp_birthPlace"),
            $homeAddress: $("#inp_homeAddress"),
            $workAddress: $("#inp_workAddress"),
            $residenceType: $('input[name="residenceType"]', this.$form),
            $patientTel: $("#inp_patientTel"),
            $patientEmail: $("#inp_patientEmail"),
            $updateBtn: $("#div_update_btn"),
            $cancelBtn: $("#div_cancel_btn"),
            $resetPassword: $("#div_resetPassword"),
            $patientImgUpload: $("#div_patient_img_upload"),
            $password: $("#inp_password"),
            $div_password: $("#div_password"),

            init: function () {
                this.initForm();
                this.bindEvents();
                this.$patientImgUpload.instance = this.$patientImgUpload.webupload({
                    server: '${contextRoot}/fileUpload/uploadPicByte',
                    auto: true
                });
                this.$patientImgUpload.instance.on( 'uploadSuccess', function( file, resp ) {
                    debugger
                    $('#inp_img_url').val(resp.url);
                    $( '#'+file.id ).addClass('upload-state-done');
                });
            },
            initForm: function () {
                var self = this;
                this.$realName.ligerTextBox({width: 240});
                this.$idCardNo.ligerTextBox({width: 240});
                this.$gender.ligerRadio();
                this.initDDL(5, this.$patientNation);
                this.$patientNation.ligerTextBox({width: 240});
                this.$patientNativePlace.ligerTextBox({width: 240});
                this.$patientBirthday.ligerDateEditor({format: "yyyy-MM-dd"});

               this.$birthPlace.addressDropdown({tabsData:[
                    {name: '省份', url: '${contextRoot}/address/getParent', params: {level:'1'}},
                    {name: '城市', url: '${contextRoot}/address/getChildByParent'},
                    {name: '县区', url: '${contextRoot}/address/getChildByParent'},
                    {name: '街道'}
                ]});
               this.$homeAddress.addressDropdown({tabsData:[
                    {name: '省份', url: '${contextRoot}/address/getParent', params: {level:'1'}},
                    {name: '城市', url: '${contextRoot}/address/getChildByParent'},
                    {name: '县区', url: '${contextRoot}/address/getChildByParent'},
                    {name: '街道'}
                ]});
               this.$workAddress.addressDropdown({tabsData:[
                    {name: '省份', url: '${contextRoot}/address/getParent', params: {level:'1'}},
                    {name: '城市', url: '${contextRoot}/address/getChildByParent'},
                    {name: '县区', url: '${contextRoot}/address/getChildByParent'},
                    {name: '街道'}
                ]});

                this.$residenceType.ligerRadio();
                this.$patientTel.ligerTextBox({width: 240});
                this.$patientEmail.ligerTextBox({width: 240});
                this.$password.ligerTextBox({width: 240});

                this.$patientMartialStatus.ligerComboBox(
                        {
                            url: '${contextRoot}/dict/searchDictEntryList',
                            valueField: 'code',
                            textField: 'value',
                            dataParmName: 'detailModelList',
                            urlParms: {
                                dictId: 4
                            },
                            onSuccess: function () {
                                self.$form.Fields.fillValues({martialStatus: '${patientModel.martialStatus}'});
                            },
                            autocomplete: true
                        });

                this.$resetPassword.hide();
                this.$form.attrScan();
                if (!(Util.isStrEquals('${patientDialogType}', 'addPanient'))) {
                    this.$div_password.hide();
                    this.$resetPassword.show();
                    this.$form.Fields.fillValues({
                        name: '${patientModel.name}',
                        idCardNo: '${patientModel.idCardNo}',
                        gender: '${patientModel.gender}',
                        nation: '${patientModel.nation}',
                        nativePlace: '${patientModel.nativePlace}',
                        birthday: '${patientModel.birthday}',
                        birthPlace: '${patientModel.birthPlaceFull}',
                        homeAddress: '${patientModel.homeAddressFull}',
                        workAddress: '${patientModel.workAddressFull}',
                        residenceType: '${patientModel.residenceType}',
                        tel: '${patientModel.tel}',
                        email: '${patientModel.email}'
                    });
                }
            },
            initDDL: function (dictId, target) {
                target.ligerComboBox({
                    url: "${contextRoot}/dict/searchDictEntryList",
                    dataParmName: 'detailModelList',
                    urlParms: {dictId: dictId},
                    valueField: 'code',
                    textField: 'value'
                });
            },


            bindEvents: function () {
                var self = this;
                //修改人口信息
                patientInfo.$updateBtn.click(function () {
                    debugger;
                   var addressList = self.$form.Fields.birthPlace.getValue();
                    var homeAddressList = self.$form.Fields.homeAddress.getValue();
                    var workAddressList = self.$form.Fields.workAddress.getValue();
                    var values = $.extend({},self.$form.Fields.getValues(),{
                        birthPlace: {
                            province:  addressList.names[0],
                            city: addressList.names[1],
                            district: addressList.names[2],
                            street: addressList.names[3]
                        },
                        homeAddress:{
                            province:  homeAddressList.names[0],
                            city: homeAddressList.names[1],
                            district: homeAddressList.names[2],
                            street: homeAddressList.names[3]
                        },
                        workAddress:{
                            province:  workAddressList.names[0],
                            city: workAddressList.names[1],
                            district: workAddressList.names[2],
                            street: workAddressList.names[3]
                        }
                    });
                    dataModel.updateRemote("${contextRoot}/patient/updatePatient", {
                        data: {patientJsonData: JSON.stringify(values) },
                        success: function (data) {

                            if(data.successFlg){
                                win.parent.$.ligerDialog.success('修改成功');
                            }else{
                                win.parent.$.ligerDialog.error('修改失败');
                            }
                            win.parent.patientDialogRefresh();
                            dialog.close();
                        }
                    })
                });
                //重置密码
                patientInfo.$resetPassword.click(function () {
                    var patientIdCardNo = self.$form.Fields.idCardNo.getValue();
                    $.ligerDialog.confirm('确认重置密码？<br>如果是请点击确认按钮，否则请点击取消。', function (yes) {
                        if (yes) {
                            var dataModel = $.DataModel.init();
                            dataModel.updateRemote("${contextRoot}/patient/resetPass", {
                                data: {idCardNo: patientIdCardNo},
                                success: function (data) {
                                    if (data.successFlg) {
                                        win.parent.$.ligerDialog.success('密码修改成功');
                                    } else {
                                        win.parent.$.ligerDialog.error('密码修改失败');
                                    }
                                    dialog.close();
                                }
                            })
                        }
                    });


                });
                //关闭dailog的方法
                patientInfo.$cancelBtn.click(function(){
                    dialog.close();
                })
            }
        };
        /* *************************** 模块初始化结束 ***************************** */

        /* *************************** 页面初始化 **************************** */
        pageInit();
        /* *************************** 页面初始化结束 **************************** */


    })(jQuery, window);
</script>