<template>
  <CollapseContainer :title="t('routes.personalSetting.basicSetting')" :canExpan="false">
    <a-row :gutter="24">
      <a-col :span="10">
        <BasicForm @register="register" />
        <div class="px-5 py-15 flex justify-center">
          <Button type="primary" @click="handleSubmit">
            {{ t('routes.personalSetting.updateBtn') }}
          </Button>
        </div>
      </a-col>
      <!-- <a-col :span="10">
        <div class="change-avatar">
          <div class="mb-2">头像</div>
          <CropperAvatar
            :uploadApi="uploadApi"
            :value="avatar"
            btnText="更换头像"
            :btnProps="{ preIcon: 'ant-design:cloud-upload-outlined' }"
            @change="updateAvatar"
            width="150"
          />
        </div>
      </a-col> -->
    </a-row>
  </CollapseContainer>
</template>
<script lang="ts">
  import { Button, Row, Col } from 'ant-design-vue';
  import { defineComponent, onMounted } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { CollapseContainer } from '/@/components/Container';
  // import { CropperAvatar } from '/@/components/Cropper';

  import { useMessage } from '/@/hooks/web/useMessage';
  // import headerImg from '/@/assets/images/header.jpg';
  // import { accountInfoApi } from '/@/api/account/index';
  import { baseSetschemas } from './data';
  import { useUserStore } from '/@/store/modules/user';
  import { updatePasswordApi } from '/@/api/sys/user';
  import { encodeStr } from '/@/utils/encodeUtil';

  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  export default defineComponent({
    components: {
      BasicForm,
      CollapseContainer,
      Button,
      ARow: Row,
      ACol: Col,
      // CropperAvatar,
    },
    setup() {
      const { createMessage } = useMessage();
      const userStore = useUserStore();

      const [register, { validate }] = useForm({
        labelWidth: 120,
        schemas: baseSetschemas,
        showActionButtonGroup: false,
      });

      onMounted(async () => {
        // const data = await accountInfoApi();
        // setFieldsValue(data);
      });

      // const avatar = computed(() => {
      //   const { avatar } = userStore.getUserInfo;
      //   return avatar || headerImg;
      // });

      // function updateAvatar(src: string) {
      //   const userinfo = userStore.getUserInfo;
      //   userinfo.avatar = src;
      //   userStore.setUserInfo(userinfo);
      // }
      async function handleSubmit() {
        try {
          const values = await validate();
          const userInfo = userStore.getUserInfo;
          console.log('userInfo', values, userInfo);
          const res = await updatePasswordApi({
            id: userInfo.id,
            password: encodeStr(window.btoa(values.password)),
            newPassword: encodeStr(window.btoa(values.newPassword)),
          });
          console.log('res', res);
          createMessage.success(t('common.optSuccess'));
          userStore.logout(true);
        } catch (error) {}
      }

      return {
        // avatar,
        register,
        // uploadApi: uploadApi as any,
        // updateAvatar,
        handleSubmit,
        t,
      };
    },
  });
</script>

<style lang="less" scoped>
  .change-avatar {
    img {
      display: block;
      margin-bottom: 15px;
      border-radius: 50%;
    }
  }
</style>
