/**
 * Neuron Frame Work
 * for Golem OEE MES
 * www.neuron.com.pl

MES_PO_constructor
 - make_matrix_click_men
 - set_toolbar
 - cmd_accept  - zatwierdza polecenia
 - przyciski akcji
 - sbtn_action - dekoder poleceń
 - act_* - metod akcji
 - info_task_product
 - komunikaty
 - info_sv
 - view_sr
 - info_trend72
 - get_id_list
 - get_product_list
 - get_ptw_info
 - view_ev_form
 -


 */


function MES_PO_constructor(parent) {

    var that = this;
    this.sbtn_state=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
    this.sbtn_btn=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
    /*
    0 - komentarz
    1 - szybki komentarz
    2 - status
    3 - andon
    4 - operator
    5 - task operacje
    6 - task braki
    7 - task produkt
    11 - inspekcja
    18 - view SR
    19 - task info
    20 - EV single
    */
    this.ev_day_limit=7;
    this.st_dialog=0;

    this.make_matrix_click_menu=function(dx,dy){
    var nr= 0,x= 0,y=0;

    for (var r = 0; r < mes_data.matrix.r_count ; ++r) {
        for (var c = 0; c < mes_data.matrix.c_count ; ++c) {
            x=mes_data.matrix.ppx [ c ];
            y=mes_data.matrix.ppy [ r ];
            mes_data.matrix._pc++;
            if( mes_data.matrix._pc <= mes_data.matrix.base_count  ) {
                nr=mes_data.matrix._pc-1;
                // this.matrix_left_panel_panels(nr,x+20,y+180, mes_data.crr == nr   );
                s = htmlts.div_btn(nr, x+20+6, y+180+6,  mes_data.matrix.pwidth, mes_data.matrix.pheight , 'pomtbcl');
                $(parent).append( s  );
            }
        }
    }// end loop
    $('.pomtbcl').on(clickEvent, function (e) {
        e.stopPropagation();
        e.preventDefault();
        var data = parseFloat( $(this).attr('data-button'));

       nApp.goEvent(120,data);
    });
    };

    this.set_toolbar_simple= function() {
        nApp.view.tb_addContainer(200,'nfw-user-container' );
    }

    this.set_toolbar=function( mlist,auth,info,st72,oee ){

        if(mlist) {

            if (!nApp.mes_set.use_sv) {
                nApp.view.tb_addBtn(false, 'btn-orange', 'lc_ico', lang[129], 'left_box_toggle')
            }
            nApp.view.tb_addSeparator(10);
        }
        if(auth){

            nApp.view.tb_addContainer(200,'nfw-user-container' );
        }

        nApp.view.tb_addBtn(true,'btn-orange','rc_ico','OEE, Status','right_box_toggle',1 );
        nApp.view.tb_addSeparatorR(1);
        nApp.view.tb_addBtn(true,'btn-blue','mdi-view-list',lang[130],'call', function(){ that.view_ev_form(2, mes_data.crr_sv  ,102); }  );
        nApp.view.tb_addSeparatorR(1);

        if(info){
        nApp.view.tb_addBtn(true,'btn-blue','mdi-information-outline',lang[174],'call', function(){   that.info_sv()   });
        nApp.view.tb_addSeparatorR(1);
        nApp.view.tb_addBtn(true,'btn-blue','mdi-playlist-minus',lang[172],'call', function(){   that.info_trend72()   });
        nApp.view.tb_addSeparatorR(1);
        nApp.view.tb_addBtn(true,'btn-blue','mdi-view-list','SR','call', function(){   that.view_sr(1,0)   });
           nApp.view.tb_addSeparatorR(1);
           nApp.view.tb_addBtn(true, 'btn-red', 'mdi-wrench', 'CMMS', 'call', function () { that.cmms_menu()   });
        }

    };

    this.config_toolbar=function(){
        if (!mes_data.grupa_cmms_use){            $('#tb_id7').hide(); }
    }

    //CMMS
    this.cmms_menu=function(){
    var dst=[];
    var ev_ico=['mdi-wrench','mdi-elevator', 'mdi-comment-alert-outline','mdi-bell-ring-outline','mdi-security','mdi-tooltip-outline-plus'];

     function _apmwin(caption,cname,cwidth){
         var dm = new Kit_dialog_constructor( 1000, 600  ,1,0,true,true);
         dm.draw();
         dm.addHeader(caption);
         dm.content('<div id="cev_pm_table"></div>',false  );
         dm.showDialog(0);

         dst = new Dataset_table_constructor('#cev_pm_table',nApp.cfgi.theme_style);
         dst.colname=cname;
         dst.colwidth=cwidth;
         dst.height= dm.height - dm.hheight -  170;
         dst.make();

     }


        var cmmsid= mes_data.cfg_cmms_id(mes_data.crr );
        if (cmmsid==0){
            kit_note('Nadzorca nie współpracuje z CMMS');
        } else {
            var mn = new Kit_popup_menu_constructor();
            mn.addBtn(true, 'Podręczne kontakty UR', 'btn-black', 'mdi-account', 1, true);
            mn.addBtn(true, 'Harmonogram PM na 30 dni', 'btn-blue', 'mdi-calendar', 2, true);
            mn.addBtn(true, 'Historia 30 dni', 'btn-orange', 'mdi-wrench', 3, true);
            mn.addBtn(true, 'Zgłoszenie awarii', 'btn-red', 'mdi-bell-ring-outline', 4, mes_data.cfg_cmms_pz(mes_data.crr ));
            mn.make(function (nr) {

                if (nr == 1) { //// kontakty
                    var sdata={};
                    sdata['q1']= nApp.auth.id ;
                    nfw_ajax('mes_get_cmms_phone',sdata, function(jdata) {

                        $('#mmlist').html(jdata['r1'] )
                        kit_dialog_noteb('Podręczne kontakty UR',jdata['r1'])

                    });
                }

                if (nr == 2) { //////////  PM
                    _apmwin('Harmonogram PM na 30 dni ( + zaległe )',['id','Data','>>','Zadanie','Odpowiedzialny'],[60,140,100,300,150]);
                    var s=''; var sdata={};
                    sdata['q1']= cmmsid ;

                    $.ajax({  'url':"mes_get_cmms_pm",  'dataType':'json', 'type':"GET",
                        data: JSON.stringify( sdata ),
                        success: function(jdata) {

                            $.each(jdata, function (i, item) {
                                s+='<tr>';
                                s+=dst.td( jdata[i].r0 );
                                s+=dst.td( jdata[i].r3 );
                                s+=dst.td( jdata[i].r4 );
                                s+=dst.td( jdata[i].r2 );
                                s+=dst.td( jdata[i].r5 );
                                s+='</tr>';
                            } );
                            dst.load(s)
                        },
                        error: function(){   }
                    });

                }
                if (nr == 3) { //////// AM
                    _apmwin('Historia 30 dni',['id','ico','OK','Zgłoszono','Rozpoczęto','Zakończono','Zdarzenie','Odpowiedzialny'],[60,50,40,150,150,150,300,150]);

                    var s=''; var sdata={};
                    sdata['q1']= cmmsid ;

                    $.ajax({  'url':"mes_get_cmms_am",  'dataType':'json', 'type':"GET",
                        data: JSON.stringify( sdata ),
                        success: function(jdata) {

                            $.each(jdata, function (i, item) {
                                s+='<tr>';
                                s+=dst.td( jdata[i].r0 );
                                s+=dst.td( '<i class="mdi i32 '+ev_ico[jdata[i].r1] +'"></i>'  );
                                //s+=dst.td( jdata[i].r2 );
                                s+=dst.tdc(  '', jdata[i].r2==1 ? '#CCEE64':'#FFA6A6'  );
                                s+=dst.td( jdata[i].r3 );
                                s+=dst.td( jdata[i].r4 );
                                s+=dst.td( jdata[i].r5 );
                                s+=dst.td( jdata[i].r6 );
                                s+=dst.td( jdata[i].r7 );
                                s+='</tr>';
                            } );
                            dst.load(s)
                        },
                        error: function(){   }
                    });

                }
                if (nr == 4) { //////  ZA
                    nApp.auth.loginU(9, function() {


                        var dm = new Kit_dialog_constructor( 550, 400  ,3,1,true,true);
                        dm.btn_call_close=function(){ dm.aFree();  };
                        dm.onTop=true;
                        dm.btn_close_after=false;
                        dm.draw();
                        dm.addHeader('Zgłoś awarię');
                        dm.btn_call_ok=function() {
                            ft.getval();

                            if (ft.c_value_txt[2]!='') {
                                var sdata = {};
                                sdata['q0'] = cmmsid;
                                sdata['q1'] = nApp.auth.id;
                                sdata['q3'] = nApp.auth.user ;
                                sdata['q10'] = ft.c_value_txt[2];
                                sdata['q11'] = ft.c_value_i[3];
                                sdata['q12'] = ft.c_value_i[4];
                                sdata['q13'] = ft.c_value_txt[5];
                                sdata['q14'] = ft.c_value_txt[6];

                                $.ajax({
                                    'url': "mes_set_cmms_za",
                                    'dataType': 'html',
                                    "contentType": "application/json",
                                    'type': "GET",
                                    data: encodeURIComponent(JSON.stringify(sdata)),
                                    success: function (jdata) {

                                        nApp.goEvent(100, 0);

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

                            } else {  kit_note( 'Nazwa jest wymagana' )  }
                            dm.aFree();
                        };
                        var ft=new Kit_form_constructor('tf');
                        ft.addTextRO(1,'Obiekt', mes_data.cfg_name( mes_data.crr )  );
                        ft.addText(2,'Zdarzenie',400);
                            ft.addCombo(3,'Priorytet',[256,257,258],2 );
                            ft.addCombo(4,'Potrzebny',[259,260,261,262],0 );
                            ft.addText(5,'Powiadomiono',400);
                            ft.addMemo(6,'Opis','',42,3);
                        dm.content( '<div class="mes_auth_marker"></div><div style="position: relative" >'+ft.htmlString()+'</div>' ,false );

                        ft.activate();
                        dm.showDialog(0);
                    });
                }

            });
        }
    }



    // formularze głównych akcji

    this._send_cmd=function(cmd,p1,comment,p2,p3 ){
        var that =this;
        var nrd=0;
        if(cmd==30){nrd=3}//andon
        if (cmd==1 || cmd==2 ){nrd=2}//status
        var sdata={};
        sdata['q1']=mes_data.crr_sv ; //nrsv
        sdata['g2']=cmd;
        sdata['q3']=p1;
        sdata['q4']=p2;
        sdata['q5']=p3;
        sdata['q6']= encodeURIComponent(comment);
        sdata['q7']= nApp.auth.key;
        sdata['q8']= nApp.auth.id;
        if(nrd!=0) {that.sbtn_disable(nrd,true)}

        $.ajax({  'url':"mes_send_cmd",  'dataType':'json', 'type':"POST", 'global':false,
            data: JSON.stringify( sdata ),
            success: function(jdata) {
                mes_data.getData();
                nApp.core_rtimer_reset();
                if(nrd!=0) {that.sbtn_disable(nrd,false)}

            },
            complete: function(data) {  }
        });

    };

    this.cmd_accept=function(cmd,par,title  ){

        function __dok(){
          that._send_cmd(cmd,par,memo.val(),0,0);
            dm.aFree();
        }

        function __fc (){
            lckb=true;
            $( "#svcmdcmt").blur();
            setTimeout(function(){
                that.get_id_list(1,  mes_data.crr_sv,  function(aid,an) {
                    memo.val( an );
                    lckb=false;
                    memo.focus()
                })
            }, 200)
        }

        var that=this;
        var memo= '';
        var lckb=false;
        $("html, body").animate({ scrollTop: 0 }, 0);
        var dm = new Kit_dialog_constructor( 450, 220  ,3,0,true,true);

        dm.btn_close_after=false;
        dm.btn_call_ok = __dok;
        dm.btn_call_close=function(){dm.aFree()};

        dm.draw();
        dm.addHeader(title);
        var sx='<div class="mes_auth_marker"></div><br><textarea id="svcmdcmt" cols="40" rows="3" style="width: 100%;" ></textarea> ';
        dm.addButtonP('<div class=" mdi i32 mdi-comment-alert-outline" ></div>','btn-blue',80,__fc  );

        dm.content(sx,false  );
        dm.showDialog(0);
        memo=$( "#svcmdcmt");

        setTimeout(function() {
            memo.addClass('inputtxt').focus().on('blur', function (event) {
                if (!lckb) {
                    memo.focus();
                    lckb=true;
                }
            });
        },50);

        BindClick(memo, function (event){ memo.focus() ;  } );
    };


    //  przyciski akcji dla PO
    this.sbtn_comment=function(x,y){
        this._sbtn_sbtn(0,x,y,100,40,'btn-black wbr',lang[165]);
    };

    this.sbtn_commentF=function(x,y){
        this._sbtn_sbtn(1,x,y,100,40,'btn-black wbr',lang[167]);
    };

    this.sbtn_CFEV=function(x,y){
        this._sbtn_sbtn(0,x,y,100,80,'btn-black btnc',htmlts.ico('i48','mdi-comment-outline'));
        this._sbtn_sbtn(1,x+130,y,100,80,'btn-black btnc',htmlts.ico('i40','mdi-playlist-play')+htmlts.ico('i40','mdi-comment-outline'));
        this._sbtn_sbtn(10,x+260,y,80,80,'btn-blue btnc',htmlts.ico('i40','mdi-message-text-outline'));
       // this._sbtn_sbtn(20,x+280,y,100,80,'btn-blue btnc',htmlts.ico('i48','mdi-view-list'));
    };

    this.sbtn_status=function(x,y){
        this._sbtn_sbtn(2,x,y,100,60,'btn-black','Status');
    };
    this.sbtn_andon=function(x,y){
        this._sbtn_sbtn(3,x,y,100,60,'btn-black',lang[6]);
    };
    this.sbtn_op=function(x,y){
        this._sbtn_sbtn(4,x,y,100,60,'btn-black',lang[6]);
    };

    this.sbtn_taskbt=function(x,y){
        this._sbtn_sbtn(5,x,y,100,60,'btn-black',lang[178]);
        this._sbtn_sbtn(6,x,y+70,100,60,'btn-red',lang[177]);
        this._sbtn_sbtn(7,x,y+140,100,60,'btn-yellow',lang[176]);

        if (this.st_dialog==0)  {this._sbtn_sbtn(19,x,y+220,100,40,'btn-blue',htmlts.ico('','mdi-information-outline')+' TaskInfo');}
    };


    this.sbtn_tinfo=function(x,y){
        this._sbtn_sbtn(19,x,y,100,40,'btn-blue',htmlts.ico('','mdi-information-outline')+' TaskInfo');
    };

    this.sbtn_ev=function(x,y){
        this._sbtn_sbtn(20,x,y,100,40,'btn-blue',htmlts.ico('','mdi-menu')+' EV');
    };
    this.sbtn_insp=function(x,y){
        this._sbtn_sbtn(11,x,y,100,40,'btn-orange',htmlts.ico('','mdi-magnify')+' INSP');
    };

    this.sbtn_reconfig=function(){
         function _bv(nr,state){
             if (that.sbtn_btn[nr]!=0){
                 if (state) {that.sbtn_btn[nr].show() } else {that.sbtn_btn[nr].hide()}
             }
         }
       var b=true;
       _bv(2,mes_data.cfg_stat_use_op( mes_data.crr ));
       _bv(3,mes_data.cfg_a_mode( mes_data.crr )==1 || mes_data.cfg_a_mode( mes_data.crr )==2  );
        ///kit_note(mes_data.cfg_a_mode( mes_data.crr ))
       _bv(5,mes_data.cfg_task_use(mes_data.crr));
       _bv(7,mes_data.cfg_task_manual_product(mes_data.crr) );

       _bv(19,mes_data.task_id( mes_data.crr )!=0);
       _bv(6,mes_data.task_id( mes_data.crr )!=0);
       _bv(11,mes_data.cfg_inspekcja_use1 ( mes_data.crr ) );

    };

    this.sbtn_disable=function(nr,state){
      if (this.sbtn_btn[nr]!=0) {
          this.sbtn_btn[nr].prop('disabled', state);
      }
    };

    this._sbtn_sbtn=function(anr,x,y,w,h,bclass,caption){
        if (this.sbtn_state[anr]==0) {

            if (nApp.cfgi.theme_style!=0)  {bclass=bclass+'  wbr'}

            $(parent).append(htmlts.btn_did('sbtn'+anr, anr, x, y, w, h, caption, bclass));
            this.sbtn_state[anr] = 1;
            this.sbtn_btn[anr] = $('#sbtn'+anr).on(clickEvent, function (e) {
                e.stopPropagation(); e.preventDefault();
                that.sbtn_action(parseFloat($(this).attr('data-button')))
            });
        }
    };

    this.sbtn_action=function(nr){
        var crr=mes_data.crr;

        if(nr==0){
            nApp.auth.loginU(3, function() {   that.cmd_accept(0, 0, lang[165])   });
        }
        if(nr==1){
            nApp.auth.loginU(3, function() {
                that.get_id_list(1,  mes_data.crr_sv,  function(aid,an) {  that._send_cmd(0, 0, an, 0, 0);  })
            })
        }
        if(nr==10){
              that.komunikaty();
        }

        if(nr==2){//status
            nApp.auth.loginU(1, function() {
                if ( mes_data.cfg_stat_use_sr(crr) ) {// status rozszerzony
                    that.act_statusR();
                } else { // status podstawowy
                    that.act_status();
                }
            });
        }

        if(nr==3){ //ANDON

            nApp.auth.login( function(){
                if(mes_data.andon_nr(crr)!=0 ){
                    if(nApp.auth.get_perm(16)    ){  that.cmd_accept(30, 0, lang[126]) } else { nApp.auth.nop() }
                }else {
                    if(nApp.auth.get_perm(15)    ){ that.act_andon()} else { nApp.auth.nop() }
                }

            });
        }
        if(nr==4){

            nApp.auth.login( function(){
                if(nApp.auth.get_perm(5) || nApp.auth.get_perm(6) || nApp.auth.get_perm(7)  ){
                    that.act_operators();
                } else { nApp.auth.nop() }
            });
        }
        if(nr==5){
            that.act_task_menu() ;
        }
        if(nr==6){ //braki
          nApp.auth.loginU(13, function() {
              if (mes_data.cfg_kb_use(crr)) {
                  that.act_defect_k();
              } else {
                  kit_dialog_calc(0, 0, lang[177], function (vx) {
                      if (vx != 0) {
                          //that._send_cmd(35, 0, '', vx, 0);
                          kit_dialog_yes_no( lang[179],'<h1>  [ '+vx+' ]</h1>', function(){   that._send_cmd(35, 0, '', vx, 0)      }  )
                      }
                  })
              }
          });
        }
        if(nr==7){ //product manual
            nApp.auth.loginU(13, function() {
                    kit_dialog_calc(0, 0, lang[176], function (vx) {
                        if (vx != 0) {
                            //that._send_cmd(35, 0, '', vx, 0);
                            kit_dialog_yes_no( lang[180],'<h1>  [ '+vx+' ]</h1>', function(){   that._send_cmd(40, 0, '', vx, 0)      }  )
                        }
                    })

            });
        }


        if(nr==19){ this.info_task_product();  }
        if(nr==20){ this.view_ev_form(0, mes_data.crr_sv,100 );  }
        if(nr==11){ this.inspekcja()  }

    };

    // action module
    this._act_stb=function(snr,asr){
      var s='';
        for (var j = 0; j < 7 ; ++j) {
           var n=6-j;
           var btnc='btn_st_'+that.st_dialog+' state_mark';
            if ( snr == n ) {  if(asr){ btnc='btn_st_as_'+that.st_dialog+' state_mark'} else{ btnc='btn_st_a_'+that.st_dialog}}
            //if ( snr == n ) {   btnc='btn_st_a_'+that.st_dialog}
           s+='<button class="btn '+btnc+' " style="background-color: '+state_colors[n]+' " data-button="'+n+'">'+htmlts.ico('i40',state_ico[n])+' '+lang[80+n]+ '  </button>'
        }
        return s;
    };

    this.act_status=function(){
        var stdh=570;
        if (this.st_dialog==1){ stdh=520 }
        var dm = new Kit_dialog_constructor( 350, stdh  ,4,0,true,true);
        dm.draw();
        dm.addHeader('Status');
        var sx=this._act_stb( mes_data.stat_nr(mes_data.crr,false) );
        dm.content('<div class="mes_auth_marker"></div>'+sx,false  );
        dm.showDialog(0);
        $('.state_mark').on('click touchstart',function(e){
            var data = $.parseJSON($(this).attr('data-button'));
            e.stopPropagation(); e.preventDefault();
            dm.aFree();
            that.cmd_accept(1, data, lang[170]+' '+lang[80+data]+' ?' )
        });

    };
    this.act_statusR=function(){
      var crr=mes_data.crr;

        function _srbox(nrp){
            var scx='<table>';
            for (var j=0; j< mes_data.cfg_stat_sr_count(crr)  ; ++j ){
                if (mes_data.cfg_srbase(crr,j)  ==nrp)
                {
                    var btnc='btn_str_'+that.st_dialog+' state_marksr';
                    if( mes_data.stat_r_nr(crr) ==j){btnc='btn_str_as_'+that.st_dialog  }
                    scx+='<button class="btn '+btnc+' " style="background-color: '+state_colors[nrp]+' " data-button="'+j+'">'+htmlts.ico('i40',state_ico[nrp])+' '+mes_data.cfg_srname(mes_data.crr,j)+ '  </button>'
                }
            }

            $('#strrbc').html(scx);

            $('.state_marksrXXXX').on('click touchstart',function(e){
                var datasr = $.parseJSON($(this).attr('data-button'));
                e.stopPropagation(); e.preventDefault();
                dm.aFree();
                that.cmd_accept(2, datasr, lang[170]+' '+mes_data.cfg_srname(mes_data.crr,datasr)+' ?')
            });
            $('.state_marksr').off('click touchstart').on('click', function(e){
                var datasr = $.parseJSON($(this).attr('data-button'));
                //e.stopPropagation(); e.preventDefault();
                dm.aFree();
                that.cmd_accept(2, datasr, lang[170]+' '+mes_data.cfg_srname(mes_data.crr,datasr)+' ?')
            });

        }

        var stdw=770;
        var stdh=630;
        var rbw=300;
        if (this.st_dialog==1){ stdh=520 }
        if (this.st_dialog==2){ stdh=620 ; stdw=660; rbw=220  }  //if (this.st_dialog==2){ stdh=520 ; stdw=580; rbw=220  }

        var dm = new Kit_dialog_constructor( stdw,stdh  ,4,0,true,true);
        dm.draw();
        dm.addHeader('Status');
        var sx=this._act_stb(mes_data.stat_nr(crr),true);
        dm.content('<div class="mes_auth_marker"></div><div style="display: inline-block;width: '+rbw+'px;" >'+sx+'</div><div  class="btn_sr_box"   id="strrbc" >blok B</div>',true );
        dm.showDialog(0);
        _srbox(mes_data.stat_nr(crr));
        $('.state_mark').on('click touchstart',function(e){
            var data = $.parseJSON($(this).attr('data-button'));
            e.stopPropagation(); e.preventDefault();
            _srbox(data)
            dm._sb.refresh();
        });
    };

    this.act_andon=function(){

        function _abtn(anr){
            var an=  mes_data.cfg_a_name( mes_data.crr, anr  );
            sx+='<button class="btn btn_andon" style="background-color: '+andon_colors[anr]+' " data-button="'+anr+'">'+htmlts.ico('i40','mdi-bell-ring-outline') +an+' </button><br>';
        }

        var dm = new Kit_dialog_constructor( 430, 430  ,4,0,true,true);
        dm.draw();
        dm.addHeader(lang[127]);
        var sx='';

        _abtn(1);
        _abtn(2);
        if(mes_data.cfg_a_use_lo( mes_data.crr ))  {_abtn(3)}
        if(mes_data.cfg_a_use_u1( mes_data.crr ))  {_abtn(4)}
        if(mes_data.cfg_a_use_u2( mes_data.crr ))  {_abtn(5)}
        dm.content('<div class="mes_auth_marker"></div>'+sx,false  );
        dm.showDialog(0);
        $('.btn_andon').on('click touchstart',function(e){
            var data = $.parseJSON($(this).attr('data-button'));
            e.stopPropagation(); e.preventDefault();
            dm.aFree();
            that.cmd_accept(30, data, lang[120+data]+' ?')
        });
    };

    this.andon_only_remove=function(nr){
        nApp.auth.login( function(){
            if(nApp.auth.get_perm(16)    ){
                mes_data.setCrr(nr)
                that.cmd_accept(30, 0, lang[126])
            } else { nApp.auth.nop() }

        });
    };

    this.braki_only=function(nr){
        nApp.auth.login( function(){
            if(nApp.auth.get_perm(13)    ){
                mes_data.setCrr(nr)

                that.sbtn_action(6);
            } else { nApp.auth.nop() }

        });
    };
    this.cmt_only=function(nr){
        nApp.auth.login( function(){
            if(nApp.auth.get_perm(3)    ){
                mes_data.setCrr(nr)

                that.sbtn_action(0);
            } else { nApp.auth.nop() }

        });
    };

    this.act_operators=function(){
        var crr=mes_data.crr;
        var mn = new Kit_popup_menu_constructor();


        var ua= nApp.auth.get_perm(5);
        mn.addBtn(true,lang[184],'btn-blue','mdi-account',10, mes_data.op_op_id(crr)!=nApp.auth.id && ua);
        mn.addBtn(true,lang[185],'btn-blue','mdi-account',14, mes_data.op_op_id(crr)!=0 && ua );

        if(mes_data.cfg_op_ur_use(crr)){
            mn.addSep('UR');
            if (mes_data.op_ur_id(crr)==0){
                mn.addBtn(true, lang[186], 'btn-orange', 'mdi-wrench', 12, nApp.auth.get_perm(6) );
            } else {
                mn.addBtn(true, lang[187], 'btn-orange', 'mdi-wrench', 16, nApp.auth.get_perm(6));

            }
        }
        if(mes_data.cfg_op_setup_use(crr)){
            mn.addSep('SetUp');
            if (mes_data.op_set_id(crr)==0){
                mn.addBtn(true, lang[188], 'btn-teal', 'mdi-elevator', 13, nApp.auth.get_perm(7) );
            } else {
                mn.addBtn(true, lang[189], 'btn-teal', 'mdi-elevator', 17, nApp.auth.get_perm(7));

            }
        }
        if(mes_data.cfg_op_use(crr)) {
            mn.addSep('');
            mn.addBtn(true, lang[249], 'btn-black', 'mdi-engine', 1, true);
        }

        mn.make( function(nr){

                 if (nr==1){ that.mojemaszyny() } else
                if (nr!=0){
                   // that._send_cmd(nr,0,'',0,0);

                    var sxn='';
                    if (nr==10){sxn=lang[184]};
                    if (nr==14){sxn=lang[185]};
                    if (nr==12){sxn=lang[186]};
                    if (nr==16){sxn=lang[187]};
                    if (nr==13){sxn=lang[188]};
                    if (nr==17){sxn=lang[189]};

                        that.cmd_accept(nr, 0, sxn);
                }
            }
        )
    };

    this.mojemaszyny=function(){

        function _ret(){
            dm.aFree();
        }

        function _gdata(){
            var sdata={};
            sdata['q1']= nApp.auth.id ;
            nfw_ajax('mes_get_mmlistt',sdata, function(jdata) {

                 $('#mmlist').html(jdata['r1'] )

            });
        }
        var dm = new Kit_dialog_constructor( 450, 500  ,1,0,true,true);
        dm.sall(lang[249],'<div id="mmlist" class="mes_auth_marker"></div>',true);

        _gdata()


    }

    this.act_task_menu=function(){
        var crr=mes_data.crr;
        var tid=mes_data.task_id(crr);
        var mn = new Kit_popup_menu_constructor();

        if (tid==0){//nApp.auth.get_perm(9)

            mn.addBtn(true,lang[191],'btn-blue mes_auth_marker','mdi-barcode',2,true);
            mn.addBtn(true,lang[192],'btn-blue','mdi-beaker-empty-outline',4, mes_data.cfg_task_suspend( crr ) );
            if (mes_data.cfg_task_mode(crr) == 2) {
                mn.addSep('');
                mn.addBtn(true,lang[201],'btn-black ','mdi-barcode',6,true);
            }

        }else{
            mn.addBtn(true,lang[193],'btn-red mes_auth_marker','mdi-barcode',1,true);
            mn.addBtn(true,lang[194],'btn-blue','mdi-beaker-outline',3,mes_data.cfg_task_suspend( crr ));
            mn.addSep('');
            mn.addBtn(true,lang[195],'btn-green','mdi-comment-check-outline',5,true);
            if (mes_data.cfg_task_mode(crr) == 2) {
            mn.addSep('');
            mn.addBtn(true,lang[201],'btn-black','mdi-barcode',6,true);
            }
        }

        mn.make( function(nr){
            var tid= mes_data.task_id( crr );
            switch(nr){
                case 1: { // koniec zlecenia
                    nApp.auth.loginU(9, function() {   that.cmd_accept(21, tid, lang[193]) });
                } break;

                case 2: {  // nowe zlecenie
                    nApp.auth.loginU(9, function() {
                        if (mes_data.cfg_task_mode(crr) == 0) { kit_note(lang[196]) }
                        else
                        if (mes_data.cfg_task_mode(crr) == 1) {
                            that.get_product_list( mes_data.crr_sv, function (nid, p2) {
                                that.act_task_menu_tm2(nid,p2)
                            })
                        } else {

                            that.get_id_list(3, mes_data.crr_sv, function (nid, p2) {
                                that.cmd_accept(20, nid, lang[191]);
                            })
                        }
                    });
                } break;


                case 3: { // zawieszenie zlecenia
                    nApp.auth.loginU(9, function() {
                        that.cmd_accept(26, tid, lang[197]);
                    });
                } break;
                case 4: { // wznowienie zlecenia zlecenia
                    nApp.auth.loginU(9, function() {
                        that.get_id_list(4, mes_data.crr_sv, function (nid, p2) {
                            that.cmd_accept(27, nid, lang[198]);

                        })
                    });
                } break;
                case 5: { // punkt kontrolny zlecenia
                    nApp.auth.loginU(11, function() {    that.cmd_accept(29, tid, lang[195]) });
                } break;

                case 6: { // punkt kontrolny zlecenia
                    that.get_id_list(5, mes_data.crr_sv, function (nid, p2) {  })
                } break;
            }
        })

    };

    this.act_task_menu_tm2=function(tid,p2){

        var crr=mes_data.crr;
        var memo= '';
        var vtarget=0;
        var utarget=mes_data.cfg_task_target( mes_data.crr  );


        function __dok(){
            var tskid=memo.val().toUpperCase();

            if(utarget){
                vtarget=tspin.val();
                if( tskid!='' && vtarget!=0 ){
                    that._send_cmd(20,tid,tskid,vtarget,1);
                    dm.aFree();
                    $("html, body").animate({scrollTop: 0}, "fast");
                } else { kit_note( 'no '+ lang[158]+' or '+lang[159] );}
            }else {
                if( tskid!=''  ){
                    that._send_cmd(20,tid,tskid,0,1);
                    dm.aFree();
                    $("html, body").animate({scrollTop: 0}, "fast");
                } else { kit_note( 'no '+ lang[158] );}
            }

        }


        window.scrollTo(0, 1);

        var dm = new Kit_dialog_constructor( 400, 380  ,3,0,true,true);
        dm.btn_close_after=false;
        dm.btn_call_ok = __dok;
        dm.btn_call_close=function(){dm.aFree()};
        dm.draw();
        dm.addHeader(lang[191]);
        var sx=lang[176]+':<br>'+p2+'<hr>';
        sx+='<div >'+lang[158]+'</div><div class="mes_auth_marker"></div><input id="svcmdcmt" type="text" /> <br><br>';

        if ( utarget ) {
            sx+='<div >'+lang[159]+'</div>';
            sx+='<div id="tspin" ></div>';
            var tspin = new Kit_spin_box_constructor('#tspin','', 0);
            tspin.width=100;
            tspin.min=0;    tspin.max=99999999;
        }

        dm.content(sx,false  );
        dm.showDialog(0);
        memo=$( "#svcmdcmt");
        memo.addClass('inputtxt').css('text-transform','uppercase').focus() ;
        BindClick(memo, function (event){ memo.focus()  } );
        if ( utarget ) { tspin.make()};

    };

    this.act_task_menu_tm22222222=function(tid,p2){

        var crr=mes_data.crr;
        var memo= '';
        var vtarget=0;


        function __dok(){
            var tskid=memo.val().toUpperCase();
            var ack=  tskid!='' && vtarget!=0;
            if ( !mes_data.cfg_task_target( mes_data.crr  ) ) { ack= tskid!=''  }
            if ( ack ) {
                that._send_cmd(20,tid,tskid,vtarget,1);
                dm.aFree();
                $("html, body").animate({scrollTop: 0}, "fast");
            }
        }
        window.scrollTo(0, 1);

        var dm = new Kit_dialog_constructor( 400, 380  ,3,0,true,true);
        dm.btn_close_after=false;
        dm.btn_call_ok = __dok;
        dm.btn_call_close=function(){dm.aFree()};
        dm.draw();
        dm.addHeader('Nowe zlecenie');
        var sx='Produkt:<br>'+p2+'<hr>';
        sx+='<div >'+lang[158]+'</div><div class="mes_auth_marker"></div><input id="svcmdcmt" type="text" /> <br><br>';

        if ( mes_data.cfg_task_target( mes_data.crr  ) ) {
            sx += '<div >' + lang[159] + '</div>';
            sx += '<div id="dtarget" style="width: 100px; background-color:#F9E98E;color:black; height: 22px;display: inline-table;padding: 5px ;font-size: 14px" >??</div> ';
            sx += '<button class="btn btn-blue " style="display: inline-table;" id="btarget"   >CALC</button>';
        }
        sx+='<div id="tspin" ></div>';
        var tspin = new Kit_spin_box_constructor('#tspin',lang[159], 10);
        tspin.width=100;

        dm.content(sx,false  );
        dm.showDialog(0);
        memo=$( "#svcmdcmt");
        memo.addClass('inputtxt').css('text-transform','uppercase').focus() ;
        BindClick(memo, function (event){ memo.focus()  } );
        tspin.make();

        $('#btarget').on('click touchstart',function(e){
            memo.blur();
            kit_dialog_calc(0,0,lang[59], function( v ){
                vtarget=v;
                $('#dtarget').text(v);

            })
        });
    };


    this.act_defect_k=function(){
        var that=this;
        function __dok() {

            var bsuma=0;
            var x=0;
            for (var j=0; j< mes_data.cfg_kb_count(crr)  ; ++j ){
                x=__getx('kdv'+j);
                if (x>0){
                    that._send_cmd(35,2,'',x,mes_data.cfg_kb_id(crr,j));
                    bsuma=bsuma+x;
                }
            }
            if (bsuma>0){
                that._send_cmd(35,0,'',bsuma,0);
            }
            dm.aFree()
        }

        var crr=mes_data.crr;
        var dm = new Kit_dialog_constructor( 600, 500  ,3,0,true,true);
        dm.btn_close_after=false;
        dm.btn_call_ok = __dok;
        dm.btn_call_close=function(){dm.aFree()};
        dm.draw();
        dm.addHeader(lang[177]);

        var sx='<table class="mes_auth_marker">';
        function  __addr(nr){
            var vid='kdv'+nr;
            sx=sx+'<tr><td><div class="kb_label" >'+ mes_data.cfg_kb_name(crr,nr)  +'</div></td ><td class="kb_labelv"><div  id="'+vid+'" >0</div></td>';
            sx=sx+'<td style="width: 50px"><div class="btvp btn btn-blue" data-bp="'+vid+'">+</div></td>';
            sx=sx+'<td style="width: 50px"><div class="btvm btn btn-blue" data-bp="'+vid+'">-</div></td> ';
            sx=sx+'<td style="width: 80px"><div class="btvc btn btn-blue" data-bp="'+vid+'">Calc</div></td> </tr>';
        }
        function __getx(id){
            var x=$('#'+id).text();
            return parseFloat(x);
        }

        for (var j=0; j< mes_data.cfg_kb_count(crr)  ; ++j ){   __addr(j)    }
        sx=sx+'</table>';
        dm.content(sx,true  );
        dm.showDialog(0);
        $('.btvp').on(clickEvent,function(e){
            e.stopPropagation(); e.preventDefault();
            var data = $(this).attr('data-bp');
            var x=__getx(data);
            x=x+1;
            $('#'+data).text(x);

        });
        $('.btvm').on(clickEvent,function(e){
            e.stopPropagation(); e.preventDefault();
            var data = $(this).attr('data-bp');
            var x=__getx(data);
            if (x>0) {
                x = x - 1;
                $('#' + data).text(x);
            }
        });
        $('.btvc').on(clickEvent,function(e){
            e.stopPropagation(); e.preventDefault();
            var data = $(this).attr('data-bp');
            var x=__getx(data);
            kit_dialog_calc(0,0,'', function( v ){
                $('#' + data).text(v);
            });
            return false
        });
    };

    this.info_task_product=function(){
        var product_id=0;
        var tool_id=0;
        var dm = new Kit_dialog_constructor( 1000, 700  ,1,0,true,true);
        dm.draw();
        dm.addHeader('Task Info');
        var s='';

        s+='<div id="pi-tabs" style=" font-size: 13px">';
        s+='<ul>';
        s+='<li><a href="#pi-tabs-1">Parametry zlecenia</a></li>'
        s+='<li><a href="#pi-tabs-2">Produkt - Info, foto</a></li>';
        s+='<li><a href="#pi-tabs-3">Produkt - Opis 1</a></li>';
        s+='<li><a href="#pi-tabs-4">Produkt - Opis 2</a></li>';
        s+='<li><a href="#pi-tabs-5">Narzędzie</a></li>';
        s+='</ul>';

        s+='<div id="pi-tabs-1" style="padding:6px;">';
        s+='<div id="pi-taskparams"></div>';
        s+='</div>';

        s+='<div id="pi-tabs-2" style="padding:6px;min-height: 500px">';
        s+='<div class="w3-container  w3-cell w3-cell-top" style="width: 550px; padding-left:10px; vertical-align: top; font-size: 12px" >';
        s+='<div id="svi_op"></div>';
        s+='</div>';
        s+='<div class="w3-container  w3-cell " style="width: 350px; padding-left:10px; vertical-align: top;" >';


        s+='<table  >';//
        s+='<tr class="giptr" >';
        s+='<td class="giptd"><img src="" id="p_foto1" style="max-width: 100%; overflow: auto;" ><div id="p_fotol1" class="giptdl">opis 1111</div></td>';

        s+='<td class="giptd"><img src="" id="p_foto2" style="max-width: 100%; overflow: auto;" ><div id="p_fotol2" class="giptdl"></div></td>';
        s+='</tr><tr class="giptr" >';
        s+='<td class="giptd"><img src="" id="p_foto3" style="max-width: 100%; overflow: auto;" ><div id="p_fotol3" class="giptdl"></div></td>';
        s+='<td class="giptd"><img src="" id="p_foto4" style="max-width: 100%; overflow: auto;" ><div id="p_fotol4" class="giptdl"></div></td>';
        s+='</tr>';
        s+='</table>';
        s+='</div></div>';

        s+='<div id="pi-tabs-3" style="padding:6px;min-height: 500px;background-color: #FEFFF9">';
        s+='<div id="pi-desc1"></div>';
        s+='</div>';
        s+='<div id="pi-tabs-4" style="padding:6px;min-height: 500px;background-color: #FEFFF9">';
        s+='<div id="pi-desc2"></div>';
        s+='</div>';

        s+='<div id="pi-tabs-5" style="padding:6px;min-height: 500px">';
        s+='<div class="w3-container  w3-cell w3-cell-top" style="width: 550px; padding-left:10px; vertical-align: top; font-size: 12px" >';
        s+='<div id="toolname"></div>';
        s+='</div>';
        s+='<div class="w3-container  w3-cell " style="width: 350px; padding-left:10px; vertical-align: top;" >';
        s+='<table  >';//
        s+='<tr class="giptr" >';
        s+='<td class="giptd"><img src="" id="t_foto1" style="max-width: 100%; overflow: auto;" ></td>';
        s+='<td class="giptd"><img src="" id="t_foto2" style="max-width: 100%; overflow: auto;" ></td>';

        s+='</tr></table></div></div>';


        dm.content(s,true  );
        dm.showDialog(0);

        $( "#pi-tabs" ).tabs();

        function _tool(){
            if (tool_id==0){ $('[href="#pi-tabs-5"]').closest('li').hide(); }
            else{
                var sdata={};
                sdata['q0']=mes_data.crr_sv;
                sdata['q1']=tool_id;

                $.ajax({
                    'url':"mes_get_tool_info",
                    'dataType':'json',
                    'type':"GET",
                    data: JSON.stringify( sdata ),
                    success: function(jdata) {
                        var t='';
                        t+='<table class="dataset_0" style="width: 100%">';
                        t+='<tr><td style="max-width: 70px;width: 70px">ID</td><td>'+jdata['r0']+'</td></tr>';
                        t+='<tr><td>Symbol</td><td>'+jdata['r1']+'</td></tr>';
                        t+='<tr><td>Nazwa</td><td>'+jdata['r2']+'</td></tr>';
                        t+='<tr><td>Uwagi</td><td>'+jdata['r20']+'</td></tr>';
                        t+='</table';

                        $('#toolname').html( t );
                        nfw_fotobase_to_img('#t_foto1', jdata['r10']  ,function(){} );
                        nfw_fotobase_to_img('#t_foto2', jdata['r11']  ,function(){} );
                        nfw_fotobase_zoom('#t_foto1',nApp.cfgi.content_width-20, nApp.cfgi.content_height );
                        nfw_fotobase_zoom('#t_foto2',nApp.cfgi.content_width-20, nApp.cfgi.content_height);
                    }
                });
            }
        }

        var sdata={};
        sdata['q0']=mes_data.crr_sv;
        sdata['q1']=0;
        sdata['q3']='dataset_'+nApp.cfgi.theme_style;

        $.ajax({
            'url':"mes_get_task_info",
            'dataType':'json',
            'type':"GET",
            data: JSON.stringify( sdata ),
            success: function(jdata) {

                $('#pi-taskparams').html(  jdata['r1']  );
                product_id=jdata['r2'];
                tool_id=jdata['r3'];

                if(product_id==0){
                   // $( "#pi-tabs-1").hide();
                    $('[href="#pi-tabs-2"]').closest('li').hide();
                    $('[href="#pi-tabs-3"]').closest('li').hide();
                    $('[href="#pi-tabs-4"]').closest('li').hide();
                    _tool();

                }else{
                    var sdata={};
                    sdata['q1']=product_id;
                    $.ajax({
                        'url':"mes_get_product_info",
                        'dataType':'json',
                        'type':"GET",
                        data: JSON.stringify( sdata ),
                        success: function(j) {
                            var t='';
                              function _tlink( nr,nrg ){
                               if (j['r'+(nrg+2)] ){
                                var sl='<br><div style="display:inline; font-size: 10px;  vertical-align: top; ">'+j['r'+nrg]+'</div>';
                                t+='<tr><td>link '+(nr)+'</td>';
                                t+='<td>'+htmlts.btn_inline('pil'+nr,300,40,j['r'+(nrg+1)],'btn btn-blue')+sl+'</td></tr>';
                               }
                              }

                            t+='<table class="dataset_0" style="width: 100%">';
                            t+='<tr><td style="max-width: 70px;width: 70px">ID</td><td>'+j['r0']+'</td></tr>';
                            t+='<tr><td>Nazwa</td><td>'+j['r1']+'</td></tr>';
                            t+='<tr><td>Opis</td><td>'+j['r2']+'</td></tr>';
                            t+='<tr><td>Uwagi</td><td>'+j['r3']+'</td></tr>';

                            _tlink(1,30);
                            _tlink(2,33);
                            _tlink(3,36);
                            t+='</table';

                            $('#svi_op').html( t );

                            $('#p_fotol1').text(j['r14']);
                            $('#p_fotol2').text(j['r15']);
                            $('#p_fotol3').text(j['r16']);
                            $('#p_fotol4').text(j['r17']);

                            nfw_fotobase_to_img('#p_foto1', j['r10']  ,function(){} );
                            nfw_fotobase_to_img('#p_foto2', j['r11']  ,function(){} );
                            nfw_fotobase_to_img('#p_foto3', j['r12']  ,function(){} );
                            nfw_fotobase_to_img('#p_foto4', j['r13']  ,function(){} );

                            nfw_fotobase_zoom('#p_foto1',nApp.cfgi.content_width-20, nApp.cfgi.content_height );
                            nfw_fotobase_zoom('#p_foto2',nApp.cfgi.content_width-20, nApp.cfgi.content_height);
                            nfw_fotobase_zoom('#p_foto3',nApp.cfgi.content_width-20, nApp.cfgi.content_height );
                            nfw_fotobase_zoom('#p_foto4',nApp.cfgi.content_width-20, nApp.cfgi.content_height);

                            $('#pi-desc1').html( j['r20'] );
                            $('#pi-desc2').html( j['r21'] );

                            _tool();

                            BindClick('#pil1',function(){ kit_note('l1 '+j['r30']);  window.open(j['r30'], '_blank');   })
                            BindClick('#pil2',function(){ kit_note('l2 '+j['r33']);  window.open(j['r33'], '_blank');   })
                            BindClick('#pil3',function(){ kit_note('l2 '+j['r36']);  window.open(j['r36'], '_blank');   })
                        }
                    });
                }

            }

        });

    };

    // komunikaty
    this.komunikaty=function(){
        var dm = new Kit_dialog_constructor( 750, 550  ,1,0,true,true);
        dm.sall(lang[206],'<div id="dic_table" class="mes_auth_marker"></div>',false);

        var dst = new Dataset_table_constructor('#dic_table',nApp.cfgi.theme_style);
        dst.colname=[lang[207],lang[206],'W.D.'];
        dst.colwidth=[110,570,50];
        dst.height= 360;
        dst.make();
        var sdata={};
        sdata['q1']=mes_set.nr_group;
        $.ajax({
            'url':"mes_get_message",
            'dataType':'json',
            'type':"GET",
            data: JSON.stringify( sdata ),
            success: function(jdata) {
                var s=''; sd='';
                $.each(jdata , function (i, item) {

                    s+='<tr>';
                    s+=dst.td(jdata[i].r1);
                    s+=dst.td(jdata[i].r2);
                    s+=dst.td(jdata[i].r3);

                    s+='</tr>';
                });
                dst.load(s);

            }

        });
        dm.showDialog(0);
    };

    this.info_sv=function(){
        var dm = new Kit_dialog_constructor( 1000, 500  ,1,0,true,true);
        dm.draw();
        dm.addHeader('sv info');
        var s='';
        s+='<div class="w3-container  w3-cell w3-cell-top" style="width: 600px; padding-left:10px; vertical-align: top; font-size: 12px" >';
        s+='<div id="svi_op"></div>';
        s+='</div>';
        s+='<div class="w3-container  w3-cell " style="width: 350px; padding-left:10px; vertical-align: top;" >';
        s+='<table  >';//
        s+='<tr class="giptr" >';
        s+='<td class="giptd"><img src="" id="svi_foto1" style="max-width: 100%; overflow: auto;" ></td>';
        s+='<td class="giptd"><img src="" id="svi_foto2" style="max-width: 100%; overflow: auto;" ></td>';
        s+='</tr></table></div>';
        dm.content(s,false  );
        dm.showDialog(0);

        var sdata={};
        sdata['q1']=mes_data.crr_sv;

        $.ajax({
            'url':"mes_get_sv_info",
            'dataType':'json',
            'type':"GET",
            data: JSON.stringify( sdata ),
            success: function(j) {
                var  t='';
                t+='<table class="dataset_0" style="width: 100%">';
                t+='<tr><td style="max-width: 120px;width: 120px">SV</td><td>'+mes_data.crr_sv+'</td></tr>';
                t+='<tr><td>Obiekt</td><td>'+j['r1']+'</td></tr>';
                t+='<tr><td>Opis</td><td>'+j['r2']+'</td></tr>';
                t+='<tr><td>Lokalizacja</td><td>'+j['r5']+'</td></tr>';
                t+='<tr><td>nr Ewidencyjny</td><td>'+j['r4']+'</td></tr>';
                t+='<tr><td>Wejście główne</td><td>'+j['r6']+'</td></tr>'
                t+='<tr><td>Uwagi</td><td>'+j['r3']+'</td></tr>';
                t+='</table';
                $('#svi_op').html( t );

                nfw_fotobase_to_img('#svi_foto1', j['r10']  ,function(){} );
                nfw_fotobase_to_img('#svi_foto2', j['r11']  ,function(){} );
                nfw_fotobase_zoom('#svi_foto1',nApp.cfgi.content_width-20, nApp.cfgi.content_height );
                nfw_fotobase_zoom('#svi_foto2',nApp.cfgi.content_width-20, nApp.cfgi.content_height);
            }
        });
    };


    // ------------------------------------ blok inspekcja ------------------------------------------------

    this.inspekcja_accept=function(cmd,nrj,title ,ecall ){

        function __dok(){
            var sdata={};
            sdata['q1']=mes_data.crr_sv ; //nrsv
            sdata['g2']=cmd;
            sdata['q3']=nrj;
            sdata['q4']= encodeURIComponent($( "#incmdcmt").val());
            sdata['q5']= nApp.auth.key;
            sdata['q6']= nApp.auth.id;

            $.ajax({  'url':"mes_send_insp_cmd",  'dataType':'json', 'type':"POST", 'global':false,
                data: JSON.stringify( sdata ),
                success: function(jdata) {

                    is_callback(ecall)
                    dm.aFree();
                            },
                complete: function(data) {  }
            });
        }

        var that=this;
        var lckb=false;
        $("html, body").animate({ scrollTop: 0 }, 0);
        var sx='<div class="mes_auth_marker"></div><br><textarea id="incmdcmt" cols="40" rows="3" style="width: 100%;" ></textarea> ';
        var dm = new Kit_dialog_constructor( 450, 220  ,3,0,true,true);
        dm.header_ico=insp_ico;
        dm.sall_call(title,sx,false,__dok)
    };





    this.inspekcja_acceptcl=function(cmd,nrj,title ,ecall ){
      var clistd=0;
        function __dok(){

            var sdata={};
            sdata['q1']=mes_data.crr_sv ; //nrsv
            sdata['g2']=2;
            sdata['q3']=nrj;
            sdata['q4']= encodeURIComponent($( "#incmdcmt").val());
            sdata['q5']= nApp.auth.key;
            sdata['q6']= nApp.auth.id;
            sdata['q7']= clistd;
            sdata['q8']=encodeURIComponent(mes_data.cfg_inspekcja_name(mes_data.crr , mes_data.inspection_nrcfg(mes_data.crr, nrj)));

            sdata['q10'] = $("#idc0").length ? htmlcmp.getCheck('#idc0'  ) : false;
            sdata['q11'] = $("#idc1").length ? htmlcmp.getCheck('#idc1'  ) : false;
            sdata['q12'] = $("#idc2").length ? htmlcmp.getCheck('#idc2'  ) : false;
            sdata['q13'] = $("#idc3").length ? htmlcmp.getCheck('#idc3'  ) : false;
            sdata['q14'] = $("#idc4").length ? htmlcmp.getCheck('#idc4'  ) : false;
            sdata['q15'] = $("#idc5").length ? htmlcmp.getCheck('#idc5'  ) : false;
            sdata['q16'] = $("#idc6").length ? htmlcmp.getCheck('#idc6'  ) : false;
            sdata['q17'] = $("#idc7").length ? htmlcmp.getCheck('#idc7'  ) : false;

            sdata['q20'] = $("#idt0").length ? encodeURIComponent($('#idt0').val()  ) : '';
            sdata['q21'] = $("#idt1").length ? encodeURIComponent($('#idt1').val()  ) : '';
            sdata['q22'] = $("#idt2").length ? encodeURIComponent($('#idt2').val()  ) : '';
            sdata['q23'] = $("#idt3").length ? encodeURIComponent($('#idt3').val()  ) : '';
            sdata['q24'] = $("#idt4").length ? encodeURIComponent($('#idt4').val()  ) : '';
            sdata['q25'] = $("#idt5").length ? encodeURIComponent($('#idt5').val()  ) : '';
            sdata['q26'] = $("#idt6").length ? encodeURIComponent($('#idt6').val()  ) : '';
            sdata['q27'] = $("#idt7").length ? encodeURIComponent($('#idt7').val()  ) : '';

            console.log(sdata)

            $.ajax({  'url':"mes_send_insp_cmd",  'dataType':'json', 'type':"POST", 'global':false,
                data: JSON.stringify( sdata ),
                success: function(jdata) {
                    is_callback(ecall)
                    dm.aFree();
                },
                complete: function(data) {  }
            });
        }

        var that=this;
        var nr=mes_data.crr;
        var cfgnr = mes_data.inspection_nrcfg(nr, nrj);
        var clista= mes_data.cfg_inspekcja_clista(nr,cfgnr);
        clistd=clista-1;
        console.log(clista+'  '+cfgnr)
        var cl=[];
        if (clista==1){ cl=mes_data.cfg_clista1A(mes_data.crr) }
        if (clista==2){ cl=mes_data.cfg_clista2A(mes_data.crr) }
        if (clista==3){ cl=mes_data.cfg_clista3A(mes_data.crr) }

        var ccl=0;
        var hh=260;
        for (var j = 0; j < 8 ; ++j) { if(cl[j]!=''){ ++ccl; hh=hh+40 }  }

        dt=new Kit_table_constructor();
        dt.tbegin('dataset_0');
        dt.addhead('<th>pozycja</th><th>OK</th> <th>Uwagi</th>') ;
        for (var j = 0; j < ccl ; ++j) {
            dt.trb();
             dt.tds( 'min-width: 350px;vertical-align: bottom'  ,cl[j]);
             dt.tdw(50,htmlts.check('idc'+j,false) );
             dt.tds('min-width: 300px;vertical-align: bottom' ,htmlts.input('idt'+j,300,'kit_form_input') );
            dt.tre();
        }

        var sx= dt.tend() +'<br><div class="mes_auth_marker"></div>uwagi<br><textarea id="incmdcmt" cols="40" rows="3" style="width: 350px;" ></textarea> ';
        var dm = new Kit_dialog_constructor( 740, hh  ,3,0,true,true);
        dm.header_ico=insp_ico;
        dm.sall_call(title,sx,false,__dok)

     };


    this.inspekcja_add=function(ecall ){

        function __dok(){
            var sdata={};
            sdata['q1']=mes_data.crr_sv ; //nrsv
            sdata['g2']=3;
            sdata['q3']=nri;

            sdata['q4']= encodeURIComponent( $( "#incmdcmt").val() );
            sdata['q5']= nApp.auth.key;
            sdata['q6']= nApp.auth.id;

            var x=xspin.val()*60 ;
            if(warunek>1){x=xspin.val()}
            sdata['q7']= x;

            $.ajax({  'url':"mes_send_insp_cmd",  'dataType':'json', 'type':"POST", 'global':false,
                data: JSON.stringify( sdata ),
                success: function(jdata) {

                    is_callback(ecall);
                    dm.aFree();
                },
                complete: function(data) {  }
            });

        }

        var that=this;
        var nri=0;
        var warunek=0;
        var nr=mes_data.crr;
        var iwarunek = ['Za X min.','Za X min. pracy','Po X cykli','Po X szt.','?'];
        var ilist= mes_data.cfg_inspekcja_nameA(nr) ;
        var ilistl=0;
        var hh=130;

        for (var j = 0; j < 12 ; ++j) {  if ( ilist[j]!='' ){ ilistl++; hh=hh+43; } }
        if(hh<350){hh=350}

        var sx='<div class="mes_auth_marker"></div><biv id="linsp"></biv> <br>';
        var dm = new Kit_dialog_constructor( 780, hh ,3,0,true,true);
        dm.header_ico=insp_ico;
        dm.sall_call(lang[246],sx,false, __dok)

        function getin(nrl){
            nri=nrl;
            warunek=mes_data.cfg_inspekcja_warunek(nr,nrl);
            return ilist[nrl]+'<br><div style="display: inline;font-size: 12px">warunek '+iwarunek[ warunek ]+'</div> '
        }

        var s='';
        s+='<div class="w3-container  w3-cell w3-cell-top" style="width: 400px; padding-left:10px; vertical-align: top; font-size: 12px" >';

        var dt=new Kit_table_constructor();
        dt.setbtn(50,30 );
        dt.tbegin('dataset_0');
        for (var j = 0; j < ilistl ; ++j) {
          dt.trb();
            dt.tdw(300,getin(j) );
            dt.tdbtn('btniadd'+j,j,'set','btn-blue iaddbtn');
          dt.tre();

        }
        s+=dt.tend();

        s+='</div>';
        s+='<div class="w3-container  w3-cell " style="width: 350px; padding-left:10px; vertical-align: top;" >';
        s+=lang[242]+': <br><div id="disp1" style="min-height: 50px;height: 50px"></div>  <br>';

        s+='<div id="xspin" ></div>'
        s+='<br><br>'+lang[243]+'<br><textarea id="incmdcmt" cols="40" rows="3" style="width: 100%;" ></textarea> '
        s+='</div>';
        $( "#linsp").html(s);

       var xspin = new Kit_spin_box_constructor('#xspin','<b>X:</b>', 10);
        xspin.width=80;
        xspin.make();

        $('.iaddbtn').on( 'click touchstart', function (e) {   e.stopPropagation(); e.preventDefault();
            $('#disp1').html( getin(  $(this).attr('data-button')))
        } );

        $('#disp1').html( getin( 0 ))

    }; // end inspekcja_add



    this.inspekcja=function(){
        var that=this;

        nApp.BindEvent(function(ev,a2,a3){
            if (a2==100){     getdata()  }
        });
        function rtimer(){
            setTimeout(function() {
                mes_data.getData();
                nApp.core_rtimer_reset();
            }, 800);
        }

        function btnh(act,nrj){
               nApp.auth.login( function(){
            if (act==0){   that.inspekcja_accept(0,nrj,lang[244],rtimer)  }
            if (act==1){
                var clista= mes_data.cfg_inspekcja_clista(mes_data.crr, mes_data.inspection_nrcfg(mes_data.crr, nrj) );

                if(clista==0){  that.inspekcja_accept(1,nrj,lang[245],rtimer) }
                else  {  that.inspekcja_acceptcl(2,nrj,lang[245],rtimer)   }

            }
        });
        }

        function getdata(){
            var nr=mes_data.crr;
            var s='<table class="dataset_0">';
            var iwarunek = ['Za X min.','Za X min. pracy','Po X cykli','Po X szt.','?'];
            var iwarunek2= [' min.',' min.',' cykli',' szt.','?'];
            var iwarunek3 = ['Time X:','Time X:','cykli X:','Szt X:','?'];

            $('#itable').html( '' );

            td=new Kit_table_constructor();
            td.setbtn(70,40);
            td.tbegin('dataset_0');

            for (var j = 0; j < 4; ++j) {
                if (mes_data.inspection_status(nr, j) != 0) {

                    var cfgnr = mes_data.inspection_nrcfg(nr, j);
                    var status= mes_data.inspection_status(nr, j);
                    var count = mes_data.inspection_w_count(nr, j);
                    var warunek=mes_data.cfg_inspekcja_warunek(nr,cfgnr);
                    var preset= mes_data.inspection_preset(nr,j);
                    var clista= mes_data.cfg_inspekcja_clista(nr,cfgnr);
                    if (warunek<2){preset=preset/60}
                    var sl=''; if(clista!=0){sl='  lista'+clista};
                    var xvp=iwarunek3[warunek]+' '+count;
                    if(warunek<2){xvp=iwarunek3[warunek]+' '+ktime.SecToGMS(count);}

                    var iname=mes_data.cfg_inspekcja_name(nr, cfgnr)+'<br>';
                    iname+='<p style="display: inline;font-size: 13px">warunek: '+iwarunek[warunek]+' ['+preset+' '+iwarunek2[warunek]+']'+sl+'</p>';

                    td.trb();
                    td.tdw(360, iname );

                    var icount=iwarunek3[warunek]+' '+count;
                    if(warunek<2){icount=iwarunek3[warunek]+' '+ktime.SecToGMS(count);}

                    var istate='<p style="font-size: 18px;display: inline">'+icount+'</p>';  var svc='white';
                    if(status==2){ istate='Inspekcja aktywna od<br><p style="font-size: 18px;display: inline">'+  ktime.SecToGMS(count)+'</p>' ; svc='#B9FFB9'  }
                    td.tdwc(230,svc,istate);

                    td.tdbtn('btnii1'+j,j,'Anuluj','btn-red iibtn1');
                    td.tdbtnW(status==2, 'btnii1'+j,j,'OK','btn-green iibtn2');

                    td.tre();

                }
            }
            $('#itable').html( td.tend() )


            $('.iibtn1').on( 'click touchstart', function (e) {   e.stopPropagation(); e.preventDefault(); btnh(0,$(this).attr('data-button'));        } );
            $('.iibtn2').on( 'click touchstart', function (e) {   e.stopPropagation(); e.preventDefault(); btnh(1,$(this).attr('data-button'));        } );
        };

        var sb=htmlts.btn_inline_wdata( 'btniiadd',1 ,200,40,'<i class="mdi i24 mdi-magnify "></i> '+lang[241]+'','btn-blue')+'<br><br>  <div id="itable"></div>';
        var dm = new Kit_dialog_constructor( 800, 400  ,1,0,true,true);
        dm.header_ico=insp_ico;
        dm.btn_close_after=true;
        dm.sall('Inspekcje',sb,false);

        BindClick('#btniiadd', function (event){
            nApp.auth.login( function(){  that.inspekcja_add(rtimer)});
        });
        getdata();
    }; // end inspekcja


    this.inspekcja_btndisp=function(){

        try {
            if (this.sbtn_btn[11].is(':visible')) {
                var nr = mes_data.crr;
                var s = mes_data.inspection_count(nr) + '/' + mes_data.inspection_countAr(nr);
                var sc = '';
                if (mes_data.inspection_countAr(nr) != 0) {
                    sc = 'class="blink_meon"'
                }
                this.sbtn_btn[11].html('<i class="mdi i24 mdi-magnify "></i> <p ' + sc + '  style="font-size:  20px; display: inline">' + s + '</p>')
            }
        } catch(e){ return false}
    };

    // ----------   end blok inspekcja --------------------------


    this.view_sr=function(mode,xnrc){

        var dm = new Kit_dialog_constructor( 880, 620  ,1,0,true,true);
        dm.draw();
        dm.addHeader('Status rozszerzony');
        var s='<select id="nrpareto" style="margin-left: 10px"></select><select id="nrcs" style="margin-left: 10px"></select><br>';
        s+='<div id="viewstx" style="width:820px;height:560px;margin-left: 10px" ></div>';
        dm.content(s, mes_data.cfg_stat_sr_count( mes_data.crr )>20  );
        dm.showDialog(0);
        var nrc=1;
        var nrpareto=1;

        var pd=$('#nrcs');
        for (var j = 0; j < 6 ; ++j) {    pd.append(new Option(lang[110+j] , j));   }
        pd.change(function () {
            nrc =$('#nrcs')[0].selectedIndex;
            _getdata();
        }).height(30).get(0).selectedIndex = 1;

        var ps=$('#nrpareto');
        ps.append(new Option(lang[173] , 0));
        ps.append(new Option(lang[208] , 1));
        ps.append(new Option(lang[209] , 2));
        ps.append(new Option(lang[210] , 3));
        ps.append(new Option(lang[211] , 4));
        ps.change(function () {
            nrpareto =$('#nrpareto')[0].selectedIndex;
            _getdata();
        }).height(30).get(0).selectedIndex = 1;

        var viewst = new MES_View_constructor('#viewstx',650,450 );
        viewst.paused=true;
        viewst.init( function(v){    });
        var lp=0;
        var count=0;
        var rname=[];
        var rbase=[];
        var rtime=[];
        var rprc=[];

        _getdata();

        function _draw(){
            var yy=10;
            function __bar(x,nr){
               var fc='black';
                viewst.cx.frame( x,yy,200,20,1, 'gray');
                viewst.cx.dtext(x+4,yy+3,14,fc,  rname[nr]);

                viewst.cx.frame( x+200,yy,60,20,1, 'gray',state_colors[rbase[nr]]);
                viewst.cx.drawIco(x+206,yy,state_ico_code[rbase[nr]],18,'black');
                viewst.cx.dtext(x+230,yy+3,14,fc, lang[90+parseFloat( rbase[nr]  )]);

                viewst.cx.frame(x+260,yy,100,20,1, 'gray');
                viewst.cx.dtext(x+264+90,yy+3,14,fc, ktime.SecToDhms( rtime[nr]),'right');

                viewst.cx.frame(x+360,yy,60,20,1, 'gray');
                viewst.cx.dtext(x+358+55,yy+3,14,fc, rprc[nr]+'%','right'  );

                if (rprc[nr]>0) {  viewst.cx.barH(x + 422, yy, 350, 20, rprc[nr], 'white', state_colors_chart[rbase[nr]], false);     }
                yy=yy+22;
            }

            viewst.cx.clear(false,false);

            if(mes_data.cfg_stat_use_sr( mes_data.crr )) {
                viewst.time_range_ico(1, 10, nrc);
              if( (mes_data.task_id(mes_data.crr )==0) && (nrc==2)  ){viewst.cx.dtext(40,10,16,'gray', lang[163])
              }else {
                  for (var j = 0; j < rname.length; ++j) {      __bar(40, j);     }
              }
            }else {viewst.cx.dtext(1,10,16,'gray', 'nadzorca nie wspiera statusu roszerzonego') }

        }

        function _getdata() {
            var sdata = {};
            sdata['q0'] = mes_data.crr_sv;
            sdata['q1'] = nrc;
            sdata['q2'] = nrpareto;
            $.ajax({
                'url': "mes_get_view_sr",
                'dataType': 'json',
                'type': "GET",
                data: JSON.stringify(sdata),
                success: function (j) {
                    var x='';
                    count=j['r25'];
                    x=j['r10'];     rname=x.split('^');
                    x=j['r11'];     rbase=x.split('^');
                    x=j['r12'];     rtime=x.split('^');
                    x=j['r13'];     rprc=x.split('^');
                    _draw();
                }
            });
        }
    };

    this.info_trend72=function(){
        var that=this;
        var dm = new Kit_dialog_constructor( 1130, 350  ,1,0,true,true);
        dm.draw();
        dm.addHeader(lang[169]);
        var s='';
        s+='<div id="t72display" style="margin-left: 10px;margin-top:30px;"></div>';
        s+='<div id="tr72cv" style="width:1085px;height:100px;margin-left: 10px"></div><div id="tr72slider" style="width:1080px;margin-left: 10px"></div>';
        dm.content(s,false  );
        dm.showDialog(0);

        var cx = new Canvas_tool_constructor( canvas_in_div('#tr72cv',$('#tr72cv').innerWidth(),$('#tr72cv').innerHeight() ) );
        var tt=[];
        var ts=[];
        function __d(v){
           function __st(){
               var stn=ts[v]-1;
               var st=lang[80+ stn];
               if (stn==7){ st=lang[87] }
               if (stn==8){ st=lang[88] }
                return st
           }

            var s=lang[200]+': '+ ktime.UnixStr( tt[v])+'   '+__st() ;
            $('#t72display').html( s );
        }


        $( "#tr72slider" ).slider(
            {
                orientation: "horizontal",
                max: 1080,
                value: 1080,
                slide: function(){
                  var v= $( "#tr72slider" ).slider( "value" );
                   __d(v);
                }
            }
        );
        var sdata={};
        sdata['q0']=mes_data.crr_sv;
        nfw_ajax('mes_get_trend72',sdata, function(jdata) {

           function et(nr){ try{  var sx= jdata['r1'].split('^');   return parseFloat(sx[nr]); } catch(e) { return 0} };
            //var sx='';
            ts=jdata['r1'].split('^');
            tt=jdata['r2'].split('^');
            cx.frame(1,20,1079,69,1,'black','black');
            for (var i = 0; i < 1080; ++i) {
              // sx+=et(i);
               var sn=et(i);
               var cl= state_colors_chart[sn-1];
                if(sn==0){cl='red'}
              //  if(sn==8 || sn==9){cl=state_colors_chart[9] }
                var bh=2;
                if(sn==1){bh=12}
                if(sn==2 || sn==3 || sn==4){bh=18}
                if(sn==5 || sn==6){bh=30}
                if(sn==7){bh=46}
                if(sn==9){bh=14}
                if(sn==11 || sn==12){bh=18}
               if (sn!=8){
                     cx.lineV(i+1,70-bh,90,1,cl)
               }
            }
           __d(1079);

        });
    };

    // formularze DB

    this.get_id_list=function(action,nrsv,callback){
    /// 0- nul  1- szybki komentarz 2- lista produktów  3-lista zleceń oczekujących  4- lista zlecen zawieszonych  5- podgląd oczekujących
    var wwidth=[0,350,600,600,500,500];
    var wname=['',lang[166],lang[176],lang[201],lang[202],lang[201]];
    var bdt=[false,true,false,false,false];
    var vaction=action;
        if (action==5){ vaction=3}

        function _ret(p1,p2){
            dm.aFree();
            is_callback( callback(  p1,  p2   ) )
        }

        function _gdata(){
            var sdata={};
            sdata['q1']= nrsv ;
            sdata['q2']= vaction;
            nfw_ajax('mes_get_id_list',sdata, function(jdata) {
                var s='', sd='';
                $.each(jdata , function (i, item) {
                    if( bdt[vaction] ){ sd= jdata[i].r2  }else{sd='.'}
                    s+='<tr>';
                    s+=dst.td(jdata[i].r2);

                    if ( vaction==3){
                        s+=dst.td(htmlts.btn_inline_wdata(jdata[i].r0,sd ,46,30,'Info','btn-blue dictcl2')+' ');
                    }
                    if ( action!=5) {
                        s += dst.td(htmlts.btn_inline_wdata(jdata[i].r0, sd, 46, 30, 'OK', 'btn-green dictcl'));

                    }
                    s+='</tr>';
                });
                dst.load(s);
                $('.dictcl').on( 'click touchstart', function (e) {
                    e.stopPropagation(); e.preventDefault();
                    _ret(parseFloat( $(this).attr('id')),$(this).attr('data-button')  )
                } );

                $('.dictcl2').on( 'click touchstart', function (e) {
                    e.stopPropagation(); e.preventDefault();
                    //kit_note('id '+parseFloat( $(this).attr('id')));
                    that.get_ptw_info(true,parseFloat($(this).attr('id')));

                } );


            });
        }

        var dm = new Kit_dialog_constructor( wwidth[action], 500  ,1,0,true,true);
        dm.sall(wname[action],'<div id="dic_table" class="mes_auth_marker"></div>',false);

        var lm= mes_data.cfg_task_spnr_lm( mes_data.getIdBySV(nrsv)   ) ;
        var nrgr=mes_data.cfg_task_spnr( mes_data.getIdBySV(nrsv)  )


        if( lm>0 && action==3 ) {

            dm.addButtonP('<div class=" " >z innej maszyny [gs'+nrgr+']</div>', 'btn-blue btn-mdl', 80, function () {
                dm.aFree();
                that.get_list2(nrsv,callback);

            });
        }

        var dst = new Dataset_table_constructor('#dic_table',nApp.cfgi.theme_style);
        dst.colname=[lang[7],'id'];
        dst.colwidth=[wwidth[action]-80,60];
        dst.height= 330;
        dst.make();
        _gdata()

    };

    this.get_list2=function(nrsv,callback){ /////////////////////////////

        function _ret(p1,p2){    dm.aFree();  is_callback( callback(  p1,  p2   ) )         }

        function _gdata(){
            var sdata={};
            sdata['q1']= nrsv ;
            sdata['q2']= 3;
            nfw_ajax('mes_get_task_list2',sdata, function(jdata) {
                var s='', sd='';
                $.each(jdata , function (i, item) {
                    sd= jdata[i].r2;
                    s+='<tr>';
                    s+=dst.td(jdata[i].r3);
                    s+=dst.td(jdata[i].r2);
                    s+=dst.td(htmlts.btn_inline_wdata(jdata[i].r0,sd ,46,30,'Info','btn-blue dictcl2')+' ');
                    s+= dst.td(htmlts.btn_inline_wdata(jdata[i].r0, sd, 46, 30, 'OK', 'btn-green dictcl'));
                    s+='</tr>';
                });
                dst.load(s);
                $('.dictcl').on( 'click touchstart', function (e) {
                    e.stopPropagation(); e.preventDefault();
                    _ret(parseFloat( $(this).attr('id')),$(this).attr('data-button')  )
                } );

                $('.dictcl2').on( 'click touchstart', function (e) {
                    e.stopPropagation(); e.preventDefault();
                    //kit_note('id '+parseFloat( $(this).attr('id')));
                    that.get_ptw_info(true,parseFloat($(this).attr('id')));

                } );


            });
        }

        var dm = new Kit_dialog_constructor( 800, 500  ,1,0,true,true);
        dm.sall(lang[201],'<div id="dic_table" class="mes_auth_marker"></div>',false);

        var dst = new Dataset_table_constructor('#dic_table',nApp.cfgi.theme_style);
        dst.colname=[lang[129],lang[7],'i', 'id'];
        dst.colwidth=[300,400,60,60];
        dst.height= 330;
        dst.make();
        _gdata()

    }

    this.get_product_list=function(nrsv,callback){
    var flx=false;

        function _ret(p1,p2){
            dm.aFree();
            is_callback( callback(  p1,  p2   ) )
        }

        function _gdata(){
            var sdata={};
            sdata['q1']= nrsv ;
            sdata['q2']= 2;
            sdata['q3']=vev;
            nfw_ajax('mes_get_id_list',sdata, function(jdata) {
                var s='';
                $('.dictclp').off('click');
                $('.dictclp2').off('click');
                $.each(jdata , function (i, item) {

                    s+='<tr>';
                    s+=dst.td(jdata[i].r2);
                    s+=dst.td(htmlts.btn_inline_wdata(jdata[i].r0,jdata[i].r2+' ' ,46,30,'Info','btn-blue dictcl2'));
                    s+=dst.td(htmlts.btn_inline_wdata(jdata[i].r0,jdata[i].r2 ,46,30,'OK','btn-green dictclp'));
                    s+='</tr>';
                });
                dst.load(s);
                flx=true; //
                    $('.dictclp').on('click', function (e) {
                        e.stopPropagation();    e.preventDefault();
                        if (flx) {
                         flx=false;
                            _ret(parseFloat($(this).attr('id')), $(this).attr('data-button'))
                        }
                    });
                       $('.dictcl2').off('click').on('click ', function (e) {
                        e.stopPropagation();    e.preventDefault();
                           that.get_ptw_info(false,parseFloat($(this).attr('id')));
                    });

                $('#dt_ld_ind').hide();
            });
        }
        var vev=false;
        var dm = new Kit_dialog_constructor( 600, 500  ,1,0,true,true);
        //dm.sall(lang[176]+'+','<div id="dic_table" class="mes_auth_marker"></div>',false);
        dm.draw();
        dm.addHeader(lang[176]);
        var s='';
        s+='<input class="w3-check" type="checkbox" id="s212ev"  style="margin-left: 5px;margin-right: 10px" checked >Tylko powiązane';
        s+='<div id="dt_ld_ind" class="dt_load" >LOAD...</div>';
        s+='<hr style="height: 5px;margin: 0"><div id="dic_table"  class="mes_auth_marker"></div>';

        dm.content(s,false  );
        dm.showDialog(0);

        var dst = new Dataset_table_constructor('#dic_table',nApp.cfgi.theme_style);
        dst.colname=[lang[7],'id'];
        dst.colwidth=[580,60];
        dst.height= 300;
        dst.make();
        vev=true;
        _gdata();
        $('#s212ev').click( function(e){

            vev=$('#s212ev').is(":checked");
            dst.make();
            _gdata();
            $('#dt_ld_ind').show();
        });
    };

    this.get_ptw_info=function(ast,id){
     var cp=lang[247];
        if(ast){cp=lang[248]}
        var dm = new Kit_dialog_constructor( 600, 570  ,1,0,true,true);
        dm.sall(cp,'<div id="ptw_table" class="mes_auth_marker"></div>',true);

        var sdata={};
        sdata['q0']=ast;
        sdata['q1']=id;
        sdata['q3']='dataset_'+nApp.cfgi.theme_style;

        $.ajax({
            'url': "mes_get_ptw_info",
            'dataType': 'json',
            'type': "GET",
            data: JSON.stringify(sdata),
            success: function (jdata) {
                $('#ptw_table').html(  jdata['r1']  );
            }
        });

        dm.showDialog(0);
    };

    this.view_ev_form=function (modex,nrsv,xday){

        function _gdata(){
            var sdata={};
            sdata['q0']= nrsv ;
            sdata['q1']= 0;
            sdata['q2']= nApp.cfgi.lang_on;
            sdata['q3']= filtr_nr;
            sdata['q4']= that.ev_day_limit;
            sdata['q5']=vev;
            nfw_ajax('mes_get_ev_list',sdata,function(jdata){
                var s='';

                $.each(jdata , function (i, item) {
                    s+='<tr>';
                    s+=dst.td('<i class=" mdi i32 '+svev_ico[jdata[i].r9]+' "></i>');

                    if (mode==0){
                        var sx='<p class="w3-tiny">'+jdata[i].r11+'</p><br>';
                        //sx+=dst.td(jdata[i].r12);
                        s+=dst.td('<p class="w3-tiny">'+jdata[i].r11+'</p><p class="w3-small">'+jdata[i].r12+'</p><p class="w3-tiny">'+jdata[i].r14+'<br>'+jdata[i].r16+'</p>');

                        s+=dst.td(jdata[i].r13);
                    }
                    if (mode==1){
                        s+=dst.td(jdata[i].r11);
                        s+=dst.td(jdata[i].r12);
                        s+=dst.td(jdata[i].r13);

                    }
                    if (mode==2){
                        s+=dst.td(jdata[i].r11);
                        s+=dst.td(jdata[i].r12);
                        s+=dst.td(jdata[i].r13);
                        s+=dst.td(jdata[i].r16);
                        s+=dst.td(jdata[i].r14);
                        s+=dst.td(jdata[i].r15);
                    }
                    s+='</tr>';
                    $('#dt_ld_ind').hide()
                });
                dst.load(s)
            } )
         }

        var mode= nApp.cfgi.l_d_mode ;
        var dw=nApp.cfgi.l_d_width;
        var twidth=[44,140,300,300,150,120,110];
        var tnames=['ico',lang[50],lang[133],lang[165],'task',lang[171],'user'];
        var sc=lang[131];

        if (mode==0){
            twidth=[44,150,140];
            tnames=['ico',lang[133],lang[165]];
            sc=lang[132];
        }else if (mode==1){
            twidth=[44,100,160,220];
            tnames=['ico',lang[50],lang[133],lang[165]];
        }else if (mode==2){
        }

        var filtr_nr=0;
        var vev=false;
        var dm = new Kit_dialog_constructor( dw, nApp.cfgi.content_height-10  ,1,0,true,true);

        sc=sc.replace('__no__',that.ev_day_limit);
        dm.draw();
        dm.addHeader(sc);
        var s='';
        s+='<button data-nr="0"  class="s212ev btn btn-green" style="width:70px;margin-right: 5px " > <i class="mdi i16 mdi-select-all"> ALL</i></button>';
        s+='<button data-nr="1"  class="s212ev btn btn-silver" style="width:50px;margin-right: 5px " ><i class="mdi i16 mdi-settings"></i></button>';
        s+='<button data-nr="2"  class="s212ev btn btn-silver" style="width:50px;margin-right: 5px " ><i class="mdi i16 mdi-wrench"></i></button>';
        s+='<button data-nr="3"  class="s212ev btn btn-silver" style="width:50px;margin-right: 5px " ><i class="mdi i16 mdi-barcode"></i></button>';
        s+='<input class="w3-check" type="checkbox" id="s212ev"  style="margin-left: 5px;margin-right: 10px" >EV';
        s+='<div id="dt_ld_ind" class="dt_load" >LOAD...</div>';
        s+='<hr style="height: 5px;margin: 0"><div id="ev_table"></div>';

        dm.content(s,false  );

        dm.showDialog(0);
        var dst = new Dataset_table_constructor('#ev_table',nApp.cfgi.theme_style);
        dst.colname=tnames;//['ico','czas','zdarzenie','komentarz','task','operator','user'];
        dst.colwidth=twidth;// [50,140,300,300,130,100,100];
        dst.height= dm.height - dm.hheight -  170;

        dst.make();
        _gdata();

        $('#s212ev').click( function(e){
             vev=$('#s212ev').is(":checked");
            _gdata();
            $('#dt_ld_ind').show();
        });

        $(".s212ev").click(function(e) {
            e.stopPropagation(); e.preventDefault();
            var filtr_=  $.parseJSON($(this).attr('data-nr'))  ;
            $(".s212ev").removeClass('btn-green').addClass('btn-silver');
            $(this).removeClass('btn-silver').addClass('btn-green');
            if (filtr_ != filtr_nr){
                filtr_nr=filtr_;
                _gdata();
                $('#dt_ld_ind').show();
            }
        });
    };



}