<commit_before><link rel="import" href="../../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../../../bower_components/etools-ajax/etools-ajax.html">
<link rel="import" href="../../../../bower_components/paper-dialog/paper-dialog.html">
<link rel="import" href="../../../../bower_components/iron-flex-layout/iron-flex-layout-classes.html">

<link rel="import" href="../../../../styles/shared-styles.html">
<link rel="import" href="../../../../styles/et2f-styles.html">

<dom-module id="et2f-outbound-data">

    <style include="shared-styles et2f-styles iron-flex" is="custom-style">

        .dialog-heading {
            font-size: 20px;
            color: var(gray-dark);
            font-weight: 500;
        }

        .dialog-intro-text {
            color: var(--gray-mid);
            font-size: 16px;
            margin-bottom: 24px;
        }

        .row-container {
            width: 250px;
        }

        .small-column-heading {
            font-size: 12px;
            font-weight: 500;
            text-align: center;
        }

        .dialog-confirm-button {
            color: var(--et2f-primary);
            font-weight: 500;
            height: 40px;
            float: right;
        }

        paper-radio-button {
            text-align: center;
            margin-left: 10px;
        }

    </style>

    <template>

        <!-- Crearances dialog -->
        <!-- we're working with the outbound data here, so no older value gets shown for now, although it gets stored -->
        <paper-dialog id="clearance-modal" opened="{{clearancesOpened}}" with-backdrop>

            <div class="dialog-heading">Travel authorization</div>
            <div class="dialog-intro-text">Before you submit this trip for approval, please answer the questions below.</div>

            <div class="layout vertical">

                <div class="layout horizontal center">
                    <div class="row-container"></div>
                    <div class="flex small-column-heading">Yes</div>
                    <div class="flex small-column-heading">No</div>
                    <div class="flex small-column-heading">Not Applicable</div>
                </div>

                <div class="layout horizontal center">
                    <div class="row-container" id="label-sec-clear">Did you request Security Clearance?</div>
                    <paper-radio-group class="flex layout horizontal" selected="{{serverData.clearances.security_clearance}}">
                        <paper-radio-button class="flex" name="requested"></paper-radio-button>
                        <paper-radio-button class="flex" name="not_requested"></paper-radio-button>
                        <paper-radio-button class="flex" name="not_applicable"></paper-radio-button>
                    </paper-radio-group>
                </div>

                <div class="layout horizontal center">
                    <div class="row-container" id="label-med">Did you request Medical Clearance?</div>
                    <paper-radio-group class="flex layout horizontal" selected="{{serverData.clearances.medical_clearance}}">
                        <paper-radio-button class="flex" name="requested"></paper-radio-button>
                        <paper-radio-button class="flex" name="not_requested"></paper-radio-button>
                        <paper-radio-button class="flex" name="not_applicable"></paper-radio-button>
                    </paper-radio-group>
                </div>

                <div class="layout horizontal center">
                    <div class="row-container" id="label-sec-course">Did you request Security Course?</div>
                    <paper-radio-group class="flex layout horizontal" selected="{{serverData.clearances.security_course}}">
                        <paper-radio-button class="flex" name="requested"></paper-radio-button>
                        <paper-radio-button class="flex" name="not_requested"></paper-radio-button>
                        <paper-radio-button class="flex" name="not_applicable"></paper-radio-button>
                    </paper-radio-group>
                </div>
            </div>

            <paper-button
              class="dialog-confirm-button"
              on-click="_clearancesOnConfirm">
                Submit for approval
            </paper-button>
        </paper-dialog>

        <!-- Rejection dialog -->
        <paper-dialog id="rejection-modal" opened="{{rejectionOpened}}" with-backdrop>

            <div class="dialog-heading">Reject travel</div>
            <div class="dialog-intro-text">Please specify reasons for rejecting this travel:</div>

            <paper-textarea label="Rejection note" value="{{serverData.rejection_note}}" char-counter maxlength="2500"></paper-textarea>

            <paper-button
              class="dialog-confirm-button"
              on-click="_rejectOnConfirm">
                Send rejection
            </paper-button>
        </paper-dialog>

        <!-- Cancellation dialog -->
        <paper-dialog id="cancel-modal" opened="{{cancellationOpened}}" with-backdrop>

            <div class="dialog-heading">Cancel travel</div>
            <div class="dialog-intro-text">Please specify reasons for cancelling this travel:</div>

            <paper-textarea label="Cancellation note" value="{{serverData.cancellation_note}}" char-counter maxlength="2500"></paper-textarea>

            <paper-button
              class="dialog-confirm-button"
              on-click="_cancelOnConfirm">
                Cancel travel
            </paper-button>
        </paper-dialog>

        <!-- General dialog -->
        <paper-dialog id="general-modal" opened="{{generalModal.opened}}" with-backdrop>

            <div class="dialog-heading">[[generalModal.heading]]</div>
            <div class="dialog-intro-text">[[generalModal.text]]</div>

            <paper-button
              class="dialog-confirm-button"
              on-click="generalModalAction">
                [[generalModal.buttonText]]
            </paper-button>
        </paper-dialog>

        <!-- Certification dialog -->
        <paper-dialog id="certification-modal" opened="{{certificationOpened}}" with-backdrop>

            <div class="dialog-heading">Certify travel</div>
            <div class="dialog-intro-text">Please specify the differences between the travel plan and the actual travel:</div>

            <paper-textarea label="Cancellation note" value="{{serverData.certification_note}}" char-counter maxlength="2500"></paper-textarea>

            <paper-button
              class="dialog-confirm-button"
              on-click="_certifyOnConfirm">
                Certify travel
            </paper-button>
        </paper-dialog>

        <etools-loading active="[[loading]]"></etools-loading>


        <template is="dom-if" if="[[postData]]">
            <etools-ajax url="[[postEndpoint]]"
              body="[[serverData]]"
              method="[[method]]"
              on-success="_handlePostSuccess"
              on-fail="_handlePostFail"></etools-ajax>
        </template>

        <template is="dom-if" if="[[attachmentData]]">
            <etools-ajax url="[[attachmentsEndpoint]]"
              body="[[attachment]]"
              method="POST"
              multi-part
              on-success="_handleAttachmentPostSuccess"
              on-fail="_handlePostFail"></etools-ajax>
        </template>

        <template is="dom-if" if="[[deleteAttachmentEndpoint]]">
            <etools-ajax url="[[deleteAttachmentEndpoint]]"
              method="DELETE"
              body="placeholder"
              on-success="_handleAttachmentPostSuccess"
              on-fail="_handlePostFail"></etools-ajax>
        </template>


    </template>

    <script>
      Polymer({
        is: 'et2f-outbound-data',
        properties: {
          data: {
            type: Object,
            notify: true,
          },
          structure: {
            type: Object,
          },
          newTravel: {
            type: Boolean,
            value: false
          },
          travelId: {
            type: String,
            notify: true,
          },
          loading: {
            type: Boolean,
            computed: '_computeLoading(postData, attachmentData)'
          },
          postData: {
            type: Boolean,
            value: false
          },
          attachmentData: {
            type: Boolean,
            value: false
          },
          serverData: {
            type: Object,
            value: function () {
              return {}
            }
          },
          formErrors: {
            type: Object,
            notify: true
          },
          attachmentsEndpoint: {
            type: String,
          },
          deleteAttachmentEndpoint: {
            type: String,
          },
          generalModal: {
            type: Object,
            value: {
              opened: false,
              heading: '',
              text: '',
              buttonText: ''
            }
          },

        },
        generalModalAction: function() {
          console.log('Default fn. ran!');
        },
        _computeLoading: function(post, attachment){
          return post || attachment
        },

        _processOutboundData: function (data) {
          var self = this;
          delete data.status;
          delete data.cost_summary;
          this.serverData = {
            deductions: [],
            itinerary: [],
            activities: [],
            cost_assignments: [],
            expenses: [],
            clearances: {}
          };

          function duplicateIdPurging(collection) {
            if(collection.length < 2) {
              return collection;
            }
            return collection.reduce(function(prev, current) {
              if(prev.some(function(prevItem){return prevItem.id === current.id;})) {
                delete current.id;
              }
              prev.push(current);
              return prev;
            }, [])
          }

          function emptyIdPurging(collection) {
            return collection.map(function(item) {
              if(item && item.id === '') {
                delete item.id
              }
              return item;
            })
          }

          function handleArray(arr) {
            arr = duplicateIdPurging(arr);
            arr = emptyIdPurging(arr);
            arr.forEach(function(obj) {
              handleObject(obj);
            });
          }


          function handleObject(obj) {
            if (!obj) {return;}
            var keys = Object.keys(obj);
            keys.forEach(function(key) {
              if(obj[key] && !(obj[key] instanceof Array )){
                obj[key] = typeof obj[key] === 'object' ? key === 'travel_type' || key === 'mode_of_travel' ? obj[key].label : obj[key].value : obj[key]
              }
              if(obj[key] && obj[key] instanceof Array ){
                obj[key] = obj[key].map(function(item) {
                  if(key === 'mode_of_travel') {
                    return item.label;
                  }
                  return typeof  item === 'object' ? item.value : item
                })
              }
            });
          }

          var dataKeys = Object.keys(data);

          dataKeys.forEach(function(fieldSet){
            if (data[fieldSet] instanceof Array) {
              handleArray(data[fieldSet])
            }
            else if(typeof data[fieldSet] === 'object'){
              handleObject(data[fieldSet]);
            }
          });

          var serverKeys = Object.keys(this.serverData);


          serverKeys.forEach(function(serverKey) {
            if(data[serverKey] instanceof Array && data[serverKey][0]) {
              if(Object.keys(data[serverKey][0]).length === 0) {
                delete self.serverData[serverKey];
              }
            }
            if(self.serverData[serverKey]) {
              self.serverData[serverKey] = data[serverKey];
            }
            delete data[serverKey];
          });

          var leftoverKeys = Object.keys(data);
          leftoverKeys.forEach(function(key) {
            if (data[key]) {
              if(typeof(data[key]) === 'object') {
                var innerKeys = Object.keys(data[key]);
                innerKeys.forEach(function (innerKey) {
                  self.serverData[innerKey] = data[key][innerKey];
                })
              } else {
                self.serverData[key] = data[key];
              }
            }

          });
        },
        _processAttachmentData: function(data) {
          this.attachments = data.attachments.filter(function(item) {
            return !item.id && item.raw;
          });
          if(data.deletedAttachments) {
            this.deletedAttachments = data.deletedAttachments.filter(function (item) {
              return item === parseInt(item, 10);
            });
          } else {
            this.deletedAttachments = [];
          }
          delete data.deletedAttachments;
          delete data.attachments;

        },
        doRequest: function (details) {
          this._processAttachmentData(this.data);
          this._processOutboundData(this.data);
          var self = this;
          var endpointConfig = {
            id: self.travelId ? self.travelId.replace(/\//g, '') : null,
            status: null
          };
          var endpointName = 'et2fStateChange';

          function doSavePost() {
            self.method = 'POST';
            self.postEndpoint = etoolsAppConfig.endpoints.et2fTravelList;
            self.postData = true;
          }

          function doSaveAndSubmit() {
            self.method = 'POST';
            self.postEndpoint = etoolsAppConfig.endpoints.et2fSaveAndSubmit;
            self.postData = true;
          }

          function doSaveUpdate() {
            self.method = 'PATCH';
            self.postEndpoint = etoolsAppConfig.globals.computeTemplate('et2fTravelDetail', endpointConfig).url;
            self.postData = true;
          }

          function doPut() {
            self.method = 'PUT';
            self.postEndpoint = etoolsAppConfig.globals.computeTemplate(endpointName, endpointConfig).url;
            self.postData = true;
          }

          function doPost() {
            self.method = 'POST';
            self.postEndpoint = etoolsAppConfig.globals.computeTemplate(endpointName, endpointConfig).url;
            self.postData = true;
          }

          var statusActions = {

            save: function () {
              if(self.travelId !== undefined) {
                doSaveUpdate();
              } else {
                doSavePost()
              }
            },

            save_and_submit: function() {

              self.set('serverData.clearances', {
                medical_clearance: 'not_applicable',
                security_clearance: 'not_applicable',
                security_course: 'not_applicable'
              });

              self._clearancesOnConfirm = function () {

                self.$['clearance-modal'].close();
                if (endpointConfig.id) {
                  endpointConfig.status = 'submit_for_approval';
                  doPost();
                } else {
                  endpointConfig.status = 'save_and_submit';
                  doSaveAndSubmit();
                }
              };
              self.clearancesOpened = true;
            },

            cancel: function() {

              self._cancelOnConfirm = function() {

                self.$['cancel-modal'].close();
                if(self.travelId !== undefined) {
                  endpointConfig.status = 'cancel';
                  doPost();
                } else {
                  this._handlePostSuccess();
                }
              };

              self.cancellationOpened = true;
            },

            approve: function() {

              self.generalModalAction = function() {
                self.$['general-modal'].close();
                endpointConfig.status = 'approve';
                doPost();
              };

              self.set('generalModal', {
                opened: true,
                heading: 'Approval',
                text: 'Are you sure you want approve this travel?',
                buttonText: 'Approve'
              });
            },

            reject: function() {
              self._rejectOnConfirm = function() {
                self.$['rejection-modal'].close();

                endpointConfig.status = 'reject';
                doPost();
              }
              self.rejectionOpened = true;
            },

            // TODO: Reopen popup
            reopen: function() {

              self.generalModalAction = function() {
                self.$['general-modal'].close();
                endpointConfig.status = 'restore';
                doPost();
              };

              self.set('generalModal', {
                opened: true,
                heading: 'Reopen',
                text: 'Are you sure you want reopen this travel?',
                buttonText: 'Reopen'
              });

            },

            send_for_payment: function() {

              self.generalModalAction = function() {
                self.$['general-modal'].close();
                endpointConfig.status = 'send_for_payment';
                doPost();
              };

              self.set('generalModal', {
                opened: true,
                heading: 'Send for payment',
                text: 'Are you sure you want to send this travel to payment?',
                buttonText: 'Reopen'
              });

            },

            went_as_planned: function() {

              self.generalModalAction = function() {
                self.$['general-modal'].close();
                endpointConfig.status = 'mark_as_certified';
                doPost();
              };

              self.set('generalModal', {
                opened: true,
                heading: 'Went as planned',
                text: 'Are you sure this travel went as planned?',
                buttonText: 'Yes'
              });

            },

            submit_certificate: function() {

              self._certifyOnConfirm = function() {

                self.$['certification-modal'].close();
                endpointConfig.status = 'submit_certificate';<commit_msg>Send for payment modal text button fixed<commit_after><link rel="import" href="../../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../../../bower_components/etools-ajax/etools-ajax.html">
<link rel="import" href="../../../../bower_components/paper-dialog/paper-dialog.html">
<link rel="import" href="../../../../bower_components/iron-flex-layout/iron-flex-layout-classes.html">

<link rel="import" href="../../../../styles/shared-styles.html">
<link rel="import" href="../../../../styles/et2f-styles.html">

<dom-module id="et2f-outbound-data">

    <style include="shared-styles et2f-styles iron-flex" is="custom-style">

        .dialog-heading {
            font-size: 20px;
            color: var(gray-dark);
            font-weight: 500;
        }

        .dialog-intro-text {
            color: var(--gray-mid);
            font-size: 16px;
            margin-bottom: 24px;
        }

        .row-container {
            width: 250px;
        }

        .small-column-heading {
            font-size: 12px;
            font-weight: 500;
            text-align: center;
        }

        .dialog-confirm-button {
            color: var(--et2f-primary);
            font-weight: 500;
            height: 40px;
            float: right;
        }

        paper-radio-button {
            text-align: center;
            margin-left: 10px;
        }

    </style>

    <template>

        <!-- Crearances dialog -->
        <!-- we're working with the outbound data here, so no older value gets shown for now, although it gets stored -->
        <paper-dialog id="clearance-modal" opened="{{clearancesOpened}}" with-backdrop>

            <div class="dialog-heading">Travel authorization</div>
            <div class="dialog-intro-text">Before you submit this trip for approval, please answer the questions below.</div>

            <div class="layout vertical">

                <div class="layout horizontal center">
                    <div class="row-container"></div>
                    <div class="flex small-column-heading">Yes</div>
                    <div class="flex small-column-heading">No</div>
                    <div class="flex small-column-heading">Not Applicable</div>
                </div>

                <div class="layout horizontal center">
                    <div class="row-container" id="label-sec-clear">Did you request Security Clearance?</div>
                    <paper-radio-group class="flex layout horizontal" selected="{{serverData.clearances.security_clearance}}">
                        <paper-radio-button class="flex" name="requested"></paper-radio-button>
                        <paper-radio-button class="flex" name="not_requested"></paper-radio-button>
                        <paper-radio-button class="flex" name="not_applicable"></paper-radio-button>
                    </paper-radio-group>
                </div>

                <div class="layout horizontal center">
                    <div class="row-container" id="label-med">Did you request Medical Clearance?</div>
                    <paper-radio-group class="flex layout horizontal" selected="{{serverData.clearances.medical_clearance}}">
                        <paper-radio-button class="flex" name="requested"></paper-radio-button>
                        <paper-radio-button class="flex" name="not_requested"></paper-radio-button>
                        <paper-radio-button class="flex" name="not_applicable"></paper-radio-button>
                    </paper-radio-group>
                </div>

                <div class="layout horizontal center">
                    <div class="row-container" id="label-sec-course">Did you request Security Course?</div>
                    <paper-radio-group class="flex layout horizontal" selected="{{serverData.clearances.security_course}}">
                        <paper-radio-button class="flex" name="requested"></paper-radio-button>
                        <paper-radio-button class="flex" name="not_requested"></paper-radio-button>
                        <paper-radio-button class="flex" name="not_applicable"></paper-radio-button>
                    </paper-radio-group>
                </div>
            </div>

            <paper-button
              class="dialog-confirm-button"
              on-click="_clearancesOnConfirm">
                Submit for approval
            </paper-button>
        </paper-dialog>

        <!-- Rejection dialog -->
        <paper-dialog id="rejection-modal" opened="{{rejectionOpened}}" with-backdrop>

            <div class="dialog-heading">Reject travel</div>
            <div class="dialog-intro-text">Please specify reasons for rejecting this travel:</div>

            <paper-textarea label="Rejection note" value="{{serverData.rejection_note}}" char-counter maxlength="2500"></paper-textarea>

            <paper-button
              class="dialog-confirm-button"
              on-click="_rejectOnConfirm">
                Send rejection
            </paper-button>
        </paper-dialog>

        <!-- Cancellation dialog -->
        <paper-dialog id="cancel-modal" opened="{{cancellationOpened}}" with-backdrop>

            <div class="dialog-heading">Cancel travel</div>
            <div class="dialog-intro-text">Please specify reasons for cancelling this travel:</div>

            <paper-textarea label="Cancellation note" value="{{serverData.cancellation_note}}" char-counter maxlength="2500"></paper-textarea>

            <paper-button
              class="dialog-confirm-button"
              on-click="_cancelOnConfirm">
                Cancel travel
            </paper-button>
        </paper-dialog>

        <!-- General dialog -->
        <paper-dialog id="general-modal" opened="{{generalModal.opened}}" with-backdrop>

            <div class="dialog-heading">[[generalModal.heading]]</div>
            <div class="dialog-intro-text">[[generalModal.text]]</div>

            <paper-button
              class="dialog-confirm-button"
              on-click="generalModalAction">
                [[generalModal.buttonText]]
            </paper-button>
        </paper-dialog>

        <!-- Certification dialog -->
        <paper-dialog id="certification-modal" opened="{{certificationOpened}}" with-backdrop>

            <div class="dialog-heading">Certify travel</div>
            <div class="dialog-intro-text">Please specify the differences between the travel plan and the actual travel:</div>

            <paper-textarea label="Cancellation note" value="{{serverData.certification_note}}" char-counter maxlength="2500"></paper-textarea>

            <paper-button
              class="dialog-confirm-button"
              on-click="_certifyOnConfirm">
                Certify travel
            </paper-button>
        </paper-dialog>

        <etools-loading active="[[loading]]"></etools-loading>


        <template is="dom-if" if="[[postData]]">
            <etools-ajax url="[[postEndpoint]]"
              body="[[serverData]]"
              method="[[method]]"
              on-success="_handlePostSuccess"
              on-fail="_handlePostFail"></etools-ajax>
        </template>

        <template is="dom-if" if="[[attachmentData]]">
            <etools-ajax url="[[attachmentsEndpoint]]"
              body="[[attachment]]"
              method="POST"
              multi-part
              on-success="_handleAttachmentPostSuccess"
              on-fail="_handlePostFail"></etools-ajax>
        </template>

        <template is="dom-if" if="[[deleteAttachmentEndpoint]]">
            <etools-ajax url="[[deleteAttachmentEndpoint]]"
              method="DELETE"
              body="placeholder"
              on-success="_handleAttachmentPostSuccess"
              on-fail="_handlePostFail"></etools-ajax>
        </template>


    </template>

    <script>
      Polymer({
        is: 'et2f-outbound-data',
        properties: {
          data: {
            type: Object,
            notify: true,
          },
          structure: {
            type: Object,
          },
          newTravel: {
            type: Boolean,
            value: false
          },
          travelId: {
            type: String,
            notify: true,
          },
          loading: {
            type: Boolean,
            computed: '_computeLoading(postData, attachmentData)'
          },
          postData: {
            type: Boolean,
            value: false
          },
          attachmentData: {
            type: Boolean,
            value: false
          },
          serverData: {
            type: Object,
            value: function () {
              return {}
            }
          },
          formErrors: {
            type: Object,
            notify: true
          },
          attachmentsEndpoint: {
            type: String,
          },
          deleteAttachmentEndpoint: {
            type: String,
          },
          generalModal: {
            type: Object,
            value: {
              opened: false,
              heading: '',
              text: '',
              buttonText: ''
            }
          },

        },
        generalModalAction: function() {
          console.log('Default fn. ran!');
        },
        _computeLoading: function(post, attachment){
          return post || attachment
        },

        _processOutboundData: function (data) {
          var self = this;
          delete data.status;
          delete data.cost_summary;
          this.serverData = {
            deductions: [],
            itinerary: [],
            activities: [],
            cost_assignments: [],
            expenses: [],
            clearances: {}
          };

          function duplicateIdPurging(collection) {
            if(collection.length < 2) {
              return collection;
            }
            return collection.reduce(function(prev, current) {
              if(prev.some(function(prevItem){return prevItem.id === current.id;})) {
                delete current.id;
              }
              prev.push(current);
              return prev;
            }, [])
          }

          function emptyIdPurging(collection) {
            return collection.map(function(item) {
              if(item && item.id === '') {
                delete item.id
              }
              return item;
            })
          }

          function handleArray(arr) {
            arr = duplicateIdPurging(arr);
            arr = emptyIdPurging(arr);
            arr.forEach(function(obj) {
              handleObject(obj);
            });
          }


          function handleObject(obj) {
            if (!obj) {return;}
            var keys = Object.keys(obj);
            keys.forEach(function(key) {
              if(obj[key] && !(obj[key] instanceof Array )){
                obj[key] = typeof obj[key] === 'object' ? key === 'travel_type' || key === 'mode_of_travel' ? obj[key].label : obj[key].value : obj[key]
              }
              if(obj[key] && obj[key] instanceof Array ){
                obj[key] = obj[key].map(function(item) {
                  if(key === 'mode_of_travel') {
                    return item.label;
                  }
                  return typeof  item === 'object' ? item.value : item
                })
              }
            });
          }

          var dataKeys = Object.keys(data);

          dataKeys.forEach(function(fieldSet){
            if (data[fieldSet] instanceof Array) {
              handleArray(data[fieldSet])
            }
            else if(typeof data[fieldSet] === 'object'){
              handleObject(data[fieldSet]);
            }
          });

          var serverKeys = Object.keys(this.serverData);


          serverKeys.forEach(function(serverKey) {
            if(data[serverKey] instanceof Array && data[serverKey][0]) {
              if(Object.keys(data[serverKey][0]).length === 0) {
                delete self.serverData[serverKey];
              }
            }
            if(self.serverData[serverKey]) {
              self.serverData[serverKey] = data[serverKey];
            }
            delete data[serverKey];
          });

          var leftoverKeys = Object.keys(data);
          leftoverKeys.forEach(function(key) {
            if (data[key]) {
              if(typeof(data[key]) === 'object') {
                var innerKeys = Object.keys(data[key]);
                innerKeys.forEach(function (innerKey) {
                  self.serverData[innerKey] = data[key][innerKey];
                })
              } else {
                self.serverData[key] = data[key];
              }
            }

          });
        },
        _processAttachmentData: function(data) {
          this.attachments = data.attachments.filter(function(item) {
            return !item.id && item.raw;
          });
          if(data.deletedAttachments) {
            this.deletedAttachments = data.deletedAttachments.filter(function (item) {
              return item === parseInt(item, 10);
            });
          } else {
            this.deletedAttachments = [];
          }
          delete data.deletedAttachments;
          delete data.attachments;

        },
        doRequest: function (details) {
          this._processAttachmentData(this.data);
          this._processOutboundData(this.data);
          var self = this;
          var endpointConfig = {
            id: self.travelId ? self.travelId.replace(/\//g, '') : null,
            status: null
          };
          var endpointName = 'et2fStateChange';

          function doSavePost() {
            self.method = 'POST';
            self.postEndpoint = etoolsAppConfig.endpoints.et2fTravelList;
            self.postData = true;
          }

          function doSaveAndSubmit() {
            self.method = 'POST';
            self.postEndpoint = etoolsAppConfig.endpoints.et2fSaveAndSubmit;
            self.postData = true;
          }

          function doSaveUpdate() {
            self.method = 'PATCH';
            self.postEndpoint = etoolsAppConfig.globals.computeTemplate('et2fTravelDetail', endpointConfig).url;
            self.postData = true;
          }

          function doPut() {
            self.method = 'PUT';
            self.postEndpoint = etoolsAppConfig.globals.computeTemplate(endpointName, endpointConfig).url;
            self.postData = true;
          }

          function doPost() {
            self.method = 'POST';
            self.postEndpoint = etoolsAppConfig.globals.computeTemplate(endpointName, endpointConfig).url;
            self.postData = true;
          }

          var statusActions = {

            save: function () {
              if(self.travelId !== undefined) {
                doSaveUpdate();
              } else {
                doSavePost()
              }
            },

            save_and_submit: function() {

              self.set('serverData.clearances', {
                medical_clearance: 'not_applicable',
                security_clearance: 'not_applicable',
                security_course: 'not_applicable'
              });

              self._clearancesOnConfirm = function () {

                self.$['clearance-modal'].close();
                if (endpointConfig.id) {
                  endpointConfig.status = 'submit_for_approval';
                  doPost();
                } else {
                  endpointConfig.status = 'save_and_submit';
                  doSaveAndSubmit();
                }
              };
              self.clearancesOpened = true;
            },

            cancel: function() {

              self._cancelOnConfirm = function() {

                self.$['cancel-modal'].close();
                if(self.travelId !== undefined) {
                  endpointConfig.status = 'cancel';
                  doPost();
                } else {
                  this._handlePostSuccess();
                }
              };

              self.cancellationOpened = true;
            },

            approve: function() {

              self.generalModalAction = function() {
                self.$['general-modal'].close();
                endpointConfig.status = 'approve';
                doPost();
              };

              self.set('generalModal', {
                opened: true,
                heading: 'Approval',
                text: 'Are you sure you want approve this travel?',
                buttonText: 'Approve'
              });
            },

            reject: function() {
              self._rejectOnConfirm = function() {
                self.$['rejection-modal'].close();

                endpointConfig.status = 'reject';
                doPost();
              }
              self.rejectionOpened = true;
            },

            reopen: function() {

              self.generalModalAction = function() {
                self.$['general-modal'].close();
                endpointConfig.status = 'restore';
                doPost();
              };

              self.set('generalModal', {
                opened: true,
                heading: 'Reopen',
                text: 'Are you sure you want reopen this travel?',
                buttonText: 'Reopen'
              });

            },

            send_for_payment: function() {

              self.generalModalAction = function() {
                self.$['general-modal'].close();
                endpointConfig.status = 'send_for_payment';
                doPost();
              };

              self.set('generalModal', {
                opened: true,
                heading: 'Send for payment',
                text: 'Are you sure you want to send this travel to payment?',
                buttonText: 'Send for payment'
              });

            },

            went_as_planned: function() {

              self.generalModalAction = function() {
                self.$['general-modal'].close();
                endpointConfig.status = 'mark_as_certified';
                doPost();
              };

              self.set('generalModal', {
                opened: true,
                heading: 'Went as planned',
                text: 'Are you sure this travel went as planned?',
                buttonText: 'Yes'
              });

            },

            submit_certificate: function() {

              self._certifyOnConfirm = function() {

                self.$['certification-modal'].close();
                endpointConfig.status = 'submit_certificate';
{'fraction_non_alphanumeric': 0.12553807834962233, 'fraction_numerical': 0.002761458699948561, 'mean_word_length': 1.4825593117817057, 'pattern_counts': {'":': 0, '<': 257, '<?xml version=': 0, '>': 255, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1566134', 'n_tokens_mistral': 9505, 'n_tokens_neox': 8951, 'n_words': 1664}