<template>
  <div class="ele-body ele-body-card">
    <a-card :bordered="false" class="user-info-tabs" style="min-height: 600px">
      <a-tabs v-model:active-key="active" size="large">
        <a-tab-pane tab="基本信息" key="info">
          <a-form
            :label-col="{ md: { span: 6 }, sm: { span: 24 } }"
            :wrapper-col="{ md: { span: 18 }, sm: { span: 24 } }"
          >
            <a-form-item label="商城名称" v-bind="validateInfos.storeName">
              <a-input
                v-model:value="form.storeName"
                placeholder="请输入商城名称"
                allow-clear
              />
            </a-form-item>
            <a-form-item label="商城全称" v-bind="validateInfos.fullName">
              <a-input
                v-model:value="form.fullName"
                placeholder="请输入商城名称"
                allow-clear
              />
            </a-form-item>
            <a-form-item label="商城介绍">
              <a-textarea
                v-model:value="form.comments"
                placeholder="请输入商城介绍"
                :rows="4"
              />
            </a-form-item>
            <a-form-item :wrapper-col="{ md: { offset: 6 } }">
              <a-button type="primary" :loading="loading" @click="save">
                {{ loading ? '保存中..' : '保存更改' }}
              </a-button>
            </a-form-item>
          </a-form>
        </a-tab-pane>
      </a-tabs>
    </a-card>
    <!-- 头像裁剪弹窗 -->
    <ele-cropper-modal
      :src="form.avatar"
      v-model:visible="visible"
      @done="onCrop"
    />
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive, computed, watch, unref } from 'vue';
  import {
    UploadOutlined,
    UserOutlined,
    HomeOutlined,
    EnvironmentOutlined,
    TagOutlined,
    QqOutlined,
    WechatOutlined,
    AlipayOutlined
  } from '@ant-design/icons-vue';
  import { Form, message } from 'ant-design-vue';
  import { assignObject } from 'ele-admin-pro';
  import { useUserStore } from '@/store/modules/user';
  import { useRouter } from 'vue-router';
  import { addStore, listStore, updateStore } from '@/api/merchant/setting';
  import type { Store } from '@/api/merchant/setting/model';

  const { currentRoute } = useRouter();

  const useForm = Form.useForm;

  const userStore = useUserStore();
  // 是否是修改
  const isUpdate = ref(false);
  // tab页选中
  const active = ref('info');

  // 保存按钮loading
  const loading = ref(false);

  // 是否显示裁剪弹窗
  const visible = ref(false);

  // 登录用户信息
  const loginUser = computed(() => userStore.info ?? {});

  // 表单数据
  const form = reactive<Store>({
    storeName: '',
    fullName: '',
    comments: '',
    storeId: undefined
  });

  // 表单验证规则
  const rules = reactive({
    storeName: [
      {
        required: true,
        message: '请输入系统名称',
        type: 'string',
        trigger: 'blur'
      }
    ]
  });

  const { validate, validateInfos } = useForm(form, rules);

  /* 修改登录用户 */
  const updateLoginUser = (obj: Record<string, any>) => {
    userStore.setInfo(assignObject({ ...loginUser.value }, obj));
  };

  /* 保存更改 */
  const save = () => {
    validate()
      .then(() => {
        loading.value = true;
        const data = {
          ...form
          // 处理其他数据
        };
        const saveOrUpdate = isUpdate.value ? updateStore : addStore;
        saveOrUpdate(data)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  /* 头像裁剪完成回调 */
  const onCrop = (result: string) => {
    form.avatar = result;
    visible.value = false;
    updateLoginUser(form);
  };

  /* 打开图片裁剪 */
  const openCropper = () => {
    visible.value = true;
  };

  const query = () => {
    console.log('>>>>');
    listStore().then((res) => {
      if (res[0]) {
        isUpdate.value = true;
        assignObject(form, res[0]);
      }
    });
  };

  watch(
    currentRoute,
    (route) => {
      const { path } = unref(route);
      if (path !== '/shop/store') {
        return;
      }
      query();
    },
    {
      immediate: true
    }
  );
</script>

<script lang="ts">
  export default {
    name: 'UserProfile'
  };
</script>

<style lang="less" scoped>
  /* 用户资料卡片 */
  .user-info-avatar-group {
    margin: 16px 0;
    display: inline-block;
    position: relative;
    cursor: pointer;

    .user-info-avatar-icon {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      color: #fff;
      font-size: 30px;
      display: none;
      z-index: 2;
    }

    &:hover .user-info-avatar-icon {
      display: block;
    }

    &:after {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: transparent;
      transition: background-color 0.3s;
    }

    &:hover:after {
      background-color: rgba(0, 0, 0, 0.4);
    }

    & + h1 {
      margin-bottom: 8px;
    }
  }

  /* 用户信息列表 */
  .user-info-list {
    margin: 47px 0 32px 0;

    .ele-cell + .ele-cell {
      margin-top: 16px;
    }

    & + .ant-divider {
      margin-bottom: 16px;
    }
  }

  /* 用户标签 */
  .user-info-tags {
    margin: 16px 0 4px 0;

    .ant-tag {
      margin: 0 12px 8px 0;
    }
  }

  /* 右侧卡片 */
  .user-info-tabs {
    :deep(.ant-card-body) {
      padding: 0;
    }

    :deep(.ant-tabs-tab) {
      padding-left: 4px;
      padding-right: 4px;
      margin: 0 12px 0 28px !important;
    }

    .ant-form {
      max-width: 580px;
      margin-top: 20px;
      padding: 0 24px;
    }
  }

  /* 用户账号绑定列表 */
  .user-account-list {
    margin-bottom: 16px;

    & > .ele-cell {
      padding: 18px 34px;
    }

    .user-account-icon {
      color: #fff;
      padding: 8px;
      font-size: 26px;
      border-radius: 50%;

      &.anticon-qq {
        background: #3492ed;
      }

      &.anticon-wechat {
        background: #4daf29;
      }

      &.anticon-alipay {
        background: #1476fe;
      }
    }
  }
</style>
