<template>
  <div class="overflow-y-auto h-full overflow-x-hidden px-2">
    <CollapseContainer :title="t('基本信息')" :iconStyle="{ fontSize: '13px' }">
      <BasicForm @register="register" />
      <div class="text-center mt-5">
        <a-button type="primary" @click="handleSubmit" class="w-36 !rounded">
          {{ t('更新基本信息') }}
        </a-button>
      </div>
    </CollapseContainer>
    <CollapseContainer :title="t('个人资料')" :iconStyle="{ fontSize: '13px' }">
      <a-descriptions :column="1">
        <a-descriptions-item label="所属组织" :labelStyle="labelStyle">
          <a-tag color="processing" v-for="item in departmentNameList" :key="item">{{
            item
          }}</a-tag>
        </a-descriptions-item>
        <a-descriptions-item label="所属角色" :labelStyle="labelStyle">
          <a-tag color="red" v-for="item in roleNameList" :key="item">{{ item }}</a-tag>
        </a-descriptions-item>
        <a-descriptions-item label="所属岗位" :labelStyle="labelStyle">
          <a-tag color="green" v-for="item in postNameList" :key="item">{{ item }}</a-tag>
        </a-descriptions-item>
      </a-descriptions>
    </CollapseContainer>
  </div>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { CollapseContainer } from '/@/components/Container/index';

  import { useMessage } from '/@/hooks/web/useMessage';

  import { baseSetschemas } from './data';
  import { useUserStore } from '/@/store/modules/user';
  import { getUserProfile, updateUserInfo } from '/@/api/system/user';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  const labelStyle = { width: '70px', textAlign: 'right', display: 'block' };
  export default defineComponent({
    components: {
      BasicForm,
      CollapseContainer,
    },
    setup() {
      const { createMessage } = useMessage();
      const userStore = useUserStore();
      const id = ref('');
      const departmentNameList = ref([]);
      const postNameList = ref([]);
      const roleNameList = ref([]);

      const [register, { setFieldsValue, validate }] = useForm({
        schemas: baseSetschemas,
        showActionButtonGroup: false,
        layout: 'vertical',
        rowProps: {
          gutter: 32,
        },
      });

      onMounted(async () => {
        const data = userStore.getUserInfo;
        id.value = data.id;
        setFieldsValue(data);
        getUserProfile().then((res) => {
          departmentNameList.value = res.departmentNameList || [];
          postNameList.value = res.postNameList || [];
          roleNameList.value = res.roleNameList || [];
        });
      });

      return {
        register,
        postNameList,
        roleNameList,
        departmentNameList,
        labelStyle,
        handleSubmit: async () => {
          const values = await validate();
          const userInfo = userStore.getUserInfo;

          values.id = id.value;
          await updateUserInfo(values);

          userInfo.id = id.value;
          userInfo.name = values.name;
          userInfo.mobile = values.mobile;
          userInfo.email = values.email;
          userInfo.remark = values.remark;
          userInfo.address = values.address;

          userStore.setUserInfo(userInfo);

          createMessage.success(t('更新成功！'));
        },
        t,
      };
    },
  });
</script>

<style lang="less" scoped>
  :deep(.vben-collapse-container__header) {
    height: 50px;
    line-height: 50px;
    margin: 0 8px 20px;
    padding: 16px 0;

    .vben-basic-title-normal {
      font-weight: bold;
      color: @primary-color;
      padding: 0;

      &::before {
        width: 4px;
        height: 16px;
        background: #1c8dff;
        border-radius: 2px;
        content: '';
        margin-right: 8px;
        display: block;
      }
    }
  }
</style>
