import { ref, reactive, onMounted, onUnmounted } from "vue";

// 标签分类
import { list as contentClassList } from "@/api/content/class";

export function useAudioClass() {
  const audioClassData = reactive({
    options: [],
  });
  function getAudioClass() {
    contentClassList().then((res) => {
      if (res.code === 0) {
        audioClassData.options = res.data;
      }
    });
  }

  return { audioClassData, getAudioClass };
}

// // 企业管理-行业管理
import { options as businessClassList } from "@/api/business/class";

export function useBusinessClass() {
  const classOptions = ref([]);
  const getClassOptions = () => {
    businessClassList().then((res) => {
      if (res.code === 0) {
        classOptions.value = res.data;
      }
    });
  };

  // 挂载
  onMounted(() => getClassOptions());
  // 卸载
  onUnmounted(() => getClassOptions());

  return { classOptions };
}

// 企业筛选
import { businessList } from "@/api/business/list";

export function useBusinessSelect(type) {
  /* type为筛选条件 */
  const businessData = reactive({
    form: {
      pageNum: 1,
      pageSize: 20,
    },
    options: [],
  });

  const getBusiness = () => {
    businessList(businessData.form).then((res) => {
      if (res.code === 0) {
        businessData.options = res.data;
        if (type) {
          businessData.options = businessData.options.filter((i) => i.id !== 0);
        }
      }
    });
  };

  const businessRemote = (e) => {
    if (businessData.form.name) {
      businessData.form.name = null;
      getBusiness();
    }
    if (e) {
      businessData.form.name = e;
      getBusiness();
    }
  };

  return { businessData, getBusiness, businessRemote };
}

// 门店筛选
import { storeSelect } from "@/api/business/list";

export function useStoreSelect(type) {
  const storeData = reactive({
    form: {
      pageNum: 1,
      pageSize: 20,
    },
    options: [],
    storeId: null,
  });

  const getStore = () => {
    storeSelect(storeData.form).then((res) => {
      if (res.code === 0) {
        storeData.options = res.data;
        if (type && res.data.length > 1) {
          storeData.options.unshift({
            id: 0,
            name: "全部",
          });
        }
        storeData.storeId =
          storeData.options.length > 0 ? storeData.options[0].id : null;
      }
    });
  };

  return { storeData, getStore };
}

// 分组筛选
import { groupOptions } from "@/api/store/group";
export function useGroupSelect() {
  const groupData = reactive({
    form: {
      pageNum: 1,
      pageSize: 20,
    },
    options: [],
  });

  const getGroup = () => {
    groupOptions(groupData.form).then((res) => {
      if (res.code === 0) {
        groupData.options = res.data;
      }
    });
  };

  const groupRemote = (e) => {
    if (groupData.form.name) {
      groupData.form.name = null;
      getGroup();
    }
    if (e) {
      groupData.form.name = e;
      getGroup();
    }
  };

  return { groupData, getGroup, groupRemote };
}

// 设备列表
import { deviceList } from "@/api/business/list";

export function useDeviceList() {
  const deviceOptions = ref([]);

  const getDeviceOptions = () => {
    deviceList({}).then((res) => {
      if (res.code === 0) {
        deviceOptions.value = res.data;
      }
    });
  };

  onMounted(() => getDeviceOptions());

  onUnmounted(() => getDeviceOptions());

  return { deviceOptions };
}

// 用户列表
import { userSelect } from "../api/system/user";

export function useUserSelect() {
  const userData = reactive({
    form: {
      pageNum: 1,
      pageSize: 20,
    },
    options: [],
  });

  const getUser = () => {
    userSelect(userData.form).then((res) => {
      if (res.code === 0) {
        userData.options = res.data;
      }
    });
  };

  const userRemote = (e) => {
    if (userData.form.userName) {
      userData.form.userName = null;
      getUser();
    }
    if (e) {
      userData.form.userName = e;
      getUser();
    }
  };

  return { userData, getUser, userRemote };
}

// 省市区
import { areaOptions } from "@/api/store/list";

export function useAreaSelect() {
  const areaData = reactive({
    options: [],
  });

  const getArea = () => {
    areaOptions().then((res) => {
      if (res.code === 0) {
        areaData.options = res.data;
      }
    });
  };

  return { areaData, getArea };
}

// 音频聚合列表
import { audioList, settingList } from "../api/content/setting";

export function useAudioList() {
  const audioData = reactive({
    form: {
      pageNum: 1,
      pageSize: 10,
      type: 0,
    },
    tableData: [],
    loading: false,
    total: 0,
  });

  const getAudio = () => {
    audioData.loading = true;
    audioList(audioData.form).then((res) => {
      if (res.code === 0) {
        audioData.tableData = res.data.records;
        audioData.total = res.data.total;
        audioData.loading = false;
      }
    });
  };

  return { audioData, getAudio };
}

// 内容设定列表
export function useSettingList() {
  const settingData = reactive({
    form: {
      pageNum: 1,
      pageSize: 10,
    },
    tableData: [],
    loading: false,
    total: 0,
  });

  const getSetting = () => {
    settingData.loading = true;
    settingList(settingData.form).then((res) => {
      if (res.code === 0) {
        settingData.tableData = res.data.records;
        settingData.total = res.data.total;
        settingData.loading = false;
      }
    });
  };

  return { settingData, getSetting };
}
