  <template>
    <b-card class="main-card mb-3">
      <div v-if="!this_entity.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_entity.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"
          >
            <!-- ///////////////////////////////////////////////////// -->
            <text-input
              :input_name="'name'"
              :form="form"
              :label="'Name*'"
            ></text-input>
            <!-- ///////////////////////////////////////////////////// -->
          </tab-content>
        </form-wizard>
      </div>
    </b-card>
  </template>

  <script>
    import TextInput from '../../components/inputs/TextInput';

    export default {
      props: {
        mode: String
      },
      components: {
        TextInput
      },
      data() {
        return {
          form: new Form({
            name: '',
          }),
          this_entity: [],
        }
      },
      methods: {
        validOne() {
          let vm = this;
          let ref_inputs = ['name'];
          let url = vm.mode == 'create' ? 'entity/valid-check' : 'entity/valid-check/' + vm.this_entity.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' ? 'entity/store' : 'entity/update/' + vm.this_entity.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('/entity/list' + pervious_query);
              }
            
              if (vm.mode == 'create') {
                General.toastedMessage('create', 'Entity', data.data.hash_id);
              }
              else {
                General.toastedMessage('edit', 'Entity', data.data.hash_id);
              }
            });
        },
        loadThisContractTemplate() {
          let vm = this;
          let url = vm.mode == 'edit' ? 'entity/show/' + vm.$route.params.hash_id : 'entity/self';
          let form = new Form({});
          General.getCall(url, form).then(data => {
            vm.this_entity = data;
            vm.form.name = vm.this_entity.data.name;
            vm.form.status = vm.this_entity.data.status;
          });
        }
      },
      created() {
        let vm = this;
        if (vm.mode == 'edit' || vm.mode == 'profile') {
          vm.loadThisContractTemplate();
        }
      }
    }
</script>