/**
 * Created by KangHaeSeok on 2017-07-26.
 */

sc.newCoupleSaveModalOpen = function () {
    $('.ListModalBack').fadeIn();
    $('.ver1').fadeIn();
    $('.list_closebtn').click(function(){
        $('.ListModalBack').fadeOut();
        $('.ver1').fadeOut();
    });
};

sc.originCoupleSaveModalOpen = function () {
    $('.ListModalBack').fadeIn();
    $('.ver2').fadeIn();
    $('.list_closebtn').click(function(){
        $('.ListModalBack').fadeOut();
        $('.ver2').fadeOut();
    });
};

// 커플 저장하기 기능
// 커플 카테고리 타입을 판별하는 변수를 인자로 받는다
sc.newCoupleSave = function (coupleSaveType) {

    var boyFindSelectedFabric = sc.boyFabricList.find(function (fabric) { return fabric > 0; });
    var girlFindSelectedFabric = sc.boyFabricList.find(function (fabric) { return fabric > 0; });

    if ((boyFindSelectedFabric === undefined) || (girlFindSelectedFabric === undefined)){
        $.alert({title: 'Alert!', content: "의상을 선택해주세요.", useBootstrap: false});
        return;
    }

    $.confirm({
        title: 'Confirm!',
        content: '현재의 코디를 저장하시겠습니까?',
        useBootstrap: false,
        buttons: {
            confirm: function () {

                sc.JacketVestcheck();
                var currentCoupleIndex = sc.getSelectedCoupleIndex();
                var newCoupleName = $("#newCoupleName").val();

                $.ajax({
                    url: '/simulation/custom_admin/new_couple_save',
                    data: {
                      season: sc.season,
                      style: sc.style,
                      boy_jacket_vest_on: sc.boyJacketVestOn,
                      girl_jacket_vest_on: sc.girlJacketVestOn,
                      boy_fabric_index_list: sc.boyFabricList,
                      girl_fabric_index_list: sc.girlFabricList,
                      couple_save_type: coupleSaveType,
                      current_couple_id: currentCoupleIndex,
                      new_couple_name: newCoupleName
                    },
                    beforeSend: function() {
                        var offset = $('.list_savebtn').offset();
                        $('.overlay').show();
                    },
                    complete: function() {
                        $('.overlay').hide();
                    },
                    success: function(data) {
                        console.log(data.success);
                        sc.coordiCloseBtnEvent();
                    },
                    error: function() {
                        alert('error');
                    }
                });

            },
            cancel: function () {
            }
        }
    });

};


//기존 커플 저장
sc.originCoupleUpdate = function () {

    var boyFindSelectedFabric = sc.boyFabricList.find(function (fabric) { return fabric > 0; });
    var girlFindSelectedFabric = sc.boyFabricList.find(function (fabric) { return fabric > 0; });

    if ((boyFindSelectedFabric === undefined) || (girlFindSelectedFabric === undefined)){
        $.alert({title: 'Alert!', content: "의상을 선택해주세요.", useBootstrap: false});
        return;
    }

    $.confirm({
        title: 'Confirm!',
        content: '현재의 코디로 수정하시겠습니까?',
        useBootstrap: false,
        buttons: {
            confirm: function () {

                sc.JacketVestcheck();
                var currentCoupleIndex = sc.getSelectedCoupleIndex();
                var originCoupleName = $("#originCoupleName").val();

                $.ajax({
                    url: '/simulation/custom_admin/origin_couple_update',
                    data: {
                      season: sc.season,
                      style: sc.style,
                      boy_jacket_vest_on: sc.boyJacketVestOn,
                      girl_jacket_vest_on: sc.girlJacketVestOn,
                      boy_fabric_index_list: sc.boyFabricList,
                      girl_fabric_index_list: sc.girlFabricList,
                      current_couple_id: currentCoupleIndex,
                      origin_couple_name: originCoupleName
                    },
                    beforeSend: function() {
                        var offset = $('.list_savebtn').offset();
                        $('.overlay').show();
                    },
                    complete: function() {
                        $('.overlay').hide();
                    },
                    success: function(data) {
                        // console.log(data.success);
                        // sc.coordiCloseBtnEvent();
                        $(".list_closebtn").click();
                        sc.coordiCloseBtnEvent();
                    },
                    error: function() {
                        $.alert({title: 'Alert!', content: "error", useBootstrap: false});
                    }
                });

            },
            cancel: function () {
            }
        }
    });

};


//남자 여자 자켓조끼 다 같이 입었는지 판별하는 기능
sc.JacketVestcheck = function () {
    if( (sc.jacketBoyFabric != 0) && (sc.vestBoyFabric != 0) ){
        sc.boyJacketVestOn = true;
    }else{
        sc.boyJacketVestOn = false;
    }

    if( (sc.jacketGirlFabric != 0) && (sc.vestGirlFabric != 0) ){
        sc.girlJacketVestOn = true;
    }else{
        sc.girlJacketVestOn = false;
    }

};