<template>
  <div>
    <h2 class="content-block">Lihat Profil</h2>

    <div class="content-block dx-card responsive-paddings">
      <form @submit.prevent="handleSubmit">
      <DxForm :form-data="formData">
        <template #avatar-template="{}">
          <div class="form-avatar">
            <img :src="imageSrc" />
          </div>
        </template>
        <DxGroupItem :col-count="8" css-class="first-group">
          <DxSimpleItem :col-span="1" template="avatar-template" />
          <DxGroupItem :col-span="7">
            <DxGroupItem :col-count="1">
              <DxSimpleItem data-field="nama" />
            </DxGroupItem>
            <DxGroupItem :col-count="2">
              <DxSimpleItem data-field="bidangId" editor-type="dxSelectBox" :editor-options="bidangOptions" css-class="bt-5" />
              <DxSimpleItem data-field="jabatanId" editor-type="dxSelectBox" :editor-options="jabatanOptions" css-class="bt-5" />
            </DxGroupItem>
            <DxGroupItem :col-count="1">
              <DxSimpleItem data-field="roleId" editor-type="dxSelectBox" :editor-options="roleOptions" />
            </DxGroupItem>
            <DxGroupItem :col-count="2">
              <DxSimpleItem data-field="email" />
              <DxSimpleItem data-field="phone" />
            </DxGroupItem>
          </DxGroupItem>
        </DxGroupItem>
        <DxButtonItem
          :button-options="{ text: 'Simpan', type: 'success', useSubmitBehavior: true }"
          horizontal-alignment="right"
        />
      </DxForm>
      </form>
    </div>
  </div>
</template>

<script>
import {
  DxForm,
  DxSimpleItem,
  DxGroupItem,
  DxButtonItem,
  // DxLabel,
} from 'devextreme-vue/form';
import notify from 'devextreme/ui/notify';

const baseUrl = process.env.VUE_APP_ROOT_API;

export default {
  props: {
    picture: String
  },
  data() {
    return {
      formData: {
        nama: '',
        username: '',
        email: '',
        roleId: null,
        bidangId: null,
        jabatanId: null,
        instansiId: null,
        phone: '',
      },
      bidangOptions: {
        items: ['Bidang 1', 'Bidang 2', 'Bidang 3'],
        searchEnabled: true,
        value: null,
      },
      jabatanOptions: {
        items: ['Jabatan 1', 'Jabatan 2', 'Jabatan 3'],
        searchEnabled: true,
        value: null,
      },
      roleOptions: {
        items: ['Role 1', 'Role 2', 'Role 3'],
        searchEnabled: true,
        value: null,
      }
    }
  },
  methods: {
    handleSubmit(e) {
      e.preventDefault();

      fetch(`${baseUrl}/users/${this.formData.userId}`, {
        method: 'PUT',
        body: JSON.stringify(this.formData),
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
        },
      })
      .then((res) => res.json())
      .then(() => {
        notify({
          message: 'Profil telah disimpan.',
          position: {
            my: 'center top',
            at: 'center top',
          },
        }, 'success', 3000);
      })
    },
  },
  setup() {
    const picture = "images/employees/02.png";

    const imageSrc = `https://js.devexpress.com/Demos/WidgetsGallery/JSDemos/${picture}`;

    const colCountByScreen = {
      xs: 1,
      sm: 2,
      md: 3,
      lg: 4
    }

    return {
      imageSrc,
      colCountByScreen
    };
  },
  components: {
    DxForm,
    DxSimpleItem,
    DxGroupItem,
    DxButtonItem,
  },

  mounted() {
    const user = JSON.parse(window.localStorage.getItem('user'));
    this.formData.userId = user.userId;

    fetch(`${baseUrl}/users/${user.userId}`)
      .then((res) => res.json())
      .then((json) => {
        this.formData.nama = json.nama;
        this.formData.username = json.username;
        this.formData.email = json.email;
        this.formData.phone = json.phone;
        this.formData.roleId = json.roleId;
        this.formData.bidangId = json.bidangId;
        this.formData.instansiId = json.instansiId;
        this.formData.jabatanId = json.jabatanId;
      })
  }
};
</script>

<style lang="scss">
.form-avatar {
  float: left;
  height: 120px;
  border-radius: 50%;
  width: 120px;
  margin-right: 20px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  background-color: #fff;
  overflow: hidden;

  img {
    height: 120px;
    display: block;
    margin: 0 auto;
  }
}
</style>
