import { useEffect, useState } from '@tarojs/taro';
import { fetchLastNotice, PublishClient, AppHomeNoticeAppVo } from '@/api/home';
import { useSelector } from '@tarojs/redux';
import { Customer } from '@/types/Customer/Customer';

const initState: AppHomeNoticeAppVo = {
  content: '', // 内容
  createAt: '', // 创建时间
  id: 0, // 主键ID
  noticeCode: '', // 公告编码
  noticeImgs: '', // 公告图片
  noticeTitle: '', // 公告名称
  noticeType: '', // 强弹类型 1是 2否
  operateId: 0, // 运营商ID
  operator: '', // 操作人
  publishClient: PublishClient.wx, // 发布端
  sortId: 0, // 排序号
  state: 1, // 状态 1已发布 0未发布
  updateAt: '', // 更新时间
  whetherClicked: false, // 是否点击false为否true为是
};
const useNotices = (): AppHomeNoticeAppVo => {
  const [state, setState] = useState(initState);
  const userinfo = useSelector(
    (state: { userinfo: Customer }) => state.userinfo,
  );
  useEffect(() => {
    fetchLastNotice({
      customerId: userinfo.customerId,
      publishClient: PublishClient.wx,
    })
      .then(res => {
        console.log(res);
        setState(res.data);
      })
      .catch(err => {
        console.log(err);
      });
  }, []);
  return state;
};

export default useNotices;
