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

export class VillageListItem extends DataModel<VillageListItem> {
  constructor() {
    super(VillageListItem, "村落列表");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
      images: { clientSide: 'forceArray' }
    }
    this._nameMapperServer = {
      name: 'villageName',
    };
    this._convertKeyType = (key, direction) => {
      if (key.endsWith('At'))
        return {
          clientSide: 'date',
          serverSide: 'string',
        };
      return undefined;
    };
    this._afterSolveServer = () => {
      if (this.images && this.images && this.images.length > 0  ) {
        this.image = this.images[0]
      }
    }
  }

  id !: number;
  villageVolunteerId = null as number|null;
  villageId = null as number|null;
  claimReason = '';
  status = '';
  statusText = '';
  createdAt = null as Date|null;
  updatedAt = null as Date|null;
  deleteAt = null as Date|null;
  image = '';
  images = [] as string[];
  villageName = '';
  volunteerName = '';
}

export class VillageMenuListItem extends DataModel<VillageMenuListItem> {
  constructor() {
    super(VillageMenuListItem, "村落菜单列表");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
    }
    this._nameMapperServer = {
    };
    this._convertKeyType = (key, direction) => {
      if (key.endsWith('At'))
        return {
          clientSide: 'date',
          serverSide: 'string',
        };
      return undefined;
    };

  }

  id = 0;
  name = '';
  logo = '';
  region = 0;
  modelId = 0;
  mainBodyColumnId = 0;
}

export class VillageApi extends AppServerRequestModule<DataModel> {

  constructor() {
    super();
  }

  async getVillageList(level?: number) {
    return (this.get('/village/village/getList', '村落列表', {
      history_level: level,
    })) 
      .then(res => transformArrayDataModel<VillageListItem>(VillageListItem, res.data2, `村落`, true))
      .catch(e => { throw e });
  }
  async getVillageMenuList(id: number) {
    return (this.get('/village/menu/getList', '村落菜单列表', {
      platform: 1,
      village_id: id,
    })) 
      .then(res => transformArrayDataModel<VillageMenuListItem>(VillageMenuListItem, res.data2, `村落菜单`, true))
      .catch(e => { throw e });
  }

  
}

export default new VillageApi();
