  <template>
    <b-card class="main-card mb-3">
      <div v-if="!this_contract.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_contract.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"
          >
            <!-- ///////////////////////////////////////////////////// -->
            <select-option-auto-complete-input
              :input_name="'contract_template_id'"
              :form="form"
              :placeholder="''"
              :label="'Clone Contract Template*'"
              :endpoint="'contract-template/list'"
              :search_field="'name'"
              :limit_tag="1"
              :return_key="'hash_id'"
            ></select-option-auto-complete-input>
            <!-- ///////////////////////////////////////////////////// -->
          </tab-content>
        </form-wizard>
      </div>
    </b-card>
  </template>

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

    export default {
      props: {
        mode: String
      },
      components: {
        SelectOptionAutoCompleteInput,
      },
      data() {
        return {
          form: new Form({
            contract_template_id: [],
          }),
          this_contract: [],
        }
      },
      methods: {
        validOne() {
          let vm = this;
          let ref_inputs = ['contract_template_id'];
          let url = vm.mode == 'create' ? 'contract/valid-check' : 'contract/valid-check/' + vm.this_contract.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' ? 'contract/generate' : 'contract/update/' + vm.this_contract.data.hash_id;

          return General.onComplete(url, vm.form, method)
            .then(data => {
              let pervious_query = data.pervious_query ? '?' + data.pervious_query : '';
              vm.$router.push('/contract/list' + pervious_query);
            
              if (vm.mode == 'create') {
                General.toastedMessage('create', 'Contract', data.data.hash_id);
              }
              else {
                General.toastedMessage('edit', 'Contract', data.data.hash_id);
              }
            });
        },
        loadThisContract() {
          let vm = this;
          let url = 'contract/show/' + vm.$route.params.hash_id;
          let form = new Form({});
          General.getCall(url, form).then(data => {
            vm.this_contract = data;
          });
        }
      },
      created() {
        let vm = this;
        if (vm.mode == 'edit') {
          vm.loadThisContract();
        }
      }
    }
</script>