/**
 *  自定义控件书写模板
 */
(function(KDApi, $){
    // 构造函数，变量名随意，与最后一句代码的KDApi.register的第二个参数一致即可
    function PersonnelSchedule (model) {
        this._setModel(model)
    }

    // 原型中封装生命周期函数，固定格式
    PersonnelSchedule.prototype = {
        _setModel: function(model) {
            this.model = model
        },
        init: function(props){
            window.rosterData = props.data.args
            initFunc(this.model,  props.data.args)
            window.showType = props.data.args.showType;
            console.log(props.data)
        },
        update: function(props){
            if(props.data.event != 'initdata'){
                if(props.data.event == 'shiftInfo'){
                    insertShift(this.model, props.data.args.shiftInfo)
                }else if(props.data.event == 'copy'){
                    copyshift(props.data.args)
                }else if(props.data.event == 'paste'){
                    pasteShift(this.model, props.data.args)
                }else if(props.data.event == 'save'){
                    this.model.invoke('save', window.rosterData.rosterShiftList)
                }else{
                    updateHtml(this.model, props.data.args)
                    window.rosterData = props.data.args
                    window.showType = props.data.args.showType;
                    console.log(props.data)
                }
               
            }
        },
        destoryed: function(){
            $('#personnelSchedule-contextmenu-tips').remove()
        }
    }

    // Other Code
    var initFunc = function(model, rosterData) {
        KDApi.loadFile(['./css/index.css','./css/style.css','./js/index.js'], model, function() {
            updateHtml(model, rosterData) 
        })
    }

    var updateHtml = function(model, rosterData){
        KDApi.getTemplateStringByFilePath('./html/index.art', model, {rosterData: rosterData}).then(function(result) {
            model.dom.innerHTML = result
            // innerContentHTML(rosterData)
            initEvent(model, rosterData.showType);
        })   
    }
    var copyshift = function(iscopy){
        if(iscopy == 'success'){
            window.iscopy = true
        }else{
            window.iscopy = false
        }

    }
    var pasteShift = function(model, noparse){
        var elem = ''
        if(window.checkedRow){
            elem = $('.personnelSchedule-body-mian',model.dom).find('div[personid='+window.rowPersonId+']');
        }else{
            elem = $('.personnelSchedule-body-mian',model.dom).find('.personnelSchedule-body-clickCell')
        }
        if(noparse.paste){
            if(window.copyhtml){
                var shiftlist = JSON.parse($(window.copyhtml).attr('shiftlist'))
                var shiftdto = shiftlist ? shiftlist.shiftDTO : {}
                elem.each(function(){
                    var hasRoster =  $(this).attr('hasRoster')
                    var shiftid = JSON.parse($(this).attr('shiftlist')).id 
                    $(this).parent().html(window.copyhtml.cloneNode(true)) 
                    parserData(shiftid, window.showType, 'paste', hasRoster, shiftdto)           
                })
            } 
        }else{
            var noPastePerson = noparse.noPastePerson
            if(window.copyhtml){
                var shiftlist = JSON.parse($(window.copyhtml).attr('shiftlist')) 
                var shiftdto = shiftlist ? shiftlist.shiftDTO : {}
                elem.each(function(){
                    var personId = $(this).attr('personid')
                    var hasRoster =  $(this).attr('hasRoster')
                    if(noPastePerson.indexOf(personId) == -1){
                        $(this).parent().html(window.copyhtml.cloneNode(true))
                        var shiftid = JSON.parse($(this).attr('shiftlist')).id 
                        parserData(shiftid, window.showType, 'paste', hasRoster, shiftdto)  
                    }
                     
                })
            } 
        }
       
    }

  var insertShift = function(model, shiftinfo){
      var shiftHtml = '';
      for(var i=0;i<shiftinfo.length;i++){
        shiftHtml += '<div class="personnelSchedule-contextmenu-searchbox" insertindex='+i+'>'+
                            '<div class="personnelSchedule-contextmenu-searchcolor" style="background:'+shiftinfo[i].shiftColor+'"></div>'+
                            '<div class="personnelSchedule-contextmenu-searchname">'+shiftinfo[i].name+'</div>'+
                        '</div>'
      }
      $('#personnelSchedule-contextmenu-tips').find('.personnelSchedule-contextmenu-searchcontent').html(shiftHtml);
       
      $('.personnelSchedule-contextmenu-searchcontent').click(function(e){
         var insertindex = $(e.target).parent().attr('insertindex')
         var shiftdto = shiftinfo[insertindex];
         var elems = ''
         if(window.checkedRow){
            elems = $('.personnelSchedule-body-mian',model.dom).find('div[personid='+window.rowPersonId+']');  
         }else{
            elems = $('.personnelSchedule-body-mian',model.dom).find('.personnelSchedule-body-clickCell')
         }
         if( window.showType == 'SHOW_ALL'){
            elems.each(function(){
                var personId = $(this).attr('personid')
                var hasRoster = $(this).attr('hasRoster')
                var lock = $(this).attr('lock')
                var shiftlist = JSON.parse($(this).attr('shiftlist')) 
                var newshiftlist = updateData(shiftlist, shiftdto, 'all', hasRoster) 
                $(this).parent().html(
           '<div class="personnelSchedule-body-plan" hasRoster="'+hasRoster+'" lock="'+lock+'" personid="'+personId+'" shiftId="'+shiftdto.id+'" shiftlist='+JSON.stringify(newshiftlist).replace(/ /g,'&#32;')+'>'+
                '<div class="personnelSchedule-body-plancontent">'+
                '<div class="personnelSchedule-body-plancircle">'+
                   '<div class="icon-uniE90B personnelSchedule-body-planicon"></div>'+   
                '</div>'+
                '<div class="personnelSchedule-body-planname">'+shiftdto.name+'</div>'+
            '</div></div>'
                )
              
                 
            })
         }else if(window.showType == 'SHOW_PLAN'){
            elems.each(function(){
                var personId = $(this).attr('personid')
                var hasRoster = $(this).attr('hasRoster')
                var lock = $(this).attr('lock')
                var shiftlist = JSON.parse($(this).attr('shiftlist')) 
                var newshiftlist = updateData(shiftlist, shiftdto, 'plan', hasRoster) 
                $(this).parent().html(
                    '<div class="personnelSchedule-body-showPlan-plan" lock="'+lock+'" personid="'+personId+'" shiftId="'+shiftdto.id+'" shiftlist='+JSON.stringify(newshiftlist).replace(/ /g,'&#32;')+'>'+
                        '<div class="personnelSchedule-body-showPlan-plancontent">'+
                            '<div class="personnelSchedule-body-plancircle">'+
                                '<div class="icon-uniE90B personnelSchedule-body-planicon"></div>'+
                            '</div>'+
                            '<div class="personnelSchedule-body-showPlan-planname">'+shiftdto.name+'</div>'+
                    '</div></div>'
                )
                 
            })

         }else if(window.showType == 'SHOW_REAL'){
             elems.each(function(){
                var personId = $(this).attr('personid')
                var hasRoster = $(this).attr('hasRoster')
                var lock = $(this).attr('lock')
                var shiftlist = JSON.parse($(this).attr('shiftlist')) 
                var newshiftlist = updateData(shiftlist, shiftdto, 'real', hasRoster) 
                $(this).parent().html(
                    $(this).parent().html(
                        '<div class="personnelSchedule-body-showReal-plan" lock="'+lock+'" personid="'+personId+'" shiftId="'+shiftdto.id+'" shiftlist='+JSON.stringify(newshiftlist).replace(/ /g,'&#32;')+'>'+
                            '<div class="personnelSchedule-body-showReal-plancontent">'+
                                '<div class="personnelSchedule-body-plancircle">'+
                                    '<div class="icon-uniE90B personnelSchedule-body-planicon"></div>'+
                                '</div>'+
                                '<div class="personnelSchedule-body-showReal-planname">'+shiftdto.name+'</div>'+
                        '</div></div>'
                    )
                )
                 
            })
         }
         $('#personnelSchedule-contextmenu-tips').remove();
      })


  }
   //插入操作需要替换原数据中的saveType和shiftDTO的值
  var updateData = function(shiftlist, shiftdto, type, hasRoster){
    var rosterShiftList = window.rosterData.rosterShiftList
    var dateType = shiftlist.dateType
      if(type == 'plan' || type == 'all'){
          for(var i=0;i<rosterShiftList.length;i++){
              for(var j=0;j<rosterShiftList[i].planRosterList.length;j++){
                  if(shiftlist.id == rosterShiftList[i].planRosterList[j].id){
                    rosterShiftList[i].planRosterList[j].saveType = hasRoster == 'false' ? '2' : '3'
                    rosterShiftList[i].planRosterList[j].dateType = dateType == 'C' ? 'C' : shiftdto.shiftAtttribute
                    rosterShiftList[i].planRosterList[j].shiftDTO = shiftdto
                    return rosterShiftList[i].planRosterList[j]
                  }
              }
          }  
      }
       if(type == 'real' || type == 'all'){
        for(var i=0;i<rosterShiftList.length;i++){
            for(var j=0;j<rosterShiftList[i].realRosterList.length;j++){
                if(shiftlist.id == rosterShiftList[i].realRosterList[j].id){
                  rosterShiftList[i].realRosterList[j].saveType = hasRoster == 'false' ? '2' : '3'
                  rosterShiftList[i].planRosterList[j].dateType = dateType == 'C' ? 'C' : shiftdto.shiftAtttribute
                  rosterShiftList[i].realRosterList[j].shiftDTO = shiftdto
                  return rosterShiftList[i].realRosterList[j]
                }
            }
        }  
      }
  }
   
  //粘贴和锁定解锁后修改原数据中的值
  var parserData = function(shiftid, type, operatorType, hasRoster, shiftdto){
    var rosterShiftList = window.rosterData.rosterShiftList
      if(type == 'SHOW_PLAN' || type == 'SHOW_ALL'){
          for(var i=0;i<rosterShiftList.length;i++){
              for(var j=0;j<rosterShiftList[i].planRosterList.length;j++){
                  if(shiftid == rosterShiftList[i].planRosterList[j].id){
                      if(operatorType == 'paste'){
                        rosterShiftList[i].planRosterList[j].shiftDTO = shiftdto;
                        rosterShiftList[i].planRosterList[j].saveType = hasRoster == 'false' ? '2' : '3'
                      }else if(operatorType == 'lock'){
                        rosterShiftList[i].planRosterList[j].lock = true
                      }else if(operatorType == 'unlock'){
                        rosterShiftList[i].planRosterList[j].lock = false
                      }
                  }
              }
          }  
      }
       if(type == 'SHOW_REAL' || type == 'SHOW_ALL'){
        for(var i=0;i<rosterShiftList.length;i++){
            for(var j=0;j<rosterShiftList[i].realRosterList.length;j++){
                if(shiftid == rosterShiftList[i].realRosterList[j].id){
                    if(operatorType == 'paste'){
                        rosterShiftList[i].realRosterList[j].shiftDTO = shiftdto
                        rosterShiftList[i].realRosterList[j].saveType = hasRoster == 'false' ? '2' : '3'
                      }else if(operatorType == 'lock'){
                        rosterShiftList[i].realRosterList[j].lock = true
                      }else if(operatorType == 'unlock'){
                        rosterShiftList[i].realRosterList[j].lock = false
                      }
                }
            }
        }  
      }

  }

    var initEvent = function(model, showType){
        //滑轮拖动事件
        var moveWidth = 0;
        var el =  $('.personnelSchedule-header-line', model.dom);
          el.mousedown(function(e){ 
            //按下元素后，计算当前鼠标与对象计算后的坐标 
            var startX = e.clientX;
            var oldwidth = $('.personnelSchedule-header-hide', model.dom).width();
           
                $(document).mousemove(function(ev){
                    var endX = ev.clientX;
                    var newwidth = oldwidth + (endX - startX);
                    $('.personnelSchedule-header-hide', model.dom).css('width', newwidth + 'px');
                    $('.personnelSchedule-body-hide', model.dom).css('width', newwidth + 'px');
                    addScrollBar(model, newwidth)
                    var minWidth = $('.personnelSchedule-header-arrange', model.dom).width();
                    // console.log(minWidth)
                    // if(newwidth < 0 || minWidth < 110){
                    //     $(document).unbind("mousemove")  
                    // }
                })
              $(document).mouseup(function(){
                $(document).unbind("mousemove").unbind("mouseup") 
              })
            
            //防止默认事件发生 
            e.preventDefault() 
          }); 
        addScrollBar(model, moveWidth);
        personInfoHoverEvent(model);
        CellClickEvent(model, showType);
        hasScroll()
    }
      //添加横向滚动条 
      var addScrollBar = function(model, moveWidth){
         $('.personnelSchedule-body-left', model.dom).css('width', moveWidth+200+'px');
         $('.personnelSchedule-body-left-scroll', model.dom).css('width', moveWidth+'px');
         $('.personnelSchedule-body-right', model.dom).css('width', 'calc(100% - '+ (moveWidth+200) + 'px)');
         //人员信息宽度
         var personHideWidth = $('.personnelSchedule-body-hide', model.dom).width();
         var personInfoWidth = $('.personnelSchedule-body-info', model.dom).width();
         //排班日历宽度
         var contentWidth = $('.personnelSchedule-body-content', model.dom).width();
         var arrangeWidth = $('.personnelSchedule-body-arrange', model.dom).width();
         if(personInfoWidth > personHideWidth){
            $('.personnelSchedule-body-left-info', model.dom).css('width', personInfoWidth+'px')
            syncScroll(model, 'left');
         }else{

         }
         if(contentWidth > arrangeWidth) { 
             $('.personnelSchedule-body-right-info', model.dom).css('width', contentWidth+'px')
             syncScroll(model, 'right');
         }else{
             $('.personnelSchedule-body-scroll', model.dom).hide();
         }
      }
     //滚动条同步事件
     var syncScroll = function(model, direction){
         if(direction == 'left'){
            $('.personnelSchedule-body-left-scroll', model.dom).scroll(function(){
                $('.personnelSchedule-header-hide', model.dom).scrollLeft($(this).scrollLeft());
                $('.personnelSchedule-body-hide', model.dom).scrollLeft($(this).scrollLeft());
               })
         }else{
            $('.personnelSchedule-body-right', model.dom).scroll(function(){
                $('.personnelSchedule-header-arrange', model.dom).scrollLeft($(this).scrollLeft());
                $('.personnelSchedule-body-arrange', model.dom).scrollLeft($(this).scrollLeft());
               })
         }
     }
    //鼠标悬浮显示人员具体信息
     var personInfoHoverEvent = function(model){  
        $(model.dom).on('mouseenter mouseleave', '.personnelSchedule-person-name .icon-2',  function(e){
            if(e.type == 'mouseenter'){
                var top = $(e.target).offset().top - 20;
                var left = $(e.target).offset().left + 20;
                var personinfo = JSON.parse($(e.target).attr('personinfo')); 
                $('body').append(
                  '<div class="personnelSchedule-person-tips" id="personnelSchedule-person-tips">'+
                  '<div class="personnelSchedule-person-arrow"></div><div class="personnelSchedule-person-code">人员编号：'+personinfo.number+'</div>'+
                  '<div class="personnelSchedule-person-department">部门：'+personinfo.department+'</div><div class="personnelSchedule-person-position">职位：'+personinfo.position+'</div>'+
                  '<div class="personnelSchedule-person-attendance">考勤组：'+personinfo.org+'</div><div class="personnelSchedule-person-time">(20210312-20210312)</div></div>'
              )
             
                $('#personnelSchedule-person-tips').css('top', top+'px');
                $('#personnelSchedule-person-tips').css('left', left+'px');
                }else if(e.type == 'mouseleave'){
                    $('body').find('#personnelSchedule-person-tips').remove() 
                }
                })
    }

    //判断内容区域div是否出现了垂直滚动条
    var hasScroll = function(dom){

        if ($('.personnelSchedule-body',dom).height() < $('.personnelSchedule-body-mian',dom).height()) {
            $('.personnelSchedule-header-arrange', dom).find('.personnelSchedule-body-content').append(
                '<div style="width: 8px"></div>'
            )
        }
    }
    var CellClickEvent = function(model, showType){
         //是否按住ctrl键的标识
         var isCtrl = false;
         //按住ctrl键进行多选
         $(window).keydown(function(e){
             if(e.ctrlKey){
                 isCtrl = true;
             }
         }).keyup(function(){
             isCtrl = false;
         })  

         var elemClass = ''
         if(showType == 'SHOW_ALL'){
            elemClass = '.personnelSchedule-body-plan,.personnelSchedule-body-actual'
         }else if(showType == 'SHOW_PLAN'){
            elemClass = '.personnelSchedule-body-showPlan-plan'
         }else if(showType == 'SHOW_REAL'){
            elemClass = '.personnelSchedule-body-showReal-plan'     
         }else if(showType == 'SHOW_NO'){
             return
         }
         //按住c键复制v键粘贴
         $(window).on('keyup', function(event){
            var elem = $('.personnelSchedule-body-mian',model.dom).find('.personnelSchedule-body-clickCell')
             if(event.keyCode == 67 && elem.length == 1){
                window.copyhtml = elem[0].cloneNode(true); 
                window.shiftId = $(elem).attr('shiftid');
                elem.removeClass('personnelSchedule-body-clickCell')
                $('#personnelSchedule-contextmenu-tips').remove()
                model.invoke('copy', window.shiftId) 
             }else if(event.keyCode == 86){
                if(window.iscopy){
                    var personIds = [];
                    elem.each(function(){
                     personIds.push($(this).attr('personid')) 
                    })
                    $('#personnelSchedule-contextmenu-tips').remove()
                    model.invoke('paste',{personIds: unique2(personIds),shiftId: window.shiftId})
                }
               
             }
         })
         //单个单元格点击事件 
         $(model.dom).on('click', elemClass, function(){
            if($(this).hasClass('personnelSchedule-body-clickCell')){
                $(this).removeClass('personnelSchedule-body-clickCell')
            }else{
                if(!isCtrl){  //没有按住ctrl键，则每次点击只有一个单元格是选中状态
                    $(elemClass,model.dom).removeClass('personnelSchedule-body-clickCell');
                }
                $(this).addClass('personnelSchedule-body-clickCell')
            }
            $('body').find('#personnelSchedule-contextmenu-tips').remove() 
         })   
        //单个单元格悬浮事件
        $(model.dom).on('mouseover mouseout', elemClass, function(e){
            if(e.type == 'mouseover'){
                if($(this).hasClass('nobackground-color')){
                    return ;
                }
                    var top = $(this).offset().top + 48;
                    var left = $(this).offset().left + 4;
                    var shiflist = JSON.parse($(this).attr('shiftlist'));
                    var shiftdto = shiflist.shiftDTO;
                    var dateType = '';
                    switch(shiflist.dateType){
                        case 'A':
                            dateType = '工作日'
                            break;
                        case 'B':
                            dateType = '休息日'
                            break;
                        case 'C':
                            dateType = '节假日'
                            break;
                        default:
                            dateType = '工作日'
                    } 
    
                    var timehtml = '',shiftEntryList = shiftdto.shiftEntryList;
                    if(shiftEntryList){
                    for(var i=0;i<shiftEntryList.length;i++){
                        var outWorkType = shiftEntryList[i].outWorkType == 'D' ? '上班' : '休息';
                        timehtml += '<div class="personnelSchedule-cell-time">'+outWorkType+'：'+shiftEntryList[i].shiftStartDateStr+'~'+shiftEntryList[i].shiftEndDateStr+'</div>'
                    }
                    $('body').append(
                      '<div class="personnelSchedule-cell-tips" id="personnelSchedule-cell-tips"><div class="personnelSchedule-cell-arrow"></div>'+
                      '<div class="personnelSchedule-cell-name">'+shiftdto.name+'</div><div class="personnelSchedule-cell-contentbox">'+
                      '<div class="personnelSchedule-cell-country">中国</div><div class="personnelSchedule-cell-workday">'+dateType+'</div><div class="personnelSchedule-cell-frequency">'+shiftdto.checkCardTimes+'次卡</div></div>'+
                      '<div class="personnelSchedule-cell-timebox">'+timehtml+
                      '</div><div class="personnelSchedule-cell-dashed"></div><div class="personnelSchedule-cell-repeat">班次时段存在重叠</div></div>'
                  )
                 
                    $('#personnelSchedule-cell-tips').css('top', top+'px');
                    $('#personnelSchedule-cell-tips').css('left', left+'px');
                }       
            }else if(e.type == 'mouseout'){
                $('body').find('#personnelSchedule-cell-tips').remove()  
            }

        })
        //单个单元格鼠标右键事件
        //阻止浏览器右键默认事件
        $(model.dom).on('contextmenu', '.personnelSchedule-body-person', function(){
            return false;
        })
        $(model.dom).on('contextmenu', elemClass, function(){
            return false;
        })
       //点击除自定义控件之外的区域，右键弹框消失
       $(document).unbind()
       $(document).mouseup(function(e) {
        var elem = e.target;
        if($(elem).is('#personnelSchedule-contextmenu-tips') || $(elem).is('#personnelSchedule-contextmenu-tips *')){

        }else{
            var _con = $(model.dom);   	// 设置目标区域
            if(!_con.is(e.target) && _con.has(e.target).length === 0){ 	
                $('#personnelSchedule-contextmenu-tips').remove()
            }
        }
    })
         //单元格右键点击事件
         $(model.dom).on('mousedown', elemClass, function(e){
            if(3 == e.which ){
                $('body').find('#personnelSchedule-contextmenu-tips').remove()
                window.checkedRow = false  //选中整行的状态置为false
                var top = $(this).offset().top +48;
                var left = $(this).offset().left + 4;
                // $(elemClass,model.dom).removeClass('personnelSchedule-body-clickCell'); //右键只有当前单元格为选中状态
                $(this).addClass('personnelSchedule-body-clickCell');
                var elem = $('.personnelSchedule-body-mian',model.dom).find('.personnelSchedule-body-clickCell')
                var elemlen = elem.length;
                var disabledtype = '';
                if(elemlen > 1){
                    disabledtype = 'copydisable'  //选中的单元格数量大于1，则复制功能不可用
                }
                contextmenuBox(top, left, model, disabledtype, false)
                }else if(1 == e.which){
                    $('body').find('#personnelSchedule-contextmenu-tips').remove() 
                }
         })
      
          //左侧姓名栏右键点击选中整行
          $(model.dom).on('mousedown', '.personnelSchedule-body-person', function(e){
            if(3 == e.which ){
                $('body').find('#personnelSchedule-contextmenu-tips').remove()
                var top = $(this).offset().top +48;
                var left = $(this).offset().left + 200;
                $('.personnelSchedule-body-column', model.dom).css("border", "none")
                $(this).parent().parent().css("border", "0.5px solid #5E80EB")
                window.checkedRow = true
                window.rowPersonId = $(this).parent().parent().attr('rowpersonid')
                contextmenuBox(top, left, model, 'copydisable',  window.checkedRow)
                }else if(1 == e.which){
                    $('body').find('#personnelSchedule-contextmenu-tips').remove() 
                    $('.personnelSchedule-body-column', model.dom).css("border", "none")
                    $('.personnelSchedule-body-column', model.dom).css("border-bottom", "1px solid #d9d9d9")
                }
          })

          $('.personnelSchedule-count', model.dom).click(function(){
            $('.personnelSchedule-count-body', model.dom).toggle()
          })
    }
    var unique2 = function (arr){
        var hash=[];
        for (var i = 0; i < arr.length; i++) {
          if(arr.indexOf(arr[i])==i){
          hash.push(arr[i]);
          }
        }
        return hash;
       }
    
    //右键菜单样式,checkedRow为是否选中整行的标识
    var contextmenuBox = function(top, left, model, disabledtype, checkedRow){
        var isdisabled = disabledtype == 'copydisable' ? 'personnelSchedule-contextmenu-disabled' : ''
        $('body').find('#personnelSchedule-cell-tips').remove()
        $('body').append(
          '<div class="personnelSchedule-contextmenu-tips" id="personnelSchedule-contextmenu-tips"><div class="personnelSchedule-contextmenu-left">'+
          '<div class="personnelSchedule-contextmenu-insert"><div class="personnelSchedule-contextmenu-icon kdfont kdfont-charubanci"></div><div class="personnelSchedule-contextmenu-name" contextmenuname="insertroster">插入班次</div></div>'+
          '<div class="personnelSchedule-contextmenu-insert '+isdisabled+'"><div class="personnelSchedule-contextmenu-icon kdfont kdfont-fuzhi4"></div><div class="personnelSchedule-contextmenu-name" contextmenuname="copyroster">复制班次</div></div>'+
          '<div class="personnelSchedule-contextmenu-insert"><div class="personnelSchedule-contextmenu-icon kdfont kdfont-zhantie2"></div><div class="personnelSchedule-contextmenu-name" contextmenuname="parseroster">黏贴班次</div></div>'+
          '<div class="personnelSchedule-contextmenu-insert"><div class="personnelSchedule-contextmenu-icon kdfont kdfont-suoding5"></div><div class="personnelSchedule-contextmenu-name" contextmenuname="lockroster">锁定</div></div>'+
          '<div class="personnelSchedule-contextmenu-insert"><div class="personnelSchedule-contextmenu-icon kdfont kdfont-jiesuo3"></div><div class="personnelSchedule-contextmenu-name" contextmenuname="unlockroster">解锁</div></div>'+
          '<div class="personnelSchedule-contextmenu-insert personnelSchedule-contextmenu-disabled"><div class="personnelSchedule-contextmenu-icon kdfont kdfont-huanban"></div><div class="personnelSchedule-contextmenu-name" contextmenuname="exchangeroster">换班</div></div>'+
          '</div><div class="personnelSchedule-contextmenu-right"><div class="personnelSchedule-contextmenu-search"><div class="personnelSchedule-contextmenu-searchicon icon-9"></div>'+
          '<input class="personnelSchedule-contextmenu-searchinput" placeholder="请输入需要搜索的内容"/></div>'+
          '<div class="personnelSchedule-contextmenu-searchcontent"></div></div></div>'
      )
     
        $('#personnelSchedule-contextmenu-tips').css('top', top+'px');
        $('#personnelSchedule-contextmenu-tips').css('left', left+'px');

        $('.personnelSchedule-contextmenu-left').click(function(e){
            e.stopPropagation();
            var target = $(e.target).attr('contextmenuname');
            var elem = ''
            var personIds = [];
            if(checkedRow){
                elem = $('.personnelSchedule-body-mian',model.dom).find('div[personid='+window.rowPersonId+']');
            }else{
                elem = $('.personnelSchedule-body-mian',model.dom).find('.personnelSchedule-body-clickCell')
            }
            if(target == 'insertroster'){
                if(checkedRow){
                    personIds.push(window.rowPersonId)  
                }else{
                    elem.each(function(){
                        personIds.push($(this).attr('personid')) 
                       })
                }
                model.invoke('getShift',unique2(personIds))
            }else if(target == 'copyroster'){
                    window.copyhtml = elem[0].cloneNode(true); 
                    window.shiftId = $(elem).attr('shiftid');
                    elem.removeClass('personnelSchedule-body-clickCell')
                    $('#personnelSchedule-contextmenu-tips').remove()
                    model.invoke('copy', window.shiftId) 
            }else if(target == 'parseroster'){
                if(window.iscopy){
                    if(checkedRow){
                        personIds.push(window.rowPersonId)
                    }else{
                        elem.each(function(){
                            personIds.push($(this).attr('personid')) 
                           })
                    }
                    
                    model.invoke('paste',{personIds: unique2(personIds),shiftId: window.shiftId})
                }
                $('#personnelSchedule-contextmenu-tips').remove()
            }else if(target == 'lockroster'){
                elem.each(function(){
                    if($(this).attr('lock') == 'false'){
                        var shiftlist = JSON.parse($(this).attr('shiftlist'))
                        $(this).css('opacity','0.3')
                        $(this).attr('lock','true')
                        $(this).parent().find('.personnelSchedule-body-lock').remove()
                        $(this).parent().append(
                            '<div class="kdfont kdfont-suoding5 personnelSchedule-body-lock"></div>'
                        )
                        $(this).removeClass('personnelSchedule-body-clickCell')
                        parserData(shiftlist.id, window.showType, 'lock')
                    }
                    
                })
                $('#personnelSchedule-contextmenu-tips').remove() 
            }else if(target == 'unlockroster'){
                elem.each(function(){
                    if($(this).attr('lock') == 'true'){
                        var shiftlist = JSON.parse($(this).attr('shiftlist'))
                        $(this).css('opacity','1')
                        $(this).attr('lock','false')
                        $(this).parent().find('.personnelSchedule-body-lock').remove()
                        $(this).removeClass('personnelSchedsule-body-clickCell')
                        parserData(shiftlist.id, window.showType, 'unlock')
                    }
                   
                }) 
                $('#personnelSchedule-contextmenu-tips').remove() 
            }

        })
       
    }
    KDApi.register('wtcroster', PersonnelSchedule)
})(window.KDApi, jQuery) 
