<script setup>
import { ref, onMounted, computed } from "vue";

import { post } from "@/utils/request";
import { userToken } from "@/composables/config";
import { userApi } from "@/api/user";
import { ucardApi } from "@/api/ucard";
import useUserStore from "@/stores/use-user-store";
import useRouter from "@/hooks/useRouter";
import { useI18n } from "vue-i18n";
const router = useRouter();
const { t } = useI18n();
const userStore = useUserStore();
// 响应式表单数据
const form = ref({
  loginName: "",
  password: "",
});

function submit() {
  if (!form.value.loginName) {
    uni.$u.toast(t("signin.form.email"));
    return;
  }
  if (!form.value.password) {
    uni.$u.toast(t("signin.form.password"));
    return;
  }

  handleLogin();
}

const customStyle = {
  height: "44px",
  "border-radius": "8px",
  background: "#f7f8fa",
  padding: "0 20px !important",
  position: "relative",
};
const remenber = ref([]);
const checkboxChange = (e) => {
  remenber.value = e;
};
const fetchUserList = (params) => post("/Login/AcctLogin", params);
async function handleLogin() {
  try {
    const res = await userApi.login({
      loginName: form.value.loginName,
      password: form.value.password,
    });
    if (res.code === 200) {
      userToken.value = res.data;
      uni.$u.toast(t("login.msg0_1"));
      getUserInfo();
      reasonsRefusalList();
      if (remenber.value.length) {
        userStore.saveAccountInfo({
          loginName: form.value.loginName,
          password: form.value.password,
          rememberPassword: true,
        });
      } else {
        userStore.saveAccountInfo({
          loginName: "",
          password: "",
          rememberPassword: false,
        });
      }
      //  console.log(1111);
    } else {
      //  console.log(12112);
    }
  } catch (error) {
    //  console.log(error, 19089);
  }
}
async function getUserInfo() {
  try {
    const res = await ucardApi.getSingle();
    userStore.saveUserInfo(res.data);
    if (res.code === 200) {
      if (!res.data || res.data.approveStatus != 2) {
        router.push("/pages/mine/improve");
      } else {
        router.push("/pages/card/index");
      }
    } else {
      uni.$u.toast(res.msg || t("login.msg0"));
    }
  } catch (error) {
    //  console.log(error, 111);
  }
}
async function reasonsRefusalList() {
  try {
    const res = await ucardApi.reasonsRefusalList();
    if (res.code === 200) {
      pickFields(res.data);
    } else {
      uni.$u.toast(res.msg || t("login.msg0"));
    }
  } catch (error) {
    //  console.log(error, 111);
  }
}
function pickFields(source, fields = ['content', 'enContent']) {
  const result = {}

  Object.entries(source).forEach(([key, value]) => {
    result[key] = fields.reduce((acc, f) => {
      acc[f] = value[f] ?? null
      return acc
    }, {})
  })
  userStore.saveReasonsOptions(result);
}

onMounted(() => {
  const accountInfo = userStore.accountInfo;
  if (accountInfo?.rememberPassword) {
    form.value.loginName = accountInfo?.loginName || "";
    form.value.password = accountInfo?.password || "";
    remenber.value = ["记住我"];
  } else {
    form.value.loginName = "";
    form.value.password = "";
    remenber.value = [];
  }
});
const inputType = ref("password");
</script>

<template>
  <cwg-page-wrapper>
    <view>
      <view class="company u-flex-y u-flex-y-center">
        <image src="/static/images/logo.png" class="company-icon" mode="widthFix"></image>
      </view>

      <view class="account">
        <view>
          <up-form :model="form" ref="uFormRef">
            <up-form-item label="" prop="loginName">
              <up-input :customStyle="customStyle" v-model="form.loginName" border="none"
                :placeholder="t('signin.form.email')">
                <template #prefix>
                  <cwg-icon name="email-outline" :size="20" color="#000" />
                </template>
              </up-input>
            </up-form-item>

            <up-form-item label="" prop="password">
              <up-input :customStyle="customStyle" v-model="form.password" :type="inputType" border="none"
                :placeholder="t('signin.form.password')">
                <template #prefix>
                  <cwg-icon name="lock-outline" :size="20" color="#000" />
                </template>
              </up-input>
            </up-form-item>
          </up-form>
        </view>
        <view class="u-flex u-flex-between u-flex-y-center">
          <view class="check-box">
            <up-checkbox-group v-model="remenber" @change="checkboxChange">
              <up-checkbox size="14" labelSize="14" labelColor="#666666" activeColor="#ea002a"
                :label="t('newSignin.item5')" name="记住我" class="wcg-checkbox"></up-checkbox>
            </up-checkbox-group>
          </view>
          <navigator url="/pages/login/reset" class="account-tip">
            <text>{{ t("signin.forget") }}</text>
          </navigator>
        </view>
        <view class="cwg-button">
          <u-button type="primary" class="" @click="submit">
            {{ t("signin.login") }}
          </u-button>
        </view>

        <!-- <navigator url="/pages/login/regist" class="account-tip">
          {{ t("signin.words") }}
          <text>{{ t("signin.signup") }}</text>
        </navigator> -->
      </view>
    </view>
  </cwg-page-wrapper>
</template>

<style lang="scss" scoped>
@import "@/uni.scss";

page {
  // padding: px2rpx(5) px2rpx(27);
}

button {
  background-color: #ea002a;
  font-size: px2rpx(14);
  font-weight: normal;
  height: px2rpx(44);
  line-height: px2rpx(44);
}

.company {
  padding: px2rpx(110) 0 px2rpx(50) 0;
  position: relative;
  align-items: flex-start !important;
}

.home-bg {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: -1;
}

.company-icon {
  width: px2rpx(117);
}

.company-head {
  .name {
    font-size: px2rpx(20);
    font-weight: 500;
    color: #ea002a;
    margin-bottom: px2rpx(2);
  }

  .into {
    font-size: px2rpx(14);
    font-weight: 350;
    color: #222222;
  }
}

.account {
  /* border-radius: px2rpx(16) px2rpx(16) px2rpx(0) px2rpx(0); */
  background-color: #ffffff;
  position: relative;
  top: px2rpx(-15);

  /* box-shadow: 0 -1px 4px rgba(97, 96, 96, 0.1); */
}

.input {
  height: px2rpx(44);
  border-radius: px2rpx(8);
  background: #f7f8fa;
  padding: 0 px2rpx(20) !important;
  position: relative;
}

.account-icon {
  width: px2rpx(12);
  height: px2rpx(14) !important;
  margin-right: px2rpx(5);
}

:deep(.u-input__content__prefix-icon) {
  height: px2rpx(20);
}

.regiset-btn {
  margin: px2rpx(20) 0;
}

.account-tip {
  color: #666666;
  font-size: px2rpx(14);
  text-align: center;

  text {
    color: #ea002a;
  }
}

:deep(.u-form-item__body) {
  padding: 0 !important;
  padding-bottom: px2rpx(14) !important;
}

:deep(.wcg-checkbox) {
  padding: 0 !important;
  /* .u-checkbox__icon-wrap {
    background-color: #ea002a !important;
    border-color: #ea002a !important;
  } */
}

.cwg-button {
  padding-top: px2rpx(14) !important;
}
</style>
