  <template>
    <b-card class="main-card mb-3">
      <div v-if="!this_admin.data && mode == 'edit'" class="text-center">
        <vue-element-loading :active="true" spinner="line-scale" color="var(--primary)"></vue-element-loading>
      </div>
      <div v-if="this_admin.data || mode == 'create'">
        <form-wizard 
          @on-complete="onComplete"
          title="" 
          subtitle=""
          finish-button-text="Submit"
          @keydown="form.errors.clear($event.target.name)"
          color="var(--primary)"
        >
          <tab-content 
            title="Details"
            :before-change="validOne"
            ref="valid_one"
            icon="fa fa-info-circle"
          >
            <!-- ///////////////////////////////////////////////////// -->
            <image-input
              :input_name="'img'"
              :form="form"
              :label="'Profile Img'"
              :exist_img="exist_img"
            ></image-input>
            <!-- ///////////////////////////////////////////////////// -->
            <text-input
              :input_name="'name'"
              :form="form"
              :label="'Name*'"
            ></text-input>
            <!-- ///////////////////////////////////////////////////// -->
            <text-input
              :input_name="'email'"
              :form="form"
              :label="'Email*'"
            ></text-input>
            <!-- ///////////////////////////////////////////////////// -->
            <password-input
              :input_name="'password'"
              :form="form"
              :label="'Password*'"
            ></password-input>
            <!-- ///////////////////////////////////////////////////// -->
            <password-input
              :input_name="'password_confirmation'"
              :form="form"
              :label="'Confirm Password*'"
            ></password-input>
            <!-- ///////////////////////////////////////////////////// -->
            <select-option-auto-complete-input
              v-if="mode != 'profile'"
              :input_name="'role_id'"
              :form="form"
              :exist_data="exist_admin_role"
              :placeholder="''"
              :label="'Role*'"
              :endpoint="'admin-role/list'"
              :search_field="'name'"
              :limit_tag="1"
              :return_key="'hash_id'"
            ></select-option-auto-complete-input>
            <!-- ///////////////////////////////////////////////////// -->
            <text-read-only-input
              v-if="mode == 'profile'"
              :value="this_admin.data.role_list[0].name"
              :label="'Role'"
            >
            </text-read-only-input>
            <!-- ///////////////////////////////////////////////////// -->
            <switch-input
              :input_name="'status'"
              :form="form"
              :label="'Status'"
            ></switch-input>
            <!-- ///////////////////////////////////////////////////// -->
          </tab-content>
        </form-wizard>
      </div>
    </b-card>
  </template>

  <script>
    import TextInput from '../../components/inputs/TextInput';
    import TextReadOnlyInput from '../../components/inputs/TextReadOnlyInput';
    import PasswordInput from '../../components/inputs/PasswordInput';
    import SwitchInput from '../../components/inputs/SwitchInput';
    import SelectOptionAutoCompleteInput from '../../components/inputs/SelectOptionAutoCompleteInput';
    import ImageInput from '../../components/inputs/ImageInput';

    export default {
      props: {
        mode: String
      },
      components: {
        TextInput,
        TextReadOnlyInput,
        PasswordInput,
        SelectOptionAutoCompleteInput,
        SwitchInput,
        ImageInput
      },
      data() {
        return {
          form: new Form({
            img: [],
            name: '',
            email: '',
            password: '',
            password_confirmation: '',
            role_id: [],
            status: 1
          }),
          this_admin: [],
          exist_img: [],
          exist_admin_role: []
        }
      },
      methods: {
        validOne() {
          let vm = this;
          let ref_inputs = ['img', 'name', 'email', 'password', 'password_confirmation', 'role_id', 'status'];
          let url = vm.mode == 'create' ? 'admin/valid-check' : 'admin/valid-check/' + vm.this_admin.data.hash_id;
          return General.validCheck(url, vm.form, ref_inputs);
        },
        onComplete() {
          let vm = this;
          let method = vm.mode == 'create' ? 'post' : 'put';
          let url = vm.mode == 'create' ? 'admin/store' : 'admin/update/' + vm.this_admin.data.hash_id;

          return General.onComplete(url, vm.form, method)
            .then(data => {
              if (vm.mode != 'profile') {
                  let pervious_query = data.pervious_query ? '?' + data.pervious_query : '';
                  vm.$router.push('/admin/list' + pervious_query);
              }
            
              if (vm.mode == 'create') {
                General.toastedMessage('create', 'Admin', data.data.name);
              }
              else {
                General.toastedMessage('edit', 'Admin', data.data.name);
              }
            });
        },
        loadThisAdmin() {
          let vm = this;
          let url = vm.mode == 'edit' ? 'admin/show/' + vm.$route.params.hash_id : 'admin/self';
          let form = new Form({});
          General.getCall(url, form).then(data => {
            vm.this_admin = data;
            vm.exist_img = vm.this_admin.data.img;
            vm.form.name = vm.this_admin.data.name;
            vm.form.email = vm.this_admin.data.email;
            vm.form.status = vm.this_admin.data.status;
            vm.form.role_id  = General.getKey(vm.this_admin.data.role_list, 'id');
            vm.exist_admin_role = General.autoCompleteKeyConvert(vm.this_admin.data.role_list);
          });
        }
      },
      created() {
        let vm = this;
        if (vm.mode == 'edit' || vm.mode == 'profile') {
          vm.loadThisAdmin();
        }
      }
    }
</script>