import { Q as QImg } from "./use-id-Bga_UBPy.js";
import { Q as QCardSection } from "./QCardSection-BEf1jKxd.js";
import { Q as QCard } from "./QCard-S3y3u9tH.js";
import { _ as _export_sfc, a as defineComponent, c as createBlock, o as openBlock, w as withCtx, b as createVNode, Q as QIcon, e as QBtn, f as withModifiers, r as resolveComponent, g as createBaseVNode, h as createElementBlock, i as createTextVNode } from "./index-BRudWVF6.js";
import { Q as QPage } from "./QPage-y4zftl3-.js";
import { Q as QInput } from "./QInput-C1k2VUPF.js";
import { Q as QForm } from "./QForm-BcQnaZUb.js";
import { u as utils } from "./utils-DT9w33jF.js";
const _sfc_main$2 = defineComponent({
  name: "AuthLogin",
  emits: ["success"],
  data() {
    return {
      loading: false,
      showPassword: false,
      form: {
        username: "",
        password: ""
      }
    };
  },
  methods: {
    viewDemo() {
      window.open("https://static-1322383831.cos.accelerate.myqcloud.com/app/mp4/fbhelper_c1.mp4", "_blank");
    },
    async submit() {
      if (!this.form.username || !this.form.password) {
        utils.notifyError("请输入用户名和密码");
        return;
      }
      this.loading = true;
      try {
        const data = await this.$api.post("/api/main/user/login", {
          username: this.form.username,
          password: this.form.password
        });
        const { token } = data || {};
        if (token) {
          this.$emit("success", { token, username: this.form.username });
        }
      } catch (err) {
        utils.notifyError(err && err.message ? err.message : "登录失败");
      } finally {
        this.loading = false;
      }
    }
  }
});
function _sfc_render$2(_ctx, _cache, $props, $setup, $data, $options) {
  return openBlock(), createBlock(QForm, {
    onSubmit: withModifiers(_ctx.submit, ["prevent"]),
    class: "q-gutter-y-md"
  }, {
    default: withCtx(() => [
      createVNode(QInput, {
        modelValue: _ctx.form.username,
        "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => _ctx.form.username = $event),
        modelModifiers: { trim: true },
        dense: "",
        outlined: "",
        disable: _ctx.loading,
        autocomplete: "username",
        placeholder: "请输入用户名",
        "bg-color": "white",
        class: "auth-input"
      }, {
        prepend: withCtx(() => [
          createVNode(QIcon, {
            name: "person_outline",
            color: "grey-6"
          })
        ]),
        _: 1
      }, 8, ["modelValue", "disable"]),
      createVNode(QInput, {
        modelValue: _ctx.form.password,
        "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => _ctx.form.password = $event),
        dense: "",
        outlined: "",
        type: _ctx.showPassword ? "text" : "password",
        disable: _ctx.loading,
        autocomplete: "current-password",
        placeholder: "请输入密码",
        "bg-color": "white",
        class: "auth-input"
      }, {
        prepend: withCtx(() => [
          createVNode(QIcon, {
            name: "lock_outline",
            color: "grey-6"
          })
        ]),
        append: withCtx(() => [
          createVNode(QBtn, {
            flat: "",
            dense: "",
            size: "sm",
            color: "grey-6",
            icon: _ctx.showPassword ? "visibility" : "visibility_off",
            onClick: _cache[1] || (_cache[1] = ($event) => _ctx.showPassword = !_ctx.showPassword)
          }, null, 8, ["icon"])
        ]),
        _: 1
      }, 8, ["modelValue", "type", "disable"]),
      createVNode(QBtn, {
        class: "full-width",
        color: "primary",
        label: "登录系统",
        type: "submit",
        loading: _ctx.loading,
        unelevated: "",
        "no-caps": "",
        size: "md"
      }, null, 8, ["loading"]),
      createVNode(QBtn, {
        class: "full-width",
        color: "grey-2",
        "text-color": "grey-8",
        label: "查看操作演示",
        unelevated: "",
        "no-caps": "",
        onClick: _ctx.viewDemo
      }, null, 8, ["onClick"])
    ]),
    _: 1
  }, 8, ["onSubmit"]);
}
const AuthLogin = /* @__PURE__ */ _export_sfc(_sfc_main$2, [["render", _sfc_render$2], ["__scopeId", "data-v-ae7f347f"]]);
const _sfc_main$1 = defineComponent({
  name: "AuthRegister",
  emits: ["success"],
  data() {
    return {
      loading: false,
      showPassword: false,
      form: {
        username: "",
        password: "",
        confirmPassword: ""
      }
    };
  },
  methods: {
    async submit() {
      if (!this.form.username || !this.form.password) {
        utils.notifyError("请输入用户名和密码");
        return;
      }
      if (this.form.password !== this.form.confirmPassword) {
        utils.notifyError("两次密码不一致");
        return;
      }
      this.loading = true;
      try {
        const registerRes = await this.$api.post("/api/main/user/register", {
          username: this.form.username,
          password: this.form.password
        });
        if (registerRes === null) return;
        const data = await this.$api.post("/api/main/user/login", {
          username: this.form.username,
          password: this.form.password
        });
        const { token } = data || {};
        if (!token) {
          utils.notifyError("注册成功，但登录未拿到 token（请检查接口返回字段）");
          return;
        }
        utils.notifySuccess("注册成功");
        this.$emit("success", { token, username: this.form.username });
      } catch (err) {
        utils.notifyError(err && err.message ? err.message : "注册失败");
      } finally {
        this.loading = false;
      }
    }
  }
});
function _sfc_render$1(_ctx, _cache, $props, $setup, $data, $options) {
  return openBlock(), createBlock(QForm, {
    onSubmit: withModifiers(_ctx.submit, ["prevent"]),
    class: "q-gutter-y-md"
  }, {
    default: withCtx(() => [
      createVNode(QInput, {
        modelValue: _ctx.form.username,
        "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => _ctx.form.username = $event),
        modelModifiers: { trim: true },
        dense: "",
        outlined: "",
        disable: _ctx.loading,
        autocomplete: "username",
        placeholder: "请输入用户名",
        "bg-color": "white",
        class: "auth-input"
      }, {
        prepend: withCtx(() => [
          createVNode(QIcon, {
            name: "person_outline",
            color: "grey-6"
          })
        ]),
        _: 1
      }, 8, ["modelValue", "disable"]),
      createVNode(QInput, {
        modelValue: _ctx.form.password,
        "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => _ctx.form.password = $event),
        dense: "",
        outlined: "",
        type: _ctx.showPassword ? "text" : "password",
        disable: _ctx.loading,
        autocomplete: "new-password",
        placeholder: "请输入密码",
        "bg-color": "white",
        class: "auth-input"
      }, {
        prepend: withCtx(() => [
          createVNode(QIcon, {
            name: "lock_outline",
            color: "grey-6"
          })
        ]),
        append: withCtx(() => [
          createVNode(QBtn, {
            flat: "",
            dense: "",
            size: "sm",
            color: "grey-6",
            icon: _ctx.showPassword ? "visibility" : "visibility_off",
            onClick: _cache[1] || (_cache[1] = ($event) => _ctx.showPassword = !_ctx.showPassword)
          }, null, 8, ["icon"])
        ]),
        _: 1
      }, 8, ["modelValue", "type", "disable"]),
      createVNode(QInput, {
        modelValue: _ctx.form.confirmPassword,
        "onUpdate:modelValue": _cache[3] || (_cache[3] = ($event) => _ctx.form.confirmPassword = $event),
        dense: "",
        outlined: "",
        type: _ctx.showPassword ? "text" : "password",
        disable: _ctx.loading,
        autocomplete: "new-password",
        placeholder: "请再次输入密码",
        "bg-color": "white",
        class: "auth-input"
      }, {
        prepend: withCtx(() => [
          createVNode(QIcon, {
            name: "lock_outline",
            color: "grey-6"
          })
        ]),
        _: 1
      }, 8, ["modelValue", "type", "disable"]),
      createVNode(QBtn, {
        class: "full-width",
        color: "primary",
        label: "注册账号",
        type: "submit",
        loading: _ctx.loading,
        unelevated: "",
        "no-caps": "",
        size: "md"
      }, null, 8, ["loading"])
    ]),
    _: 1
  }, 8, ["onSubmit"]);
}
const AuthRegister = /* @__PURE__ */ _export_sfc(_sfc_main$1, [["render", _sfc_render$1], ["__scopeId", "data-v-6849fbf7"]]);
const logoUrl = "" + new URL("logo-v1@1x-BErmKY1F.png", import.meta.url).href;
const _sfc_main = defineComponent({
  name: "IndexPage",
  components: {
    AuthLogin,
    AuthRegister
  },
  data() {
    return {
      mode: "login",
      logoUrl
    };
  },
  mounted() {
    const token = localStorage.getItem("token");
    if (token) this.$router.replace("/reels/list");
  },
  methods: {
    toWeb() {
      window.open("https://fbhelper.top", "_blank");
    },
    onAuthSuccess({ token, username }) {
      localStorage.setItem("token", token);
      if (username) localStorage.setItem("username", username);
      this.$router.replace("/reels/list");
    }
  }
});
const _hoisted_1 = { class: "row justify-center items-center q-mt-md text-caption text-grey-7" };
const _hoisted_2 = { key: 0 };
const _hoisted_3 = { key: 1 };
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  const _component_auth_login = resolveComponent("auth-login");
  const _component_auth_register = resolveComponent("auth-register");
  return openBlock(), createBlock(QPage, { class: "popup-page auth-page q-pa-lg" }, {
    default: withCtx(() => [
      createBaseVNode("div", {
        class: "column items-center auth-header",
        onClick: _cache[0] || (_cache[0] = (...args) => _ctx.toWeb && _ctx.toWeb(...args)),
        style: { "cursor": "pointer" }
      }, [
        createVNode(QImg, {
          src: _ctx.logoUrl,
          width: "64px",
          height: "64px",
          fit: "contain"
        }, null, 8, ["src"]),
        _cache[3] || (_cache[3] = createBaseVNode("div", { class: "text-h6 text-weight-bold q-mt-md" }, "批量操作助手", -1)),
        _cache[4] || (_cache[4] = createBaseVNode("div", { class: "text-caption text-grey-7 q-mt-xs" }, "专业版 - 提升工作效率", -1))
      ]),
      createVNode(QCard, {
        class: "auth-card q-mt-lg",
        bordered: ""
      }, {
        default: withCtx(() => [
          createVNode(QCardSection, null, {
            default: withCtx(() => [
              _ctx.mode === "login" ? (openBlock(), createBlock(_component_auth_login, {
                key: 0,
                onSuccess: _ctx.onAuthSuccess
              }, null, 8, ["onSuccess"])) : (openBlock(), createBlock(_component_auth_register, {
                key: 1,
                onSuccess: _ctx.onAuthSuccess
              }, null, 8, ["onSuccess"]))
            ]),
            _: 1
          })
        ]),
        _: 1
      }),
      createBaseVNode("div", _hoisted_1, [
        _ctx.mode === "login" ? (openBlock(), createElementBlock("div", _hoisted_2, [
          _cache[5] || (_cache[5] = createTextVNode(" 新用户？ ", -1)),
          createVNode(QBtn, {
            flat: "",
            dense: "",
            type: "link",
            color: "primary",
            label: "立即注册",
            onClick: _cache[1] || (_cache[1] = ($event) => _ctx.mode = "register")
          })
        ])) : (openBlock(), createElementBlock("div", _hoisted_3, [
          _cache[6] || (_cache[6] = createTextVNode(" 已有账号？ ", -1)),
          createVNode(QBtn, {
            flat: "",
            dense: "",
            type: "link",
            color: "primary",
            label: "立即登录",
            onClick: _cache[2] || (_cache[2] = ($event) => _ctx.mode = "login")
          })
        ]))
      ])
    ]),
    _: 1
  });
}
const Index = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-e014a498"]]);
export {
  Index as default
};
