import Vue from "vue";
import * as constant from "../constant";
import numeral from "numeral";
import dayjs from "dayjs";

// 乐团状态
Vue.filter("bandStatus", (value) => {
  let templateStatus = {
    APPLY: "报名中",
    PAY: "缴费中",
    PREPARE: "筹备中",
    UNDERWAY: "进行中",
    CANCELED: "取消",
  };
  return templateStatus[value];
});

// 商品类型
Vue.filter("shopType", (value) => {
  let template = {
    INSTRUMENT: "乐器",
    ACCESSORIES: "辅件",
    TEACHING: "教材",
    STAFF: "教谱",
    OTHER: "其它",
  };
  return template[value];
});

// 金额格式化
Vue.filter("moneyFormat", (value) => {
  return numeral(value).format("0,0.00");
});

// 金额格式化，不要多余的0
Vue.filter("moneyFormat2", (value) => {
  const str = numeral(value).format("0,0.00");
  const nums = str.split(".");
  const result = [nums[0]];
  const subfix = parseFloat("." + nums[1]);
  if (subfix > 0) {
    result.push(("" + subfix).replace("0.", ""));
  }
  return result.join(".");
});

Vue.filter("numberFormat", (value) => {
  return numeral(value).format("0,0");
});

// 课程类型
Vue.filter("coursesType", (val) => constant.courseType[val]);

// 合并数组
Vue.filter("joinArray", (value, type) => {
  if (!type) {
    type = " ";
  }
  if (typeof value == "object" && value != null) {
    return value.join(type);
  } else {
    return value;
  }
});

// 数据类型
Vue.filter("dataStatusCN", (value) => {
  let templateStatus = {
    到课: "",
    请假: "truant",
    旷课: "leave",
  };
  return templateStatus[value];
});

// 考勤类型
Vue.filter("clockingIn", (value) => {
  let templateStatus = {
    NORMAL: "正常",
    TRUANT: "旷课",
    LEAVE: "请假",
    QUIT_SCHOOL: "休学",
    DROP_OUT: "退学",
    LATE: "迟到",
  };
  return templateStatus[value];
});

// 课程类型
Vue.filter("teachModeStatus", (value) => {
  let templateStatus = {
    ONLINE: "线上课",
    OFFLINE: "线下课",
  };
  return templateStatus[value];
});

// 消耗类型
Vue.filter("periodRecordStatus", (value) => {
  let templateStatus = {
    NOT_START: "未开始",
    APPLYING: "报名中",
    NORMAL: "正常",
    LOCK: "锁定",
    FINISH: "结束",
    CANCEL: "取消",
  };
  return templateStatus[value];
});

// 计算分钟数
Vue.filter("calcMinute", (minute) => {
  if (minute <= 0) {
    return "0分钟";
  }
  let minutes = minute % 60; // 算出分钟
  let hours = 0; // 小时
  if (minute >= 60) {
    hours = (minute - minutes) / 60;
  }
  let text = "";
  if (hours) {
    text = hours + "小时";
  }
  if (minutes) {
    text += minutes + "分钟";
  }
  return text;
});

Vue.filter("formatDate", (value) => {
  let d = new Date(value.replace(/-/gi, "/"));
  let hour = d.getHours() >= 10 ? d.getHours() : "0" + d.getHours();
  let minute = d.getMinutes() >= 10 ? d.getMinutes() : "0" + d.getMinutes();
  return hour + ":" + minute;
});

Vue.filter("formatDateExtend", (value, format = "YYYY-MM-DD") => {
  return dayjs(value).format(format);
});

Vue.filter("formatTimer", (value) => {
  if (value) {
    return value.split(" ")[0];
  } else {
    return value;
  }
});

// 格式化单位
Vue.filter("formatUnit", (value) => {
  const template = {
    1: "元",
    2: "%",
  };
  return template[value];
});

Vue.filter("coupontypeDetailType", (val) => constant.coupontypeDetail[val]);

// 乐团学员状态
Vue.filter("musicGroupStudentType", (value) => {
  let template = {
    NORMAL: "在读",
    LEAVE: "请假",
    QUIT: "退团",
    APPLY: "报名",
  };
  return template[value];
});

Vue.filter("coursesType", (val) => constant.courseType[val]);
