import {
  addVariables
} from './script.js';

import {
  addCode
} from './template.js';

import {
  getInputCode
} from './input.js';

const elementEnum = {
  'hw-input': getInputCode
};

const getFormCode = (data) => {
  let code = ``;
  let variablesCode = ``;
  let functionCode = ``;
  let isForm = data.isForm || false;
  let formData = data.elementData || {};
  let tier = data.tier || [];
  let parentData = data.formData || {};
  addVariables(tier, formData.options.key || ('form-' + formData.options.uuid), {});
  if (isForm) {
    // code += `<el-form-item label="${formData.options.name || ''}" required="${formData.options.required}">`;
    addCode(`<el-form-item label="${formData.options.name || ''}" required="${formData.options.required}">`);
  } else {
    // code += `<div> `;
    addCode(`<div> `);
  }
  tier.push(formData.options.key || ('form-' + formData.options.uuid));
  // code += `<el-form :model="pageForm['${formData.options.key || ('form-' + formData.options.uuid)}']" label-width="${formData.options.labelWidth}" >`;
  addCode(`<el-form :model="pageForm['${formData.options.key || ('form-' + formData.options.uuid)}']" label-width="${formData.options.labelWidth}" >`);

  formData.tasks.forEach(task => {
    if (task.type === 'hw-form') {
      getFormCode({
        isForm: true, formData: formData, elementData: task, tier
      });
    } else {
      const noFun = () => {
      };
      (elementEnum[task.type] || noFun)({
        isForm: true, formData: formData, elementData: task, tier
      });
    }
  });

  // code += `</el-form>`;
  addCode(` <el-form-item>
      <el-button type="primary" >${formData.options.submitBottomName}</el-button>
      <el-button>${formData.options.resetBottomName}</el-button>
    </el-form-item>`);
  addCode(`</el-form>`);
  if (isForm) {
    // code += `</el-form-item>`;
    addCode(`</el-form-item>`);
  } else {
    // code += `</div>`;
    addCode(`</div>`);
  }
  addCode(code);

};

export {
  getFormCode
};
