//착신전환
$(()=>{
    //착신전화 등록 버튼
    $(".reg_forw").on("click", function(){
        
        let time_type = $('input[name="time"]:checked').val();
        let start_time = $("#forward_start_time").val();
        let stop_time = $("#forward_end_time").val();
        let type = $('input[name="condition"]:checked').val();
        let to = $(".forw_num").val();
        if(type == "0"){
            popAlert("착신전환 동작 조건 설정을\n선택해 주세요.");
            return;
        }
        // 매일이면 시간 강제입력 (어자피 사용하지 않음)
        if ( time_type == 0 ) {
            if (start_time == "" || stop_time == "" ) {
                start_time = "09:00";
                stop_time = "18:00";
            }
        } else if ( time_type == 4 && (start_time == "" || stop_time == "" ) ) {
            popAlert("착신전환 동작시간을 입력해주세요.");
            return;
        }
        console.log('reg_forw','착신전화!' + time_type + "::" + start_time + "::" + stop_time + "::" + type + "::" + to);
        if(!phonenumberValidation(to)){
            popAlert("착신전환 등록전화번호를 확인해 주세요.");
            return;
        }
        let params ={
            url : `https://${serverData.cust_con_pbx_ip}:${serverData.cust_con_port}/ipxapi/v1/users/${userData.id}/cfwd?to=${to}&start_time=${start_time}&type=${type}&time_type=${time_type}&stop_time=${stop_time}`
            ,token : window.localStorage.getArray('userData').token
        };
        requestToCm(params , (response) => {
            console.log('regist forward', 'params', JSON.stringify(params), 'response',  JSON.stringify(response));
            if(response.data.error.message == 'OK'){
                popAlert("착신전환이 등록 되었습니다.");
                getForward(false);
            }else{
                popAlert(response.data.error.message);
            }
        });
    });

    //착신전화 해제 버튼
    $(".cancel_forw").on("click", function(){
        
        let time_type = $('input[name="time"]:checked').val();
        let start_time = $("#forward_start_time").val();
        let stop_time = $("#forward_end_time").val();
        let type = "0";
        let to = $(".forw_num").val();

        let params ={
            url : `https://${serverData.cust_con_pbx_ip}:${serverData.cust_con_port}/ipxapi/v1/users/${userData.id}/cfwd?to=${to}&start_time=${start_time}&type=${type}&time_type=${time_type}&stop_time=${stop_time}`
            ,token : window.localStorage.getArray('userData').token
        };
        requestToCm(params , (response) => {
            console.log('release forward', 'params', JSON.stringify(params), 'response',  JSON.stringify(response));
            if(response.data.error.message == 'OK'){
                popAlert("착신전환이 해제 되었습니다.");
                getForward(false);
            }else{
                popAlert(response.data.error.message);
            }
        });
    });
});

function getForward(first){
    console.log('getForward','first ::',first);

    let params ={
        url : `https://${serverData.cust_con_pbx_ip}:${serverData.cust_con_port}/ipxapi/v1/users/${userData.id}/cfwd`
        ,token : window.localStorage.getArray('userData').token
        ,method : 'GET'
    };
    requestToCm(params , (response) => {
        console.log('get forward', 'params', JSON.stringify(params), 'response',  JSON.stringify(response));
        if(response.data.error){
            popAlert(response.data.error.message);
        }else{
            console.log('getForward',"jsonData:"+JSON.stringify(response.data));
            window.localStorage.setArray("forwardData", response.data.data);//저장
            printForward(first);
        }
    });
}

function printForward(first){

    //alert(first);
    let forwardData = window.localStorage.getArray("forwardData");
    //등록/해제 여부
    if(forwardData.type == 0){
        $(".msg_box").addClass('off');

    }else{
        if(forwardData.to == "8888"){
            $(".con_value").text('VMS 착신 중입니다.');
            $(".msg_box").css("background-color","#ef6964");
        }
        $(".msg_box").removeClass('off');
        $(".con_num").html(forwardData.to);
    }
    //동작시간
    switch (forwardData.time_type) {//1,2,3,4
        case "0":
            if(first){
                $("#all").prop("checked", true);
                $("#spcf").prop("checked", false);
            }else{
                $("#all").prop("checked", true).checkboxradio("refresh");
                $("#spcf").prop("checked", false).checkboxradio("refresh");
            }

            break;
        default:
            if(first){
                $("#all").prop("checked", false);
                $("#spcf").prop("checked", true);
            }else{
                $("#all").prop("checked", false).checkboxradio("refresh");
                $("#spcf").prop("checked", true).checkboxradio("refresh");
            }
    }
    $("#forward_start_time").val(forwardData.start_time);
    $("#forward_end_time").val(forwardData.stop_time);
    //동작 조건
    //$("input[name=condition]").prop("checked", false);

    //alert("forwardData.type"+forwardData.type);
    if(first){
        $("input[name=condition]").prop("checked", false);
        $("input:radio[name='condition']:radio[value='"+forwardData.type+"']").prop("checked",true);
    }else{
        $("input[name=condition]").prop("checked", false).checkboxradio("refresh");
        $("input:radio[name='condition']:radio[value='"+forwardData.type+"']").prop("checked",true).checkboxradio("refresh");
    }
    //전화번호
    $(".forw_num").val(forwardData.to);
}