function encodeUnicode(str) {
    var res = [];
    for (var i = 0; i < str.length; i++) {
        res[i] = ("00" + str.charCodeAt(i).toString(16)).slice(-4);
    }
    return  "\\u" + res.join("\\u");
}


function sendData(d) {
    console.log(d);
    $.ajax({
        type:"get",
        crossDomain:true,
        url:"http://localhost:8320/api",
        data: "data=" + d,
        success:function (ret) {
            console.log(ret);
        },
        error:function(){
            console.error("get error");
        }
    });
}


function postCMD(cmd,UserID,UserPsw,ItemName,Coast,Pay,Change,GuestName,Msg,Self,free,PatNo) {
    var d = {}; // new object();
    d.CMD = cmd;
    d.UserID = UserID;
    d.UserPsw = UserPsw;
    d.ItemName = encodeUnicode(ItemName);
    d.Coast = Coast;
    d.Pay = Pay;
    d.Change = Change;
    d.GuestName=encodeUnicode(GuestName);
    d.Msg = encodeUnicode(Msg);
    d.Self = Self;
    d.free = free;
    d.PatNo = PatNo;
    sendData(JSON.stringify(d));
    //console.log(d);
    //console.log(JSON.stringify(d));
}


/**
 * 推送价格信息
 * @param data 应收金额
 * @Pay Pay 实收金额
 * @param changeAmount
 * @param message
 * @param patientId
 * @param name
 */
function sendPeiceMessage(data,pay,changeAmount,message,patientId,name) {
    $.ajax({
        type: "GET",
        url: '/thmz/getByPatientId?patientId=' + patientId,
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
        success: function (res) {
            if (res == '401' || res == 401) {
                window.location.href = '/thmz/login/view'
                return;
            }
            if (res.code == 0) {
                if (res.data != null) {
                    var gender= res.data.sex==1?"先生":"女士";
                    var age=$("#age").val();
                    if(age==undefined || age==null){
                        age = res.data.age;
                    }
                    if(age<16){
                        gender="小朋友";
                    }
                    message=name+gender+message;
                    postCMD("price",localStorage.getItem("userID"),'000000','收费项目',parseFloat(data).toFixed(2),parseFloat(pay).toFixed(2),parseFloat(changeAmount).toFixed(2), name,message,parseFloat(data).toFixed(2),parseFloat(0).toFixed(2),res.data.phoneNo);
                }
            }
        }
    });
}