require('../mock')
const $ = require('jquery');
// require("../css/partDetailControl.less");
require("../../css/dataAnalysis/improveStatistics.less");

require('../modal.js');
const {api} = require('../api.js')
const {post,getCookie,getUrlArgObjectNew,emptyBox,setDatePicker,getPickerDate,downloadExportedData,expJson,listenScroll} = require('../utils.js')
require('../../resource/jquery-ui/jquery-ui.min.js');
require('../../resource/jquery-ui/jquery-ui.min.css');
const arrowLeft= require("../../images/arrow_left.png")

const arrowRight = require("../../images/arrow_right.png");
listenScroll()
let isPlacefile = getCookie('isPlacefile')||1

let deptName="",deptNameTemp="", behospitalCode="", behospitalCodeTemp="",deptId="",deptIdTemp="",doctorName="",doctorId="",
	data_desc=["totalModeNum"],data_asc=[];
let srcUrl = $("#contentIframe",parent.document).attr("src")
let deptIdParam = getUrlArgObjectNew("deptId",srcUrl)||""
let deptNameParam = getUrlArgObjectNew("deptName",srcUrl)||""
let startDateParam = getUrlArgObjectNew("startDate",srcUrl)||""
let endDateParam = getUrlArgObjectNew("endDate",srcUrl)||""

$(function() {
  const  iconCalenBlue= require("../../images/icon_calen_blue.png")
  const  iconCalenGrey= require("../../images/icon_calen_grey.png")
  //日期控件事件绑定
  $('.datapickerBox').append(`<img class="iconCalen" src=${iconCalenGrey} />`)
  $('.iconCalen').on("mouseenter", function(e){
	$(this).attr("src", iconCalenBlue)
  })
  $('.iconCalen').on("mouseleave", function(e){
	$(this).attr("src", iconCalenGrey)
  })
  $('.iconCalen').on("click", function(e){
	$(this).parent().find("input").focus()
  })
  //日期默认传参/当年
  if(startDateParam){
	const startDate = new Date(startDateParam);
	const endDate =  new Date(endDateParam);
	setDatePicker($("#datepicker"),$("#datepicker2"),"",startDate,endDate);
  }else{
	setDatePicker($("#datepicker"), $("#datepicker2"), 1);
  }
  //父页面传参
  if(deptIdParam){
	deptId =deptIdTemp=deptIdParam;
	deptName=deptNameTemp=deptNameParam;
	$(".selectDept").val(deptNameParam).attr("title",deptNameParam);
	$(".partTitle .titlePic ").html(`<a class="consoleP" href="useStatistics.html">临床质控使用统计 / </a>科室使用详情`);
  }

  //初始列表数据获取
  getTabData(1);
  //导出事件绑定
  $('.export').click(function(){
	const param = getFilterParam();
	if(param.startDate > param.endDate){
	  $.alerModal({"message":'开始时间不能大于结束时间~',type:"tip",time:'1000',isFather: true, fatherWrapper: $("#mainBox", parent.document)});
	  return
	}
	expJson(api.getQcClickInnerPageByExport,param,{noPlacefile:true}).then(res =>{
	  downloadExportedData(res.data,"临床质控使用统计详情"+startDateParam.substr(0,10).replace(/\//g,'')+"-"+endDateParam.substr(0,10).replace(/\//g,'')+".xls");
	})
  })
  //给每一个标题绑定一个排序事件
  $("th[code]").on("click",function(e){
	const code=$(this).attr("code");
	if(data_asc==code){
	  data_desc=[code];
	  data_asc=[];
	  $("th[code]").removeClass("asc desc");
	  $(this).addClass("desc");
	}else{
	  data_desc=[];
	  data_asc=[code];
	  $("th[code]").removeClass("asc desc");
	  $(this).addClass("asc");
	}
	getTabData(1);
  });
  //重置
  $(".abnormalClear").click(function(){
	data_desc=['totalModeNum']
	data_asc=[]
	setDatePicker($("#datepicker"),$("#datepicker2"),"",startDateParam,endDateParam);
	$("#doctorNum,#doctorName").val("");
	$("th[code]").removeClass("desc asc");
	$("th[code='totalModeNum']").addClass("desc");
	behospitalCode =behospitalCodeTemp= ""
	deptName = deptNameTemp = deptNameParam
	deptId = deptIdTemp = deptIdParam
	doctorId = "";
	doctorName = "";
	getTabData(1)
  })

//查询
  $(".filter").on("click", function(e){
	let startDate=getPickerDate($("#datepicker"),1)
	let endDate=getPickerDate($("#datepicker2"),2)
	if(startDate > endDate){
	  $.alerModal({"message":'开始时间不能大于结束时间~',type:"tip",time:'1000',isFather: true, fatherWrapper: $("#mainBox", parent.document)});
	  return
	}
	deptId = deptIdTemp;
	deptName = deptNameTemp;
	doctorId = $("#doctorNum").val().trim();
	doctorName = $("#doctorName").val().trim();
	getTabData(1)
  })
});

function getFilterParam(current){
  let startDate=getPickerDate($("#datepicker"),1)
  let endDate=getPickerDate($("#datepicker2"),2)
  const param = {
	//"isPlacefile":isPlacefile,
	"deptId": deptId=="null"?"":deptId||"", //科室id
	"deptName": deptName=="全部"||deptName=="全院"?"":deptName||"",
    "doctorId":doctorId,
    "doctorName":doctorName,
	"asc":data_asc,     //升序
	"desc":data_desc,       //降序
	"startDate":startDate,
	"endDate":endDate,
	current,
	size:15
  }
  return param;
}
/*$(".partTitle a").click(function(){
    //初始菜单选中
    $(".menu .page",parent.document).removeClass("active").eq(0).addClass("active");
    $(".container",parent.document).addClass("console-cont");
})
//判断有无某一权限
  function hasData(data){
    let trdObj = JSON.parse(getLocal("trdObj"))
    let lis = trdObj['YH-ZKK-TMQXGSTJ']
    if(!lis){
      return false
    }
    if(lis.indexOf(data)>-1){//有权限
        return true
    }
    return false;
}*/
function getTabData(activePage){
  const param = getFilterParam(activePage);
  $('.pagination').html("")
  $('.tbody').html(emptyBox('努力加载中...','',1))
  post(api.getQcClickDept,param,{noPlacefile:true}).then(res =>{
	if(res.data.code == '0'){
	  const data = res.data.data;
	  const detail =data.records||[];
	  const totalPage = data.pages;
	  const totalNum = data.total;
	  renderTab(detail,detail.hospitalId,activePage);
	  if(totalNum>0){
		renderPagination(totalPage,Number(activePage),totalNum)
	  }
	}else{
	  $.alerModal({"message":res.data.msg,type:"tip",time:'1000',isFather: true, fatherWrapper: $("#mainBox", parent.document)});
	}
  }).catch((e) =>{

  })
}
//表格dom渲染
function renderTab(data,hisId,activePage){
  let str = ``
  for(let i = 0; i < data.length; i++){
	const item = data[i];

	str += `
            <tr data-index=${i} data-deptid="${item.deptId}" data-dept="${item.deptName}">
                <td  class="textCenter">${(activePage-1)*15 + i+1}</td>
                <td >${item.deptName || "-"}</td>
                <td >${item.doctorId || "-"}</td>
                <td >${item.doctorName || "-"}</td>
                <td>${item.singleModeNum}</td>
                <td>${item.totalModeNum}</td>
            </tr>
        `
  }
  $('.tbody').html(str?str:emptyBox())
}

/*function loading(){
    const str = `
    <div class="loadingBox">
            <div class="mask"></div>
            <img class="loadingImg" src=${loadingImg}  alt="loading" />
        </div>
    `
    $("#mainBox", parent.document).append(str)
}
function hideLoading(){
    $("#mainBox .loadingBox", parent.document).remove()
}*/

//分页渲染
function renderPagination(totalPage,activePage,totalNum){
  let str = `<span class="totalSum">共${totalPage}页/${totalNum}条数据</span><span class="prePage"><img class="arrowPage" src=${arrowLeft} /></span>`
  if(totalPage <= 6){
	for(let i = 1; i <= totalPage; i++){

	  str += `<span class="pageNum ${'page'+i}" data-page=${i}>${i}</span>`
	}
  }else{
	if(activePage <= 3){ //选中页数小于4
	  for(let i = 1; i <= 4; i++){
		str += `<span class="pageNum ${'page'+i}" data-page=${i}>${i}</span>`
	  }
	  str += `<span class="more" >...</span>`
	  str += `<span class="pageNum ${'page'+totalPage}" data-page=${totalPage}>${totalPage}</span>`

	} else if(activePage > totalPage -3){
	  str += `<span class="pageNum page1" data-page=1>1</span>`
	  str += `<span class="more" >...</span>`
	  str += `<span class="pageNum ${'page'+(totalPage-3)}" data-page=${totalPage -3}>${totalPage -3}</span>`
	  str += `<span class="pageNum ${'page'+(totalPage-2)}" data-page=${totalPage -2}>${totalPage -2}</span>`
	  str += `<span class="pageNum ${'page'+(totalPage-1)}" data-page=${totalPage -1}>${totalPage -1}</span>`
	  str += `<span class="pageNum ${'page'+totalPage}" data-page=${totalPage}>${totalPage}</span>`
	} else {
	  str += `<span class="pageNum page1" data-page=1>1</span>`
	  str += `<span class="more" >...</span>`
	  str += `<span class="pageNum ${'page'+(activePage -1)}" data-page=${activePage -1}>${activePage -1}</span>`
	  str += `<span class="pageNum ${'page'+activePage}" data-page=${activePage}>${activePage}</span>`
	  str += `<span class="pageNum ${'page'+(activePage +1)}" data-page=${activePage +1}>${activePage +1}</span>`
	  str += `<span class="more" >...</span>`
	  str += `<span class="pageNum ${'page'+totalPage}" data-page=${totalPage}>${totalPage}</span>`
	}

  }
  str += `<span class="nextPage"><img class="arrowPage" src=${arrowRight} /></span>`
  $('.pagination').html(str)
  $('.page' + activePage).addClass('activePage')
  $(".pageNum").on("click", function(e){
	const activePageNow = Number($(this).attr('data-page'))
	getTabData(activePageNow)
	// renderPagination(totalPage,activePageNow,totalNum)
  })
  $(".prePage").on("click", function(e){
	let activePageNow = Number($(".activePage").attr('data-page'))
	if(activePageNow > 1){
	  activePageNow--
	  getTabData(activePageNow)
	  // renderPagination(totalPage,activePageNow,totalNum)
	}
  })
  $(".nextPage").on("click", function(e){
	let activePageNow = Number($(".activePage").attr('data-page'))
	if(activePageNow < totalPage){
	  activePageNow++
	  getTabData(activePageNow)
	  // renderPagination(totalPage,activePageNow,totalNum)
	}
  })

}

