text: $(document).ready(function(){
  	config.elem = {
  		form: '#form-lamaran',
  		table: '#table-lamaran',
  		modal: '#modal-lamaran',
  	};
  	config.action = 'add';
  	_pages.init();
  })
  const _pages = {
  	validation: () => {
  		let settingValidation = {};
  		settingValidation.submitHandler = function (form) {
  			let confirmation = gen.func.my.confirmation();
  			confirmation.then(function (result) {
  				if (result.value) {
  					_logic.submit();
  				}
  			});
  		};
  		$(config.elem.form).validate(settingValidation);
  	},
  	modal: {
  		init: (modal, mode) => {
  			$(config.elem.form).trigger("reset");
  			switch (modal) {
  				case '#modal-lamaran':
  					switch (mode) {
  						case 'add':
  							$(config.elem.modal).modal('show')
  							config.action = 'add';
  							break;
  						case 'edit':
  							$(config.elem.modal).modal('show')
  							config.action = 'edit';
  							//setpage
  							$('input[name=nama]').attr('readonly',false);
  							$('input[name=jabatan_dilamar]').attr('readonly',false);
  							$('input[name=email]').attr('readonly',false);
  							$('input[name=no_hp]').attr('readonly',false);
  							$('textarea[name=alamat]').attr('readonly',false);
  							$('input[name=cv]').attr('readonly',false);
  							$('input[id=cv_view]').hide();
  							$('input[id=cv_edit]').show();
  							$('input[id=cv_edit]').attr('required',false);
  							$('select[name=status]').attr('disabled',false);
  							$('button[type=submit]').show();
  							break;
  						case 'details':
  							$(config.elem.modal).modal('show')
  							config.action = 'details';
  							//setpage
  							$('input[name=nama]').attr('readonly',true);
  							$('input[name=jabatan_dilamar]').attr('readonly',true);
  							$('input[name=email]').attr('readonly',true);
  							$('input[name=no_hp]').attr('readonly',true);
  							$('textarea[name=alamat]').attr('readonly',true);
  							$('input[name=cv]').attr('readonly',true);
  							$('input[id=cv_view]').show();
  							$('input[id=cv_edit]').hide();
  							$('input[id=cv_edit]').attr('required',false);
  							$('select[name=status]').attr('disabled',true);
  							$('button[type=submit]').hide();
  							break;
  						default:
  							gen.func.error.message('ERROR: ACTION IS NOT FOUND');
  							break;
  					}
  					break;
  			
  				default:
  					gen.func.error.message('ERROR: MODAL IS NOT FOUND');
  					break;
  			}
  		},
  		fill: (data) => {
  			$('input[name=pelamar_id]').val(data.pelamar_id);
  			$('input[name=nama]').val(data.nama);
  			$('input[name=jabatan_dilamar]').val(data.jabatan_dilamar);
  			$('input[name=email]').val(data.email);
  			$('input[name=no_hp]').val(data.no_hp);
  			$('textarea[name=alamat]').val(data.alamat);
  			$('select[name=status]').val(data.status);
  			$('input[name=cv]').val(data.cv);
  		}
  	},
  	init: () => {
  		_pages.validation();
  		_component.init();
  	}
  }
  
  const _logic = {
  	app: (element) =>{
  		switch ($(element).attr('data-action')) {
  			case 'add':
  				_pages.modal.init(config.elem.modal,$(element).attr('data-action'));
  				break;
  			case 'edit':
  				document.getElementById(`${$(element).attr('data-form')}`).reset();
  				_pages.modal.init(config.elem.modal,$(element).attr('data-action'));
  				_logic.process.getLamaranDetails($(element).attr('data-id'));
  			
  				break;
  			case 'details':
  				document.getElementById(`${$(element).attr('data-form')}`).reset();
  				_pages.modal.init(config.elem.modal,$(element).attr('data-action'));
  				_logic.process.getLamaranDetails($(element).attr('data-id'));
  			
  				break;
  			default:
  				gen.func.error.message('ERROR: ACTION NOT DECLARED');
  				break;
  		}
  	},
  	process: {
  		insert: () => {
  			// let data = $(config.elem.form).serialize();
  			let data = new FormData($(config.elem.form)[0]);
  			let promise = gen.func.request.multipart(config.url.insert, data, config.elem.form);
  			promise.then((response) => {
  				setTimeout(() => {
  					gen.func.my.load('end',config.elem.form,);
  				}, 500);
  				if(response.response == 'success'){
  					gen.func.my.alert(response.response, response.message, response.url);
  				}else{
  					if(response.flag == 'validation'){
  						gen.func.my.notification({
  							title: 'Please check your field',
  							message: response.validation,
  							type: response.response
  						})
  					}else{
  						gen.func.my.alert(response.response, response.message);
  					}
  				}
  			}).fail((response) => {
  				setTimeout(() => {
  					gen.func.my.load('end',config.elem.form);
  				}, 500);
  				gen.func.error.message(response);
  			});
  		},
  		update: (id) => {
  			config.url.update = config.url.update.replace('valueRoutes',id);
  			let data = new FormData($(config.elem.form)[0]);
  			// let data = $(config.elem.form).serialize();
  			let promise = gen.func.request.multipart(config.url.update, data, config.elem.form);
  			promise.then((response) => {
  				setTimeout(() => {
  					gen.func.my.load('end',config.elem.form);
  				}, 500);
  				if(response.response == 'success'){
  					gen.func.my.alert(response.response, response.message);
  					$(config.elem.modal).modal('hide');
  					config.datatables.fnDraw();
  				}else{
  					if(response.flag == 'validation'){
  						gen.func.my.notification({
  							title: 'Please check your field',
  							message: response.validation,
  							type: response.response
  						})
  					}else{
  						gen.func.my.alert(response.response, response.message);
  					}
  				}
  			}).fail((response) => {
  				setTimeout(() => {
  					gen.func.my.load('end',config.elem.form);
  				}, 500);
  				gen.func.error.message(response);
  			});
  		},
  		getLamaranDetails: (id) => {
  			debugger
  			let url = (config.url.getDetails).replace('valueRoutes', id);
  			gen.func.request.get(url, null, config.elem.form).done((response) => {
  				setTimeout(() => {
  					gen.func.my.load('end',config.elem.form);
  				}, 500);
  				_pages.modal.fill(response);
  			}).fail((response) => {
  				setTimeout(() => {
  					gen.func.my.load('end',config.elem.form);
  				}, 500);
  				gen.func.error.message(response);
  			});
  		}
  	},
  	submit: () => {
  		switch(config.action){
  			case 'add':
  				_logic.process.insert();
  				break;
  			case 'edit':
  				let id = $('input[name=pelamar_id]').val();
  				_logic.process.update(id);
  				break;
  			default:
  				gen.func.error.message('ERROR: ACTION NOT DECLARED');
  				break;
  		}
  	}
  }
  const _component = {
  	daterange: () => {
  		$('.daterange-basic').daterangepicker({
              applyClass: 'bg-slate-600',
  			cancelClass: 'btn-light',
  			autoUpdateInput: false
  		},function(start_date, end_date) {
  			$('input[name=date-filter]').val(start_date.format('YYYY/MM/DD')+' - '+end_date.format('YYYY/MM/DD')).trigger('change');
  		});
  	},
  	init: () => {
  		_component.daterange();
  	}
  }
  const _ddl = {
  	status: (value) => {
  		gen.func.request.get(config.url.getStatus, null, config.elem.ddl_status).then(function(response) {
  			$('select[name=Status] option').remove();
  			let content = '';
  			$.each(response, function (key, result) {
  				if(value == result.StatusID){
  					content += "<option selected value='" + result.StatusID + "'>" + result.Status +
  					"</option>";
  				}else{
  					content += "<option value='" + result.StatusID + "'>" + result.Status +
  					"</option>";
  				}
  			});
  			$('select[name=Status]').append(content);
  			setTimeout(() => {
  				gen.func.my.load('end',config.elem.ddl_status);
  			}, 500);
  		}).fail(function(response){
  			setTimeout(() => {
  				gen.func.my.load('end',config.elem.ddl_status);
  			}, 500);
  			gen.func.error.message(response);
  		});
  	} 
  }
  const _event = function(){
  	$(document).on('click','.btn-action', function(){
  		_logic.app(this);
  	});
  	$(document).on('change','.search', function(){
  		if($(config.datatables)){
  			config.datatables.fnDraw();
  		}
  	});
  }();
  