  <template>
    <b-card class="main-card mb-3">
      <div v-if="!this_contract_template.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_template.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>
            <!-- ///////////////////////////////////////////////////// -->
            <number-input
              :input_name="'complete_day_range'"
              :form="form"
              :label="'Complete Day Range*'"
            ></number-input>
            <!-- ///////////////////////////////////////////////////// -->
          </tab-content>
        </form-wizard>
      </div>
    </b-card>
  </template>

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

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