<!DOCTYPE html>
<html>

  <head>
    <!-- Twitter bootstrap -->
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet">

    <!-- apiCheck is used by formly to validate its api -->
    <script src="//rawgit.com/kentcdodds/api-check/master/dist/api-check.js"></script>
    <!-- This is the latest version of angular (at the time this template was created) -->
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.js"></script>
    
    <!-- This is the current state of master for formly core. -->
    <script src="//rawgit.com/formly-js/angular-formly/master/dist/formly.js"></script>
    <!-- This is the current state of master for the bootstrap templates -->
    <script src="//rawgit.com/formly-js/angular-formly-templates-bootstrap/master/dist/angular-formly-templates-bootstrap.js"></script>

    <!-- additional dependencies go here -->
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
    <title>Angular Formly Question</title>
  </head>

  <body ng-app="formlyQuestion" ng-controller="MainCtrl as vm">
    <div>
      <h1>angular-formly question</h1>
      <small>
        angular: {{vm.env.angularVersion}} |
        angular-formly: {{vm.env.formlyVersion}}
      </small>
      <div>
        <h2>Getting Help Steps</h2>
        <ol>
          <li>
            Reproduce your question in this jsbin.
            <small>
              Protip: Please format your code.
              In jsbin, if you select all the text,
              then hit <code>shift + tab</code>,
              it will format all the code for you
            </small>
          </li>
          <li>
            Update the steps to reproduce with steps the user takes
            or just a sentence or two describing what you're trying
            to accomplish
          </li>
          <li>
            Copy the link to it without the number.
            For example: <strong>jsbin.com/abcde/3/edit</strong> -> <strong>jsbin.com/abcde/edit</strong>
            which allows you to make updates to the jsbin without updating your links
          </li>
          <li>
            Paste the link in a (well done) question on
            <a href="http://stackoverflow.com/questions/ask?tags=angular-formly">StackOverflow</a>
          </li>
          <li>
            Paste a link to the StackOverflow question into
            <a href="http://chat.angular-formly.com">chat.angular-formly.com</a>
            and ask politely for help.
          </li>
          <li>
            Remove these steps from your question.
          </li>
          <li>Wait patiently while people help you for free :-)</li>
        </ol>
      </div>
      <hr />
      <div>
        <h2>Steps to Reproduce</h2>
        <ol>
          <li>
            Step 1
          </li>
          <li>
            Step 2
          </li>
          <li>
            Step 3
          </li>
        </ol>
      </div>

      <hr />

      <div>
        <h2>Form</h2>
        <form ng-submit="vm.onSubmit()" name="vm.form" novalidate>
          <formly-form model="vm.model" fields="vm.fields" options="vm.options" form="vm.form">
            <button type="submit" class="btn btn-primary submit-button" ng-disabled="vm.form.$invalid">Submit</button>
            <button type="button" class="btn btn-default" ng-click="vm.options.resetModel()">Reset</button>
          </formly-form>
        </form>
      </div>

      <hr />

      <div>
        <h2>Model Value</h2>
        <pre>{{vm.model | json}}</pre>
      </div>
      
      <hr />
      
      <div>
        <h2>Form</h2>
        <pre>{{vm.form | json}}</pre>
      </div>
      
      <hr />

      <div>
        <h2>Fields <small>(note, functions are not shown)</small></h2>
        <pre>{{vm.originalFields | json}}</pre>
      </div>
      
      <hr />
      
      <div>
        <h2>Form State</h2>
        <pre>{{vm.options.formState | json}}</pre>
      </div>
    </div>

    <!-- Put custom templates here -->

  </body>

</html>
// Code goes here

/* global angular */
(function() {

  'use strict';

  console.clear(); // <-- keep the console clean in jsbin

  var app = angular.module('formlyQuestion', [
    // additional dependencies go here
    'formly',
    'formlyBootstrap'
  ]);

  app.run(function(formlyConfig) {
    // set types here
    formlyConfig.setType({
    name: 'tableItems',
    template: '<table><formly-form ng-repeat="element in model[options.key]" ng-init="fields = copyFields(to.fields)" fields="to.fields" model="element" bind-name="\'formly_ng_repeat\' + index + $parent.$index" root-el="tr" field-root-el="td">'+
              '</formly-form></table>'
    });
    
    
    formlyConfig.setType({
      name: 'inputCell',
      template: '<td><input ng-model="model[options.key]" /></td>'
    });
    
    formlyConfig.setWrapper({
      name: 'customValidation',
      template: '<td><formly-transclude></formly-transclude></td>'
    });
  });

  app.controller('MainCtrl', function MainCtrl(formlyVersion) {
    var vm = this;
    // funcation assignment
    vm.onSubmit = onSubmit;

    // variable assignment
    vm.env = getEnv();
    vm.model = {
      items: [
        {
          "code": "1-1",
          "description": "item 1-1",
          "id": "9545b1976a024718b4e58f84a105e675",
          "invoice_id": "2acbc4f89e8749c489b81b7846333487",
          "item_id": null,
          "order": null,
          "quantity": null,
          "total": null,
          "unit_price": null
        },
        {
          "code": "1-2",
          "description": "item 1-2",
          "id": "9545b1976a024718b4e58f84a105e672",
          "invoice_id": "2acbc4f89e8749c489b81b7846333487",
          "item_id": null,
          "order": null,
          "quantity": null,
          "total": null,
          "unit_price": null
        }
      ]
    };
    vm.options = {formState: {}};
    vm.fields = getFields();
    vm.originalFields = angular.copy(vm.fields);

    // function definition
    function getFields() {
      // return your fields here
      return [
        {
          key: 'items',
          type: 'tableItems',
          templateOptions: {
            fields: [
              /*{
                fieldGroup: [*/
                  {
                    type: 'input',
                    key: 'code',
                    templateOptions: {
                      label: 'code',
                      "required": true
                    }
                  },
                  {
                    type: 'input',
                    key: 'description',
                    templateOptions: {
                      label: 'code'
                    }
                  }
                // ]
              //}
            ]
          }
        }
      ];
    }


    function onSubmit() {
      vm.options.updateInitialValue();
      alert(JSON.stringify(vm.model), null, 2);
    }

    function getEnv() {
      return {
        angularVersion: angular.version.full,
        formlyVersion: formlyVersion
      };
    }
  });

})();
/* Styles go here */

body {
  margin: 20px
}

.formly-field {
  margin-bottom: 16px;
}