var catId = "";
var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function() {
        this.itemInit();
        this.eventInit();
        this.otherInit();
    },
    itemInit:function(){
        $(document).on("keyup", ".uPayN", function() {
            $(this).number(true);
        });
        if ($(".uPayN").length > 0) {
            $(".uPayN").number(true);
        }
        //상단 표기 상태 변경
        cordova.plugins.finPlugin.getUseType("", function(res){

            if(0 < res.length){
                if('연동형' == res){
                    $("#hd_title").html("<a id='backBtn' onclick='app.backbuttonAction();'><h3 class='btnPre'><span class='blind'>이전</span></h3></a><h3 class='title'>알리페이 결제</h3><h3 id='battery' class='battery'><span></span></h3>");
                }else {
                }
            }
        }, null);
        if(isIphone()){
          cordova.plugins.finPlugin.getAliCost("", function(res){
              $("#uSum").val(res);
          }, function(error){
              alert(error);
          });
        }else {
          app.setAliManualData();
        }
    },
    eventInit:function(){
       
    },
    otherInit:function(){
        document.addEventListener("backbutton", app.backbuttonAction, false);
    },
    backbuttonAction:function(){
        var isPopupClose = false;
        if ($("#layerPopup.Popup12").is(':visible')){
            isPopupClose = true;
            app.hideWarnningPopup();
        }
        if(!isPopupClose){
            cordova.plugins.finPlugin.backButton("", null, null);
        }
    },
    setAliManualData:function(){
        cordova.plugins.finPlugin.getAliManualAmount("", function(manualData){
            var dataObject = JSON.parse(manualData);
            $("#uSum").val(dataObject.price);
            catId = dataObject.catId;
        }, function(error){
            showToastMessage(error);
        });
    },
    changeAliPayNum:function(uniqNum){
        cordova.plugins.finPlugin.changeAliPayNum(uniqNum, null, null);
    },
    hideWarnningPopup:function(){
        $('#layerPopup.Popup12').hide();
    },
    requestPayment:function(){
        if (isClickedButton()) {
            return;
        }
        var price = replaceAll($("#uSum").val(),",",""); // 판매금액 입력값
        
        if(isIphone()){
            if(price == ""){
                $("#layerPopup.Popup12").show();
            }else{
                var data = {
                    "PRICE":$("#uSum").val(),
                    "ALINUM":$("#uniqueNumber").val()
                };
                cordova.plugins.finPlugin.reqConfirmAliSelfInput(data, function(res){
                    alert(res);
                }, function(error){
                    alert(error);
                });
            }
        }else {
            if(price == "" || catId == ""){
                $("#layerPopup.Popup12").show();
            }else{
                
                var sendData = {
                    "price":price,
                    "catId":catId,
                    "aliUniqNum":$("#uniqueNumber").val()
                };
                cordova.plugins.finPlugin.requestPayment(sendData,function(dbId){
                    open_PopupError('결제가 완료 되었습니다.');
                    RECEIPT_ALI.DATA.RECEIPT_ID = dbId;
                    cordova.plugins.finPlugin.moveStack(RECEIPT_ALI.DATA,null, null);
                }, function(error){
                    if(error) open_PopupError(error);
                    else open_PopupError('결제 요청에 실패 하였습니다.');
                });
            }
        }
        
    }
};

app.initialize();

