import cityList from '@/utils/city.json';
const custom = {
  getStdSubject: (data) => { // 获取标准类型格式
    let arr= []
    for(let item of data) {
      // item.label = item.name
      if(item.children){
        arr.push(...custom.getStdSubject(item.children))
      }
      else{
        arr.push(item)
      }
    }
    return arr
  },
  get_course_category_name(data,subject_id){ // 根据id 获取名称
    let name = ''
    if(!data || !subject_id) {
      return ''
    }
    for(let i = 0;i<data.length;i++) {
      if(name){
        return name
      }
      else{
        if(data[i].id == subject_id ) {
          name = data[i].name
          break
        }
        else if(data[i].children){
          name = custom.get_course_category_name(data[i].children,subject_id)
        }
      }
    }
    return name
  },
  getExercisesTypeList() {
    return [
      {
        label: '单选题',
        value2:'single',
        value:1
      },
      {
        label: '多选题',
        value2:'mutil',
        value:2
      },
      {
        label: '判断题',
        value2:'judge',
        value:3
      },
      {
        label: '填空题',
        value2:'application',
        value:4
      },
      {
        label: '选填题',
        value2:'application',
        value:5
      }
    ]
  },
  getExercisesTypeName(type) {
    let name = ''
    switch (type){
      case 1:
        name = '单选题';
        break;
      case 2:
        name = '多选题';
        break;
      case 3:
        name = '判断题';
        break;
      case 4:
        name = '填空题';
        break;
      case 5:
        name = '选填题';
        break;
    }
    return name
  },
  getProvinceName(province) {
    if(province != 0) {
      if(province < 0 || !province) return ''
    }
    return cityList[province].province
  },
  getExamGroupType() {
    return [
      {
        name:'1',
        value:1
      },
      {
        name:'10',
        value:10
      },{
        name:'100',
        value:100
      },{
        name:'1000',
        value:1000
      },
      {
        name:'1万',
        value:10000
      },
      {
        name:'10万',
        value:100000
      },
      {
        name:'100万',
        value:1000000
      },
      {
        name:'1000万',
        value:10000000
      },
    ]
  },
  timestampToDate(stamp) { // 时间戳转日期
      var time = new Date(stamp);
      var y = time.getFullYear();
      var m = time.getMonth()+1;
      var d = time.getDate();
      const date = `${y}-${m}-${d}`
      // console.log(date)
    return date
  },
  timestampToDate2(stamp) { // 时间戳转日期 精确到秒
      var time = new Date(stamp);
      var y = time.getFullYear();
      var m = time.getMonth()+1;
      var d = time.getDate();
      var h = time.getHours();
      var min = time.getMinutes();
      var s = time.getSeconds();
      const date = `${y}-${m}-${d} ${h}:${min}:${s}`
      // console.log(date)
    return date
  },
  getSTdPerson(num) {
    const flag = 10000
    if(num<flag) {
      return num
    }
    else{
      return num/flag+'万'
    }
  },addTreeFormatSubject(data,type) {
    if(!data) {
      return []
    }
    data.map( item => {
        item.label = item.name
        item.value = item[type]
        if(item.level !== 3) {
          item.disabled = true
        }
        if(item.children) {
          custom.addTreeFormatSubject(item.children,type)
        }
    })
    return data
  },
  getUserPosition(user_type) { // 获取用户身份
    const position_arr = ['普通用户','助教','老师', '主管']; //普通用户，助教，老师，主管
    const val_arr = []
    for(let i = 0;i< position_arr.length;i++) {
      if((user_type & 0b1) === 0b1 && i === 0) {
        val_arr.push(0)
      }
      if((user_type & 0b10) === 0b10  && i === 1){
        val_arr.push(1)
      }
      if((user_type & 0b100) === 0b100  && i === 2){
        val_arr.push(2)
      }
      if((user_type & 0b1000) === 0b1000 && i === 3){
        val_arr.push(3)
      }
    }
    let str = ''
    for(let i= 0;i<val_arr.length;i++) {
      if(!str) {
        str = position_arr[val_arr[i]]
      }
      else{
        str =str+'&'+position_arr[val_arr[i]]
      }
    }
    return str
  },dateFmt(date, fmt = 'yyyy-mm-dd HH:MM') {
		if (!date) return ''
		// if (typeof(date) == 'string') {
		// 	date = date.replace(/-/g, '/');
		// 	// date = date.replace(/\./g, '/');
		// }

		date = new Date(date);

		let ret;
		let opt = {
			"y+": date.getFullYear().toString(), // 年
			"m+": (date.getMonth() + 1).toString(), // 月
			"d+": date.getDate().toString(), // 日
			"H+": date.getHours().toString(), // 时
			"M+": date.getMinutes().toString(), // 分
			"S+": date.getSeconds().toString() // 秒
			// 有其他格式化字符需求可以继续添加，必须转化成字符串
		};
		for (let k in opt) {
			ret = new RegExp("(" + k + ")").exec(fmt);
			if (ret) {
				fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
			};
		};
		return fmt;
	},
  getCourseStateName(state){ // 获取课程状态名称
    let name = ''
    switch(state) {
      case -1:
        name = '未上架';
        break;
      case 1:
        name = '已上架';
        break;
      case 2:
        name = '待审核';
        break;
      case 3:
        name = '审核通过';
        break;
      case 4:
        name = '审核拒绝';
        break;
    }
    return name
  },
  getStdPrice(price) { // 获取标准格式价格
		return Number(price)/100;
	},
  getDealType() { // 获取交易类型
    return [
      {
        name:'课程',
        value:1
      },
      {
        name:'试卷',
        value:4
      },{
        name:'邀请',
        value:7
      },{
        name:'提现',
        value:8
      },{
        name:'助教',
        value:9
      },{
        name:'助教下学员购买试卷',
        value:10
      },{
        name:'助教下学员购买课程',
        value:11
      },{
        name:'助教下老师卖出试卷',
        value:12
      },{
        name:'助教下老师卖出课程',
        value:13
      },{
        name:'域内 课程',
        value:15
      },{
        name:'域内 试卷',
        value:16
      }
    ]
  }
}
export default custom
