import { test, expect } from "@playwright/test";
import { makeEntry, mockApi, 登录 } from "../测试夹具";

test.describe("报销：从支出详情记录", () => {
  test("01-支出详情可以记录部分报销并保留原支出", async ({ page }) => {
    await mockApi(page, { initialEntries: [makeEntry({ amount: 3200 })] });
    await 登录(page);
    await page.getByText("午餐").click();
    await page.getByRole("button", { name: "记录报销" }).click();
    await page.getByLabel("本次报销金额").fill("20");
    await page.getByRole("button", { name: "确认报销" }).click();
    await expect(page.getByText(/已报销/)).toBeVisible();
    await expect(page.getByText("净支出")).toBeVisible();
  });

  test("02-报销金额不能超过原支出的剩余金额", async ({ page }) => {
    await mockApi(page, { initialEntries: [makeEntry({ amount: 3200 })] });
    await 登录(page);
    await page.getByText("午餐").click();
    await page.getByRole("button", { name: "记录报销" }).click();
    await page.getByLabel("本次报销金额").fill("3201");
    await page.getByRole("button", { name: "确认报销" }).click();
    await expect(page.locator("form.reimbursement-sheet").getByText(/不超过剩余可报销金额/)).toBeVisible();
  });

  test("03-报销到账不出现在普通收入分类统计中", async ({ page }) => {
    await mockApi(page, { initialEntries: [makeEntry(), makeEntry({ id: "reimbursement", type: "income", amount: 1000, categoryId: "restaurant", reimbursementOfEntryId: "entry-lunch", note: "午餐报销" })] });
    await 登录(page);
    await page.goto("/stats");
    await expect(page.getByText("报销到账")).not.toBeVisible();
  });

  test("04-流水中的报销到账标记为不计入并使用蓝色删除线金额", async ({ page }) => {
    await mockApi(page, { initialEntries: [makeEntry(), makeEntry({ id: "reimbursement", type: "income", amount: 1000, categoryId: "restaurant", reimbursementOfEntryId: "entry-lunch", note: "午餐报销" })] });
    await 登录(page);

    const reimbursement = page.locator(".entry-row", { hasText: "午餐报销" });
    await expect(reimbursement.getByText("不计入")).toBeVisible();
    await expect(reimbursement.locator(".entry-value .reimbursement")).toHaveText("¥32.00");
    await expect(reimbursement.locator(".entry-value .reimbursement")).not.toContainText("+");
    await expect(reimbursement.locator(".entry-value .reimbursement")).toHaveCSS("color", "rgb(52, 120, 184)");
    await expect(reimbursement.locator(".entry-value .reimbursement")).toHaveCSS("text-decoration-line", "line-through");
  });

  test("05-报销详情可以跳转到关联原支出", async ({ page }) => {
    await mockApi(page, { initialEntries: [
      makeEntry({ note: "午餐支出" }),
      makeEntry({ id: "reimbursement", type: "income", amount: 1000, categoryId: "restaurant", reimbursementOfEntryId: "entry-lunch", note: "午餐报销" }),
    ] });
    await 登录(page);
    await page.goto("/entries/reimbursement");

    await page.getByRole("link", { name: /关联原支出/ }).click();
    await expect(page).toHaveURL(/\/entries\/entry-lunch$/);
    await expect(page.getByRole("button", { name: "记录报销" })).toBeVisible();
    await expect(page.locator("textarea")).toHaveValue("午餐支出");
  });
});
