//求并集
export function getUnion(a, b) {
  if (a.constructor === Array && b.constructor === Array) {
    let set1 = new Set(a);
    let set2 = new Set(b);
    return Array.from(new Set([...set1, ...set2]));
  }
  return null;
}
//求差集
export function getDifference(a, b) {
  if (a.constructor === Array && b.constructor === Array) {
    let set1 = new Set(a);
    let set2 = new Set(b);
    return Array.from(new Set([...set1].filter(x => !set2.has(x))));
  }
  return null;
}
//求交集
export function getIntersect(a, b) {
  if (a.constructor === Array && b.constructor === Array) {
    let set1 = new Set(a);
    let set2 = new Set(b);
    return Array.from(new Set([...set1].filter(x => set2.has(x))));
  }
  return null;
}
//获取id的list
export function getIdList(data) {
  let list = [];
  if (typeof data === 'undefined') {
    return list;
  }
  if (data.constructor === Array) {
    for (let i = 0; i < data.length; i++) {
      list.push(data[i].id);
    }
  }
  return list;
}
//区域
export function getDivision(data) {
  if (data === null || data == 'NaN') {
    let empty = [];
    return empty;
  }
  let list = data.split('|');
  let num = [];
  for (let i = 0; i < list.length; i++) {
    num[i] = parseInt(list[i]);
  }
  //console.log(list);
  return num;
}
export function getLevelList(data) {
  let level = [];
  level[0] = {
    id: '-1',
    label: '全部医院等级',
  }
  for (let i = 0; i < data.length; i++) {
    let obj = {
      id: data[i].no,
      label: data[i].value,
    }
    level.push(obj);
  }
  //console.log(level);
  return level;
}
function inTags(id, tags) {
  let flag = false;
  for (let i = 0; i < tags.length; i++) {
    if (tags[i].key == id) {
      flag = true;
    }
  }
  return flag;
}
export function inOrganization(list, id) {
  let flag = false;
  if (list.length == 0) {
    flag = true;
  }
  for (let i = 0; i < list.length; i++) {
    if (list[i].id != id) {
      flag = true;
    }
  }
  return flag;
}
export function initTags(value) {
  let tags = [];
  for (let j = 0; j < value.length; j++) {
    let tagsComponent = {};
    tagsComponent.value = value.value;
    tagsComponent[j].name = this.optionsComponent[j].label;
  }
  return tags;
}
//机构列表
export function getOrganizationList(data) {
  let list = [];
  list[0] = {
    value: '0',
    label: '全部机构',
  };
  for (let i = 0; i < data.length; i++) {
    let obj = {
      value: data[i].key,
      label: data[i].name,
    }
    list.push(obj);
  }
  return list;
}
//机构列表
export function getHospitalList(data) {
  let list = [];
  list[0] = {
    value: 0,
    label: '全部医院',
  };
  for (let i = 0; i < data.length; i++) {
    let obj = {
      value: data[i].id,
      label: data[i].name,
    }
    list.push(obj);
  }
  //console.log(list);
  return list;
}

//拼树
export function changeTags(option) {
  let list = [];
  // for(let i=0;i<option.length;i++) {
  //   console.log(option[i].label);
  //   let obj = {
  //     name: option[i].name,
  //     key: option[i].key,
  //   }
  //   list.push(obj);
  // }
  console.log(option);
  return option;
}
// 递归删除多余的节点
function handlerAction(treeData, selData) {
  treeData.forEach((elem, index) => {
    if (!isValuable(elem.value, selData)) {
      treeData.splice(index, 1)
      handlerAction(treeData, selData)
    } else {
      if (elem.children) {
        handlerAction(elem.children, selData)
      }
    }
  })
  return treeData;
}
function isValuable(val, selData) {
  for (let i = 0; i < selData.length; i++) {
    if (selData[i]['value'].indexOf(val) >= 0) {
      return true
    }
  }
  return false
}
function changeTreeKey(oData, orgKey, targetKey) {
  oData = [...oData]
  oData.forEach(elem => {
    elem[targetKey] = elem[orgKey]
    delete elem[orgKey]
    if (elem.children) {
      changeTreeKey(elem.children, orgKey, targetKey)
    }
  })
  return oData
}
function deleteTreeKey(oData, delKeys) {
  oData = [...oData]
  oData.forEach(elem => {
    delKeys.forEach(key => {
      delete elem[key]
    })
    if (elem.children) {
      deleteTreeKey(elem.children, delKeys)
    }
  })
  return oData
}
function deleteEmptyChild(oData) {
  oData = [...oData]
  oData.forEach(elem => {
    if (elem.children && elem.children.length == 0) {
      delete elem.children
    } else if (elem.children) {
      deleteEmptyChild(elem.children)
    }
  })
  return oData
}
function addEmpty(data) {
  let after = [];
  after[0] = {
    label: "全部地区",
    value: "0",
  }
  for (let i = 0; i < data.length; i++) {
    after.push(data[i]);
  }
  return after;
}
export function treeHandler(treeData, selData) {
  //console.log('treeData',treeData,'selData',selData);
  treeData = deleteTreeKey(treeData, ['status', 'disabled'])
  treeData = changeTreeKey(treeData, 'id', 'value')
  selData = changeTreeKey(selData, 'name', 'label')
  selData = changeTreeKey(selData, 'key', 'value')
  let endData = deleteEmptyChild(handlerAction(treeData, selData));
  // console.log(treeData, selData)
  //return handlerAction(treeData, selData);
  let returnData = addEmpty(endData);
  return returnData;
}

//小生态
export function getEcologyList() {
  let dataList = [{
    id: '123',
    name: '上海第一人民医院',
    hospitalLevelName: '二甲医院',
    provinceName: '上海市',
    cityName: '上海市',
    countyName: '浦东新区',
    townName: '--',
  }, {
    id: '124',
    name: '上海第二人民医院',
    hospitalLevelName: '二甲医院',
    provinceName: '上海市',
    cityName: '上海市',
    countyName: '浦东新区',
    townName: '--',
  }, {
    id: '125',
    name: '上海第三人民医院',
    hospitalLevelName: '二甲医院',
    provinceName: '上海市',
    cityName: '上海市',
    countyName: '浦东新区',
    townName: '--',
  }]
  return dataList;
}
export function getEcologySelect(data) {
  // let ecologyist = [{
  //   label: '上海小生态',
  //   value: '1',
  // },{
  //   label: '北京小生态',
  //   value: '2',
  // }];
  let ecologyist = [];
  for (let i = 0; i < data.length; i++) {
    let obj = {
      label: data[i].name,
      value: data[i].id,
    }
    ecologyist.push(obj);
  }
  return ecologyist;
}
export function isOverlap(startA, endA, startB, endB) {
  let maxStart = Math.max(startA, startB);
  let minEnd = Math.min(endA, endB);
  console.log('maxStart', maxStart, 'minEnd', minEnd);
  if (maxStart <= minEnd) {
    return true;
  } else {
    return false;
  }
}
export function setRegionOption(data) {
  let option = [];
  option[0] = { id: 0, value: 0, label: "全部", leaf: true };
  for (let i = 0; i < data.length; i++) {
    let obj = data[i];
    obj.label = data[i].provinceName;
    obj.value = data[i].provinceId;
    option.push(obj);
  }
  return option;
}
export function setRegionOption2(data) {
  let option = [];
  option[0] = { id: 0, value: '0', label: "全部地区", leaf: true };
  for (let i = 0; i < data.length; i++) {
    let obj = data[i];
    obj.label = data[i].provinceName;
    obj.value = data[i].provinceId;
    option.push(obj);
  }
  return option;
}
export function setRegionOptionNew(data) {
  let option = [];
  for (let i = 0; i < data.length; i++) {
    let obj = data[i];
    obj.label = data[i].label;
    obj.value = data[i].id;
    if(obj.degree == 5 || obj.degree == 0 || obj.label == "全部") {
    // if(obj.degree == 4 ) {
      obj.leaf = true;
    }
    option.push(obj);
  }
  return option;
}
export function setAdministrativeId(value) {
  let areaId = '000';
  for (let i = 0; i < value.length; i++) {
    areaId += '_' + value[i];
  }
  return areaId;
}
export function getRegionOption() {
  let option = [{
    value: 1,
    label: '浙江',
    children: [{
      value: 2,
      label: '杭州',
      children: [{
        value: 120115000000,
        label: '西湖区'
      }, {
        value: 5,
        label: '东湖区'
      }]
    }]
  }];
  return option;
}
export function getLearnOrganization(data) {
  if (data.length == 0) {
    let arr = [];
    return arr;
  }
  // let organization = [
  //   {
  //     label: "全部",
  //     value: 0
  //   },
  // ];
  let organization = [];
  for (let i = 0; i < data.length; i++) {
    let obj = {
      label: data[i].hospitalName,
      value: data[i].hospitalId,
      index: i,
    };
    organization.push(obj);
  }
  return organization;
}
export function getPicOption(title, legendData, colorData, seriesData) {
  let option = {
    title: {
      text: title,
      x: "left",
      left: '20px',
      top: "20px",
    },
    tooltip: {
      trigger: 'item',
      // formatter: "{a} <br/>{b} : {c} ({d}%)"
      formatter:function(data){
        let value = data.name + '：' + data.percent.toFixed(1)+"%"
        return value;
      }
    },
    label: {
      formatter:function(data){ return data.percent.toFixed(1)+"%";}
    },
    legend: {
      orient: "vertical",
      top: "center",
      right: "26",
      data: legendData
    },
    grid: {
      left: "30%",
      right: "0%",
      bottom: "10%",
      containLabel: true
    },
    color: colorData,
    backgroundColor: '#FFFFFF',
    series: [
      {
        name: "",
        type: "pie",
        radius:  [0, '55%'],
        center: ["50%", "57.2%"],
        data: seriesData,
      }
    ]
  };
  return option;
}
export function getBarOption(xAxisData, seriesData) {
  let option = {
    title: {
      text: "人员年龄分布",
      x: "left",
      left: '20px',
      top: "20px",
    },
    color: ['rgba(91, 143, 249, 0.85)'],
    backgroundColor: '#FFFFFF',
    tooltip: {
      trigger: 'item',
      formatter: "{b} : {c} "
    },
    grid: {
      top: '84',
      bottom: '36',
      left: '20',
      right: '20',
      containLabel: true
    },
    xAxis: [
      {
        type: 'category',
        data: xAxisData,
        axisLabel: {
          interval: 0
        },
        axisTick: { // 隐藏刻度
          show: false
        },
        axisLine: { //隐藏轴线
          show: false
        },
      }
    ],
    yAxis: [
      {
        type: 'value',
        minInterval: 1,
        axisLine: { //隐藏轴线
          show: false
        },
        axisTick: { // 隐藏刻度
          show: false
        },
      }
    ],
    series: [
      {
        name: '',
        type: 'bar',
        barWidth: 18,
        data: seriesData,
      }
    ]
  };
  return option;
}
export function getRank(xAxisData, seriesData) {
  let option = {
    color: ["#1E90FF", "#32CD32"],
    title: {
      text: "排行榜",
      x: "left",
      textStyle: {
        //主标题文本样式{"fontSize": 18,"fontWeight": "bolder","color": "#333"}
        fontSize: 20,
        fontStyle: "normal",
        fontWeight: "normal",
        color: "#000"
      }
    },
    tooltip: {
      trigger: "axis",
      formatter: '{b0}<br/>{a0}: {c0}%',
      axisPointer: {
        // 坐标轴指示器，坐标轴触发有效
        type: "shadow" // 默认为直线，可选为：'line' | 'shadow'
      }
    },
    legend: {
      data: ["完成人数"],
      bottom: 0
    },
    grid: {
      left: "3%",
      right: "4%",
      bottom: "10%",
      containLabel: true
    },
    xAxis: [
      {
        type: "category",
        data: xAxisData,
      }
    ],
    yAxis: [
      {
        type: "value",
        axisLabel: {
          show: true,
          interval: 'auto',
          formatter: '{value}%'
        },
      }
    ],
    series: [
      {
        name: "完成通过率",
        type: "bar",
        stack: "完成通过率",
        barMaxWidth: "50",
        data: seriesData,
      },
      // {
      //   name: "参与人数",
      //   type: "bar",
      //   stack: "人数",
      //   barMaxWidth: "80",
      //   data: [220, 182, 191, 220, 182, 191]
      // }
    ]
  };
  return option;
}
export function getSearchType(formData, checkAll) {
  let type = 0;
  if (formData.region.length == 1 || formData.region.length == 2) {
    type = formData.region.length;
    if (formData.region.length == 1 && formData.region[0] == 0) {
      type = 6;
    }
  } else if (formData.region.length == 3) {
    if (formData.organization.length == 0 || checkAll === true) {
      type = 3;
    } else {
      type = 4;
    }
  } else if (formData.region.length == 4) {
    if (formData.organization.length == 0 || checkAll === true) {
      type = 5;
    } else {
      type = 4;
    }
  }
  return type;
}
export function hasAll(data) {
  let flag = false;
  for (let i = 0; i < data.length; i++) {
    if (data[i] == 0) {
      flag = true;
      return flag
    }
  }
  return flag;
}
export function getIds(formData, organizationList, checkAll) {
  let ids = '';
  if (formData.organization.length == 0) {
    //地区id
    ids = formData.region[formData.region.length - 1];
  } else {
    //机构id 选了'全部'
    if (checkAll === true) {
      // for (let i = 0; i < organizationList.length; i++) {
      //   if (organizationList[i].value != 0) {
      //     ids += organizationList[i].value + ',';
      //   }
      // }
      ids = formData.region[formData.region.length - 1];
    } else {
      for (let i = 0; i < formData.organization.length; i++) {
        if (formData.organization[i] != 0) {
          ids += formData.organization[i] + ',';
        }
      }
      ids = ids.substring(0, ids.length - 1);
    }
  }
  return ids;
}
//获取id的list
export function getDoctorIdList(data) {
  let list = [];
  if (typeof data === 'undefined') {
    return list;
  }
  if (data.constructor === Array) {
    for (let i = 0; i < data.length; i++) {
      list.push(data[i].doctorId);
    }
  }
  return list;
}
export function getAjustmentList(data) {
  let idList = "";
  for (let i = 0; i < data.length; i++) {
    idList += data[i] + ',';
  }
  idList = idList.substring(0, idList.length - 1);
  return idList;
}
export function removeZero(data) {
  let list = [];
  for (let i = 0; i < data.length; i++) {
    if (data[i].value != 0) {
      list.push(data[i]);
    }
  }
  return list;
}
export function initRank(data) {
  let list = [{ gradeFlag: -1, gradeName: "全部" }];
  for (let i = 0; i < data.length; i++) {
    list.push(data[i]);
  }
  return list;
}
export function getAvgTime(value) {
  let list = [0,0];
  let avgTime = '';
  if(value != null && value !='') {
      list[0] = Math.floor(value / 60);
      list[1] = value % 60;
  }
  if(list[0] == 0 && list[1] == 0) {
      avgTime = list[1] + '秒';
  } else if(list[0] == 0 && list[1] != 0) {
      avgTime = list[1] + '秒';
  } else if(list[0] != 0 && list[1] == 0) {
      avgTime = list[0] + '分钟';
  } else if(list[0] != 0 && list[1] != 0) {
      avgTime = list[0] + '分钟' + list[1] + '秒';
  }
  return avgTime;
}
export function getComponent(data) {
  let list = [];
  for (let i = 0; i < data.length; i++) {
    list.push(data[i].value);
  }
  return list;
}
export function getSimpleCheckedNodes(store) {
  const checkedNodes = [];
  const traverse = function (node) {
    const childNodes = node.root ? node.root.childNodes : node.childNodes;

    childNodes.forEach(child => {
      if (child.checked) {
        checkedNodes.push(child.data);
      }
      if (child.indeterminate) {
        traverse(child);
      }
    });
  };
  traverse(store)
  return checkedNodes;
}
export function setSelectedKeys(checkedKeys, halfCheckedKeys) {
  let selected = [];
  for (let i = 0; i < checkedKeys.length; i++) {
    let obj = {
      key: checkedKeys[i].id,
      type: 1,
    };
    selected.push(obj);
  }
  for (let j = 0; j < halfCheckedKeys.length; j++) {
    let obj = {
      key: halfCheckedKeys[j],
      type: 2,
    };
    selected.push(obj);
  }
  return selected;
}
export function setDuringTime(time , type) {
  let value = time;
  if(time == '' || time == null) {
    return '';
  }
  if(type == 'begin') {
    value = time[0];
  } else if(type == 'end') {
    value = time[1];
  }
  return value;
}
export function setHospitalIdList(data) {
  let listStr = '';
  if(data == null || data.length == 0) {
    return listStr
  }
  for(let i = 0 ; i<data.length ; i++) {
    listStr += data[i]+'_';
  }
  listStr = listStr.substring(0, listStr.length - 1);
  return listStr;
}
export function getHospitalIdList(data) {
  let listArr = [];
  if(data == null || data == '') {
    return listArr
  }
  listArr = data.split("_")
  return listArr;
}
export function learnTimeValue(value) {
  if(value === null || value === '') {
    return '-';
  }
  if(value === 0) {
    return '0秒';
  }
  let s = parseInt(value),m = 0, h = 0;//秒，分，小时
  if(s==NaN){//错误error
    return '';
  }
  if(s > 60) {
    m = parseInt(s/60);
    s = parseInt(s%60);
    if(m > 60) {
      h = parseInt(m/60);
      m = parseInt(m%60);
    }
  }
  let result = "";
  if(s > 0) {
      result = ""+parseInt(s)+"秒";
  }
  if(m > 0) {
    result = ""+parseInt(m)+"分"+result;
  }
  if(h > 0) {
    result = ""+parseInt(h)+"小时"+result;
  }
  return result;
}
export function getProjectLevelList(data) {
  let list = [];
  for(let i = 0;i < data.length ; i++) {
    let obj = {
      value: data[i].id,
      label: data[i].text,
    }
    list.push(obj);
  }
  return list;
}
export function getProjectLeaderList(data) {
  let list = [];
  for(let i = 0;i < data.length ; i++) {
    let obj = {
      value: data[i].id,
      label: data[i].name,
    }
    list.push(obj);
  }
  return list;
}
export function getCreditList(data) {
  let list = [];
  for(let i = 0;i < data.length ; i++) {
    let obj =  data[i];
    obj.id = data[i].id;
    obj.label = data[i].name;
    list.push(obj);
  }
  return list;
}
