import { DataModel, transformArrayDataModel } from '@imengyu/js-request-transform';
import { AppServerRequestModule } from '../RequestModules';

export class ActivityListItem extends DataModel<ActivityListItem> {
  constructor() {
    super(ActivityListItem, "活动列表");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
    }
    this._convertKeyType = (key, direction) => {
      if (key.endsWith('Time') || key === 'deadline')
        return {
          clientSide: 'date',
          serverSide: 'string',
        };
      return undefined;
    };

  }

  id = 0;
  type = 0;
  title = '';
  organizers = null;
  image = '';
  desc = '';
  name = '';
  startTime : Date|null = null;
  endTime : Date|null = null;
  deadline : Date|null = null;
  rewardTime : Date|null = null;
  price = '';
  status = '';
  flagText = '';
  typetext = '';
  activityMethodsText = '';
  statusText = '';
}
export class ActivityListDetail extends DataModel<ActivityListDetail> {
  constructor() {
    super(ActivityListDetail, "活动详情");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
    }
    this._convertKeyType = (key, direction) => {
      if (key.endsWith('Time') || key === 'deadline' || key === 'createdAt' || key === 'updatedAt')
        return {
          clientSide: 'date',
          serverSide: 'string',
        };
      return undefined;
    };
  }

  id = null as number|null;
  mainBodyId = null as number|null;
  type = '';
  modelId = null as number|null;
  contentIds = '';
  adminId = null as number|null;
  title = '';
  tagIds = '';
  organizers = '';
  flag = '';
  image = '';
  images = [] as string[];
  desc = '';
  content = '';
  startTime = null as Date|null;
  endTime = null as Date|null;
  deadline = null as Date|null;
  rewardTime = null as Date|null;
  limitNum = null as number|null;
  applicantsNum = null as number|null;
  price = '';
  score = null as number|null;
  activityMethods = '';
  status = '';
  weight = null as number|null;
  createdAt = null as Date|null;
  updatedAt = null as Date|null;
  route = '';
  flagText = '';
  typeText = '';
  activityMethodsText = '';
  statusText = '';
}
export class GuideListItem extends DataModel<GuideListItem> {
  constructor() {
    super(GuideListItem, "宣讲员列表");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      id: { clientSide: 'string', serverSide: 'string', clientSideRequired: true },
    }
    this._convertKeyType = (key, direction) => {
      if (key.endsWith('Time') || key === 'deadline')
        return {
          clientSide: 'date',
          serverSide: 'string',
        };
      return undefined;
    };
  }

  id = '';
  mainBodyUserId = null as number|null;
  name = '';
  image = '';
  sex = null as number|null;
  age = null as number|null;
  mobile = '';
  regionId = null as number|null;
  address = '';
  unitName = '';
  intro = '';
  status = '';
  crTitle = '';
  statusText = '';
  sexText = '';
  regionIdText = '';
}
export class GuideListDetail extends DataModel<GuideListDetail> {
  constructor() {
    super(GuideListDetail, "宣讲员详情");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
      createdAt: { clientSide: 'date', serverSide: 'string' },
    }
  }

  id = '';
  mainBodyUserId = null as number|null;
  name = '';
  identityType = '';
  createdAt = null as Date|null;
  sex = null as number|null;
  age = null as number|null;
  mobile = '';
  regionId = null as number|null;
  intro = '';
  statusText = '';
  sexText = '';
  regionIdText = '';
}
export class SignupParamsItem extends DataModel<SignupParamsItem> {
  constructor() {
    super(SignupParamsItem, "宣讲员详情");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
      params: { clientSide: 'object', serverSide: 'string' },
      setting: { clientSide: 'object', serverSide: 'string' },
    }
  }

  id = null as number|null;
  pid = null as number|null;
  params = null as { [key: string]: any }|null;
  setting = null as { [key: string]: any }|null;
  name = '';
  title = '';
  type = '';
  rule = '';
  default = null as number|null;
  status = '';
  weight = null as number|null;
}

export class RunningApi extends AppServerRequestModule<DataModel> {

  constructor() {
    super();
  }

  async getActivityList(searchText?: string) {
    const res = await this.get('/activity/activity/lists', '获取活动列表', {
      keywords: searchText,
      type: 3,
    });
    return transformArrayDataModel<ActivityListItem>(ActivityListItem, res.data as any, `活动列表`, true);
  }
  async getActivityDetail(id: number) {
    return (await this.get('/activity/activity/details', '获取活动详情', {
      id,
    }, ActivityListDetail)).data as ActivityListDetail;
  }
 
  async getGuideList(searchText?: string) {
    const res = await this.get('/volunteer/volunteer/getXuanjiangList', '获取宣讲员列表', {
      keywords: searchText,
      type: 3,
    });
    return transformArrayDataModel<GuideListItem>(GuideListItem, res.data as any, `宣讲员列表`, true);
  }

  async getScheduleList(activity_id: number) {
    const res = await this.get('/activity/schedule/lists', '获取活动日程列表', {
      activity_id,
    });
    return transformArrayDataModel<ActivityListDetail>(ActivityListDetail, res.data as any, `活动日程列表`, true);
  }

  async getSignupParamsList(activity_id: number) {
    const res = await this.get('/activity/signup_params/lists', '获取活动报名参数列表', {
      activity_id,
    });
    return transformArrayDataModel<SignupParamsItem>(SignupParamsItem, res.data as any, `活动报名参数列表`, true);
  }

  async getGuideDetail(id: string) {
    return (await this.get('/volunteer/volunteer/getXuanjiangDetails', '获取宣讲员详情', {
      volunteer_id: id,
    }, GuideListDetail)).data as GuideListDetail;
  }

  async submitActivitySignupInfo(data: {
    activity_id: number,
    name: string,
    mobile: string,
    id_card: string,
  }) {
    await this.post('/volunteer/volunteer/getXuanjiangDetails', '活动报名', data);
  }
}

export default new RunningApi();
