import { clockState, clockJournalState, industry, channelTypeList, addressList } from './config';
import dayjs from "dayjs";
import Taro from "@tarojs/taro";

export const getClockState = (id, journal = false) => {
  let arr = journal ? clockJournalState.filter(v => v.id === id) : clockState.filter(v => v.id === id);
  if (arr.length > 0) {
    return arr[0];
  } else {
    return {};
  }
}

//获取用户工作时间区间
export const getUserWordTimes = () => {
  let userInfor = Taro.getStorageSync('userInfor');
  return userInfor.workTimeInfor;
}

//获取当天公出小时数
export const getHours = (startTime, endTime) => {
  const { restStart, restEnd } = getUserWordTimes();
  let a1 = dayjs(startTime);
  let b1 = dayjs(endTime);
  let c1 = b1.diff(a1, 'minutes');
  let hourse = parseInt(c1 / 60);
  let minutes = c1 % 60
  let minutes1 = parseInt(minutes / 30) * 0.5;

  //开始时间在12:00之前 包括12:00
  if (!a1.isAfter(dayjs(a1.format('YYYY-MM-DD') + restStart))) {
    if (b1.isAfter(dayjs(b1.format('YYYY-MM-DD') + restEnd))) {
      return hourse + minutes1 - 1.5
    } else if (!b1.isAfter(dayjs(b1.format('YYYY-MM-DD') + restStart))) {
      return hourse + minutes1
    } else {
      let a2 = dayjs(startTime);
      let b2 = dayjs(b1.format('YYYY-MM-DD') + restStart);
      let c2 = b2.diff(a2, 'minutes');
      let hourse2 = parseInt(c2 / 60);
      let minutes2 = c2 % 60
      let minutes3 = parseInt(minutes2 / 30) * 0.5;
      return hourse2 + minutes3
    }
    //  开始时间在12:00之后13:30之前
  } else if (a1.isBefore(dayjs(a1.format('YYYY-MM-DD') + restEnd)) && a1.isAfter(dayjs(a1.format('YYYY-MM-DD') + restStart))) {
    if (b1.isAfter(dayjs(b1.format('YYYY-MM-DD') + restEnd))) {
      let a2 = dayjs(a1.format('YYYY-MM-DD') + restEnd);
      let b2 = dayjs(endTime);
      let c2 = b2.diff(a2, 'minutes');
      let hourse2 = parseInt(c2 / 60);
      let minutes2 = c2 % 60
      let minutes3 = parseInt(minutes2 / 30) * 0.5;
      return hourse2 + minutes3
    } else {
      return 0;
    }
    //开始时间在13:30之后 包括13:30
  } else if (!a1.isBefore(dayjs(a1.format('YYYY-MM-DD') + restEnd))) {
    return hourse + minutes1
  }
}

//获取总时长
export const getNumHourse = (startTimeValue, endTimeValue, days) => {
  const { start, end } = getUserWordTimes();

  //开始时间 向下取整   结束时间向上取整
  let startTimeArr = [parseInt(dayjs(startTimeValue).format('HH')), parseInt(dayjs(startTimeValue).format('mm'))];
  let endTimeArr = [parseInt(dayjs(endTimeValue).format('HH')), parseInt(dayjs(endTimeValue).format('mm'))];

  if (startTimeArr[1] > 0 && startTimeArr[1] < 30) {
    startTimeArr[1] = 30;
  } else if (startTimeArr[1] > 30) {
    startTimeArr[0] = startTimeArr[0] + 1;
    startTimeArr[1] = 0;
  }

  if (endTimeArr[1] > 0 && endTimeArr[1] < 30) {
    endTimeArr[1] = 0;
  } else if (startTimeArr[1] > 30) {
    endTimeArr[1] = 30;
  }

  let startTime = dayjs(startTimeValue).format('YYYY-MM-DD') + ' ' + startTimeArr.join(':')
  let endTime = dayjs(endTimeValue).format('YYYY-MM-DD') + ' ' + endTimeArr.join(':')

  if (dayjs(dayjs(startTime).format('YYYY-MM-DD')).isSame(dayjs(dayjs(endTime).format('YYYY-MM-DD')))) {
    let num = getHours(startTime, endTime)
    if (num < 0) {
      return 0
    } else {
      return getHours(startTime, endTime)
    }
  } else {
    let a = getHours(startTime, dayjs(dayjs(startTime).format('YYYY-MM-DD') + end));
    let b = getHours(dayjs(dayjs(endTime).format('YYYY-MM-DD') + start), endTime);

    let totalDuration = getHours(dayjs(startTime).format('YYYY-MM-DD') + start, dayjs(dayjs(startTime).format('YYYY-MM-DD') + end));   //一天工作时长

    let c = (days - 2) * totalDuration;
    return a + b + c;
  }
}

function addZero(num) {
  return Number(num) < 10 ? `0${num}` : num;
}

export const formatDate = (year, month, day, hour, minute) => {
  const newmonth = addZero(month);
  const newday = addZero(day);
  const newhour = addZero(hour);
  const newminute = addZero(minute);

  return year + '-' + newmonth + '-' + newday + ' ' + newhour + ":" + newminute;
};

export const deepClone1 = (obj) => {
  //判断拷贝的要进行深拷贝的是数组还是对象，是数组的话进行数组拷贝，对象的话进行对象拷贝
  var objClone = Array.isArray(obj) ? [] : {};
  //进行深拷贝的不能为空，并且是对象或者是
  if (obj && typeof obj === "object") {
    for (key in obj) {
      if (obj.hasOwnProperty(key)) {
        if (obj[key] && typeof obj[key] === "object") {
          objClone[key] = deepClone1(obj[key]);
        } else {
          objClone[key] = obj[key];
        }
      }
    }
  }
  return objClone;
}

// 是否是登录人的id
export const isOneself = (id) => {
  let userInfor = Taro.getStorageSync('userInfor');
  if (userInfor.aid === id) {
    return true
  } else {
    return false
  }
}

// 数组合并处理
export const getNewList = (uids, unames) => {
  // if(!uids){
  //   return []
  // }
  if (uids.indexOf(",") > -1) {
    let idlist = uids.split(',')
    let namelist = unames.split(',')
    let newlist = []
    for (var i = 0; i < idlist.length; i++) {
      newlist.push({ id: idlist[i], name: namelist[i] })
    }
    return newlist
  } else {
    return [{ id: uids, name: unames }]
  }
}

// 数组重组
export const getNewOptions = (arr) => {
  let list = []
  for (var i = 0; i < arr.length; i++) {
    list.push({
      label: arr[i].userName,
      value: arr[i].uid,
      desc: arr[i].prdcList.length == 0
        ? "(不在打卡时间范围)"
        : (arr[i].prdcList[0].status == 1 ? "(今日已打卡)" : "(今日未打卡)")
    })
  }
  return list
}

// 提取数组元素
export const getUids = (array, key) => {
  let arr = []
  if (array && array.length > 0) {
    array.forEach(e => {
      arr.push(e[key])
    });
  }
  return arr
}

// 提取数组里的某一项组成新数组(公出他人企业限制)
export const getList = (type, arr, name, isUnique = false) => {
  let list = []
  for (var i = 0; i < arr.length; i++) {
    if (type == 1) {
      list.push(arr[i][name])
    } else {
      if (arr[i].status == 1) {
        list.push(arr[i][name])
      }
    }
  }
  // 去重
  if (isUnique) {
    unique(list)
  }
  return list
}

// 显示提示(公出他人企业)
export const isTips = (arr) => {
  let show = false
  for (var i = 0; i < arr.length; i++) {
    if (arr[i].status == 1) {
      show = true
    }
  }
  return show
}

export const isPublicity = (array, type) => {
  if (array && array.length > 0) {
    if (type == 0) {
      let show = false
      for (var i = 0; i < array.length; i++) {
        if (array[i].type == 2) {
          show = true
        }
      }
      return show
    }
    if (type == 1) {
      let list = []
      for (var i = 0; i < array.length; i++) {
        let name = array[i].adminName;
        let t = array[i].type;
        if (!!name && t == 2) {
          list.push(name)
        }
      }
      unique(list)
      return list
    }
  }
}

// 数组去重
export const unique = (arr, key) => {
  if (arr.length <= 1) {
    return arr
  }
  for (var i = 0; i < arr.length; i++) {
    for (var j = i + 1; j < arr.length; j++) {
      if (key) {
        if (arr[i][key] == arr[j][key]) {         //第一个等同于第二个，splice方法删除第二个
          arr.splice(j, 1);
          j--;
        }
      } else {
        if (arr[i] == arr[j]) {
          arr.splice(j, 1);
          j--;
        }
      }
    }
  }
  return arr;
}

// 
export const getNameList = (aidList, arr) => {
  let list = []
  for (var i = 0; i < aidList.length; i++) {
    if (aidList[i] == arr[i].id) {
      list.push(arr[i].name)
    }
  }
  return list
}

export const getContactType = (val) => {
  let theType = "";
  let list = [
    { key: "公出", value: 0 },
    { key: "电话", value: 1 },
    { key: "QQ", value: 2 },
    { key: "微信", value: 3 },
    { key: "邮件", value: 4 },
    { key: "公出打卡", value: 5 },
    { key: "来访面谈", value: 6 },
  ]
  list.map(function (item) {
    if (item.value == val) {
      theType = item.key
    }
  })
  return theType;
}

// 行业
export const getIndustryType = (val) => {
  let theType = "";
  industry.map(function (item) {
    if (item.id == val) {
      theType = item.title
    }
  })
  return theType;
}

// 渠道类别
export const getChannel = (val) => {
  let theType = "";
  channelTypeList.map(function (item) {
    if (item.id == val) {
      theType = item.title
    }
  })
  return theType;
}

export const getProvince = (p, c, a) => {
  let province = !!p && p.slice(0, 2)
  let city = c
  let area = !!a && a.slice(0, 2)
  let pid = "";
  let cid = ""
  let aid = ""
  if (!!province) {
    addressList.map(function (i) {
      if (i.name.slice(0, 2) == province) {
        pid = i.id
        if (!!city && i.cityList.length > 0) {
          i.cityList.map(function (j) {
            if (j.name == city) {
              cid = j.id
              if (!!area && j.areaList.length > 0) {
                j.areaList.map(function (k) {
                  if (k.name.slice(0, 2) == area) {
                    aid = k.id
                  }
                })
              }
            }
          })
        }
      }
    })
  }
  return [pid, cid || pid, aid || cid || pid]
}