// info  nam="SAMPLE07C"  tit="巟暐挷彂"  dsc="巟暐挷彂報嶞"  pro=3

void GridText(HREP rp, PCGRID gr, int ir, int ic, PCSTR st, int fmt)
{ // 暥帤楍偺昤夋
  CELL cel; CellText(rp, GridCell(&cel, gr, ir, ic), st, fmt);
}

void PaddText(HREP rp, PCGRID gr, int ir, int ic, REAL lm, REAL tm, REAL rm, REAL bm, PCSTR st, int fmt)
{ // 暥帤楍偺昤夋
  CELL cel; CellText(rp, ReshapeCell(GridCell(&cel, gr, ir, ic), lm, tm, rm, bm), st, fmt);
}

PCSTR PINdig(REAL d, int n)
{ // n寘栚悢帤
  int m;
  char bf[32];
  static char dg[2];

  sprintf(bf, "%012.0f", d);
  m = strlen(bf);
  dg[0] = (n >= 0 && n <= 12 && n >= 13 - m)? bf[n - (13 - m)]: '\0';
  return dg;
}

  // 僨乕僞擖椡昞
typedef struct {
  WORD  ser;
  BYTE  suf;
  BYTE  atr;
  char  zip[12];
  char  adr[64];
  char  nam[64];
  char  kan[64];
  char  tel[16];
  REAL  pin;
  char  acc[32];
  char  det[48];
  int   fee;
  int   unp;
  int   tax;
  int   unc;
  char  rem[64];
  char  mem[64];
  int   reg;
  char  usr[32];
  BYTE  rsv[20];
} HOUD;
typedef HOUD * PHOUD;
typedef const HOUD * PCHOUD;

void PrintSlip(HREP rp, PCHOUD ph, int nk, BOOL sw)
{ // 挷彂報嶞
  int i, j, k, rst;
  CELL cel;
  GRID grd;
  static REAL cw0[3] = { 10.0, 14.0, 102.0 };
  static REAL cw1[4] = { 26.0, 35.0, 32.5, 32.5 };
  static REAL cw3[3] = { 33.0, 46.5, 46.5 };

  SetCell(&cel, 12.0, 4.5, 138.0, 11.0);
  SetTextFont(rp, "俵俽 柧挬", 3.7, 0, 0);
  CellText(rp, &cel, "椷榓俈擭暘丂曬廣丄椏嬥丄宊栺嬥媦傃徿嬥偺巟暐挷彂", DT_CEN);

  SetGrid1(&grd, 12.0, 11.0, NELEM(cw0), 2, cw0, 7.7);
  for (i = 0; i <  grd.nr; i++) {
    for (j = 0; j < grd.nc; j++) {
      rst = RST((j == 0)? lstB: 0, (i == 0)? lstB: 0, (j == grd.nc-1)? lstB: lstR, (i == grd.nr-1)? lstB: (i == 0 && j == 0)? 0: lstR);
      DrawGridCell(rp, &grd, i, j, rst);
    }
  }
  SetTextFont(rp, NULL, 2.4, 0, 0);
  PaddText(rp, &grd, 0, 0, 0.0, 5.0, 0.0, 0.5, "巟 暐 傪", DT_CEN);
  PaddText(rp, &grd, 1, 0, 0.0, 0.5, 0.0, 5.0, "庴偗傞幰", DT_CEN);
  SetTextFont(rp, NULL, 2.4, 0, 0);
  PaddText(rp, &grd, 0, 1, 0.0, 1.3, 0.0, 4.2, "廧強(嫃強)", DT_CEN);
  PaddText(rp, &grd, 0, 1, 0.0, 4.2, 0.0, 1.3, "枖偼強嵼抧", DT_CEN);
  PaddText(rp, &grd, 1, 1, 0.0, 1.3, 0.0, 4.2, "巵 柤 枖偼", DT_CEN);
  PaddText(rp, &grd, 1, 1, 0.0, 4.2, 0.0, 1.3, "柤丂丂丂徧", DT_CEN);
  SetTextFont(rp, NULL, 3.7, 0, 0);
  PaddText(rp, &grd, 0, 2, 2.0, 0.0, 2.0, 0.0, ph->adr, DT_AUT);
  PaddText(rp, &grd, 1, 2, 2.0, 0.0,47.0, 0.0, ph->nam, DT_AUT);
  SetLineStyle(rp, lstR);
  DrawLine(rp, 93.0, 18.7, 93.0, 26.4);
  DrawLine(rp, 93.0, 21.7, 138.0, 21.7);
  SetLineStyle(rp, lstD);
  for (j = 1; j < 13; j++) {
    DrawLine(rp, 93.0 + j * 45.0/13, 21.7, 93.0 + j * 45.0/13, 26.4);
  }
  SetCell(&cel, 93.0, 18.7, 138.0, 21.7);
  SetTextFont(rp, NULL, 2.4, 0, 0);
  CellText(rp, &cel, "屄恖斣崋枖偼朄恖斣崋", DT_CEN);
  if (sw) {
    SetTextStyle(rp, 2.6, 2.6, 0.0, NULL);
    for (j = 0; j < 13; j++) {
      SetCell(&cel, 93.0 + j * 45.0/13, 21.7, 93.0 + (j + 1) * 45.0/13, 26.4);
      CellText(rp, &cel, PINdig(ph->pin, j), DT_CEN);
    }
  }

  SetGrid1(&grd, 12.0, 26.4, NELEM(cw1), 7, cw1, 5.7);
  for (i = 0; i < grd.nr; i++) {
    for (j = 0; j < grd.nc; j++) {
      rst = RST((j == 0)? lstB: 0, 0, (j == grd.nc-1)? lstB: lstR, (i == grd.nr-1)? lstB: lstR);
      DrawGridCell(rp, &grd, i, j, rst);
    }
  }
  SetLineStyle(rp, lstD);
  DrawLine(rp, 84.0, 32.1, 84.0, 66.3);
  DrawLine(rp, 94.5, 32.1, 94.5, 66.3);
  DrawLine(rp,116.5, 32.1,116.5, 66.3);
  DrawLine(rp,127.0, 32.1,127.0, 66.3);
  SetTextFont(rp, NULL, 2.6, 0, 0);
  GridText(rp, &grd, 0, 0, "嬫丂丂丂暘", DT_CEN);
  GridText(rp, &grd, 0, 1, "嵶丂丂丂丂栚", DT_CEN);
  GridText(rp, &grd, 0, 2, "巟丂暐丂嬥丂妟", DT_CEN);
  GridText(rp, &grd, 0, 3, "尮 愹 挜 廂 惻 妟", DT_CEN);
  for (k = 0; k < nk; k++) {
    SetTextFont(rp, NULL, 2.6, 0, 0);
    PaddText(rp, &grd, 1 + k, 0, 1.0, 0.0, 1.0, 0.0, (ph + k)->acc, DT_AUT);
    PaddText(rp, &grd, 1 + k, 1, 1.0, 0.0, 1.0, 0.0, (ph + k)->det, DT_AUT);
    if ((ph + k)->unp == 0) {
      SetTextStyle(rp, 2.6, 2.6, 0.0, NULL);
      PaddText(rp, &grd, 1 + k, 2, 1.0, 0.0, 2.0, 0.0, cformat("%d", (ph + k)->fee), DT_RGT);
    } else {
      SetTextFont(rp, NULL, 2.2, 0, 0);
      PaddText(rp, &grd, 1 + k, 2, 1.0, 0.0, 2.0, 2.8, "撪", DT_LFT);
      SetTextStyle(rp, 2.6, 2.6, 0.0, NULL);
      PaddText(rp, &grd, 1 + k, 2, 1.0, 0.0, 2.0, 2.8, cformat("%d", (ph + k)->unp), DT_RGT);
      PaddText(rp, &grd, 1 + k, 2, 1.0, 2.8, 2.0, 0.0, cformat("%d", (ph + k)->fee), DT_RGT);
    }
    if ((ph + k)->unc == 0) {
      SetTextStyle(rp, 2.6, 2.6, 0.0, NULL);
      PaddText(rp, &grd, 1 + k, 3, 1.0, 0.0, 2.0, 0.0, cformat("%d", (ph + k)->tax), DT_RGT);
    } else {
      SetTextFont(rp, NULL, 2.2, 0, 0);
      PaddText(rp, &grd, 1 + k, 3, 1.0, 0.0, 2.0, 2.8, "撪", DT_LFT);
      SetTextStyle(rp, 2.6, 2.6, 0.0, NULL);
      PaddText(rp, &grd, 1 + k, 3, 1.0, 0.0, 2.0, 2.8, cformat("%d", (ph + k)->unc), DT_RGT);
      PaddText(rp, &grd, 1 + k, 3, 1.0, 2.8, 2.0, 0.0, cformat("%d", (ph + k)->tax), DT_RGT);
    }
  }

  SetGrid0(&grd, 12.0, 66.3, 1, 1, 126.0, 9.5);
  rst = RST(lstB, 0, lstB, lstB);
  DrawGridCell(rp, &grd, 0, 0, rst);
  SetTextFont(rp, NULL, 2.4, 0, 0);
  PaddText(rp, &grd, 0, 0, 0.0, 0.0, 118.0, 6.5, "(揈梫)", DT_CEN);
  SetTextFont(rp, NULL, 2.6, 0, 0);
  PaddText(rp, &grd, 0, 0, 8.0, 0.0, 1.0, 0.0, ph->rem, DT_AUT);

  SetGrid1(&grd, 12.0, 75.8, NELEM(cw0), 2, cw0, 7.7);
  for (i = 0; i < grd.nr; i++) {
    for (j = 0; j < grd.nc; j++) {
      rst = RST((j == 0)? lstB: 0, 0, (j == grd.nc-1)? lstB: lstR, (i == grd.nr-1)? lstB: (i == 0 && j == 0)? 0: lstR);
      DrawGridCell(rp, &grd, i, j, rst);
    }
  }
  SetTextFont(rp, NULL, 2.4, 0, 0);
  PaddText(rp, &grd, 0, 0, 0.0, 6.6, 0.0,-1.1, "巟 暐 幰", DT_CEN);
  SetTextFont(rp, NULL, 2.4, 0, 0);
  PaddText(rp, &grd, 0, 1, 0.0, 1.3, 0.0, 4.2, "廧強(嫃強)", DT_CEN);
  PaddText(rp, &grd, 0, 1, 0.0, 4.2, 0.0, 1.3, "枖偼強嵼抧", DT_CEN);
  PaddText(rp, &grd, 1, 1, 0.0, 1.3, 0.0, 4.2, "巵 柤 枖偼", DT_CEN);
  PaddText(rp, &grd, 1, 1, 0.0, 4.2, 0.0, 1.3, "柤丂丂丂徧", DT_CEN);
  SetTextFont(rp, NULL, 2.4, 0, 0);
  PaddText(rp, &grd, 1, 2, 27.0, 4.7, 67.0, 0.0, "(揹榖)", DT_CEN);
  SetTextFont(rp, NULL, 3.7, 0, 0);
  PaddText(rp, &grd, 0, 2, 2.0, 0.0, 2.0, 0.0, "搶嫗搒仩仩仩嬫仜仜挰1-23-45", DT_AUT);
  PaddText(rp, &grd, 1, 2, 2.0, 0.0,47.0, 2.0, "仜仮仩仜乮姅乯", DT_AUT);
  SetTextFont(rp, NULL, 2.6, 0, 0);
  PaddText(rp, &grd, 1, 2, 36.0, 4.7, 0.0, 0.0, "012-345-6789", DT_LFT);
  SetLineStyle(rp, lstR);
  DrawLine(rp, 93.0, 83.5, 93.0, 91.2);
  DrawLine(rp, 93.0, 86.5, 138.0, 86.5);
  SetLineStyle(rp, lstD);
  for (j = 1; j < 13; j++) {
    DrawLine(rp, 93.0 + j * 45.0/13, 86.5, 93.0 + j * 45.0/13, 91.2);
  }
  SetCell(&cel, 93.0, 83.5, 138.0, 86.5);
  SetTextFont(rp, NULL, 2.4, 0, 0);
  CellText(rp, &cel, "屄恖斣崋枖偼朄恖斣崋", DT_CEN);
  if (sw) {
    SetTextStyle(rp, 2.6, 2.6, 0.0, NULL);
    for (j = 0; j < 13; j++) {
      SetCell(&cel, 93.0 + j * 45.0/13, 86.5, 93.0 + (j + 1) * 45.0/13, 91.2);
      CellText(rp, &cel, PINdig(1234567890123.0, j), DT_CEN);
    }
  }

  SetGrid1(&grd, 12.0, 92.0, NELEM(cw3), 1, cw3, 4.6);
  for (i = 0; i < grd.nr; i++) {
    for (j = 0; j < grd.nc; j++) {
      rst = RST((j == 0)? lstB: 0, (i == 0)? lstB: 0, (j == grd.nc-1)? lstB: lstR, (i == grd.nr-1)? lstB: lstR);
      DrawGridCell(rp, &grd, i, j, rst);
    }
  }
  SetTextFont(rp, NULL, 2.6, 0, 0);
  GridText(rp, &grd, 0, 0, "惍丂丂棟丂丂棑", DT_CEN);
  GridText(rp, &grd, 0, 1, " 嘆", DT_LFT);
  GridText(rp, &grd, 0, 2, " 嘇", DT_LFT);
  SetTextFont(rp, NULL, 3.7, 0, 0);
  PaddText(rp, &grd, 0, 1,10.0, 0.0, 0.0, 0.0, "01234", DT_LFT);
  PaddText(rp, &grd, 0, 2,10.0, 0.0, 0.0, 0.0, "01234567", DT_LFT);

  SetCell(&cel, 12.0, 96.6, 138.0, 99.8);
  SetTextFont(rp, NULL, 2.6, 0, 0);
  CellText(rp, &cel, "309", DT_RGT);

  SetLineStyle(rp, lstD);
  DrawLine(rp, 0.0, 105.0, 148.5, 105.0);
  DrawLine(rp, 148.5, 0.0, 148.5, 105.0);
}

typedef struct {
  int  n1a;
  int  n1b;
  int  n2a;
  int  n2b;
} D01P;

BOOL ReportMain(HREP rp, int msg, int pg, UINT pa)
{ // 巟暐挷彂
  int j, k, re;
  HBUF rb;
  static int i, n, nb, nr;
  static HOPN op;
  static PCHOUD dh;
  static D01P par = { 1, 1, 1, 1 };
  static COLI ci[4] = {
    { TID_I4B,  2, "n1a", NULL, "屄恖乛採弌梡(斣崋桳)(&I)", },
    { TID_I4B,  2, "n1b", NULL, "屄恖乛岎晅梡(斣崋柍)(&N)", },
    { TID_I4B,  2, "n2a", NULL, "朄恖乛採弌梡(斣崋桳)(&C)", },
    { TID_I4B,  2, "n2b", NULL, "朄恖乛岎晅梡(斣崋柍)(&O)", },
  };
  static COLS cs = { ci, NELEM(ci) };
  static TBLI ti = { &cs, };

  if (msg == REPM_CREATE) {
    if (!ConnectDB("SAMPLE07A")) return FALSE;
  } else if (msg == REPM_SETUP) {
    op = OpenTable("HOUDAT", OM_RDO); if (!op) return FALSE;
    dh = TablePtr(OHTBL(op)); nr = NTableRows(OHTBL(op));
    rb = CreateRowBufX(&par, &ti);
    re = EditRowDlgBox(hWND(rp), rb, "巟暐挷彂");
    DeleteRowBuf(rb);
    if (!re) return FALSE;
    n = 0;
    for (i = 0; i < nr; i++) {
      switch (dh[i].atr) {
      case 1: n += (par.n1a + par.n1b); break;
      case 2: n += (par.n2a + par.n2b); break;
      }
    }
    SetPageSize(rp, 297.0, 210.0);
    SetReqBodyWidth(rp, 0.0, RSW_SKP);
    SetNPages(rp, (n + 3) / 4);
  } else if (msg == REPM_STARTPRINT) {
    i = n = 0;
  } else if (msg == REPM_DRAWPAGE || msg == REPM_SKIPPAGE) {
    for (j = 0; j < 4; j++) {
      if (n == 0) { _L0:
        while (i < nr && dh[i].atr != 1 && dh[i].atr != 2) i++;
        if (i >= nr) break;
        switch (dh[i].atr) {
        case 1: n = par.n1a + par.n1b; nb = par.n1b; break;
        case 2: n = par.n2a + par.n2b; nb = par.n2b; break;
        }
        if (n == 0) { i++; goto _L0;}
      }
      SetOrigin(rp, (j % 2) * 148.5, (j / 2) * 105.0);
      k = 1;
      while (i + k < nr && dh[i + k].atr == 3) k++;
      PrintSlip(rp, &dh[i], k, n > nb);
      n--;
      if (n == 0) i++;
    }
  } else if (msg == REPM_DESTROY) {
    DisconnectDB();
  }
  return TRUE;
}
