/*
*--------------------------------------------------------------------
* jQuery-Plugin "freeesections -config.js-"
* Version: 1.0
* Copyright (c) 2018 TIS
*
* Released under the MIT License.
* http://tis2010.jp/license.txt
* -------------------------------------------------------------------
*/
jQuery.noConflict();
(function($,PLUGIN_ID){
	"use strict";
	var vars={
		fieldinfos:{}
	};
	var functions={
		fieldsort:function(layout){
			var codes=[];
			$.each(layout,function(index,values){
				switch (values.type)
				{
					case 'ROW':
						$.each(values.fields,function(index,values){
							/* exclude spacer */
							if (!values.elementId) codes.push(values.code);
						});
						break;
					case 'GROUP':
						$.merge(codes,functions.fieldsort(values.layout));
						break;
				}
			});
			return codes;
		}
	};
	/*---------------------------------------------------------------
	 initialize fields
	---------------------------------------------------------------*/
	kintone.api(kintone.api.url('/k/v1/app/form/layout',true),'GET',{app:kintone.app.getId()},function(resp){
		var sorted=functions.fieldsort(resp.layout);
		/* get fieldinfo */
		kintone.api(kintone.api.url('/k/v1/app/form/fields',true),'GET',{app:kintone.app.getId()},function(resp){
			var config=kintone.plugin.app.getConfig(PLUGIN_ID);
			vars.fieldinfos=resp.properties;
			$.each(sorted,function(index){
				if (sorted[index] in vars.fieldinfos)
				{
					var fieldinfo=vars.fieldinfos[sorted[index]];
					/* check field type */
					switch (fieldinfo.type)
					{
						case 'NUMBER':
							/* exclude lookup */
							if (!fieldinfo.lookup) $('select#id').append($('<option>').attr('value',fieldinfo.code).text(fieldinfo.label));
							break;
						case 'SINGLE_LINE_TEXT':
							/* exclude lookup */
							if (!fieldinfo.lookup)
							

							break;
					}
				}
			});
			/* initialize valiable */
			if (Object.keys(config).length!==0)
			{
				$('select#id').val(config['id']);
				$('select#name').val(config['name']);
				$('select#shortcut1').val(config['shortcut1']);
				$('select#shortcut2').val(config['shortcut2']);
				$('input#freeeappid').val(config['freeeappid']);
				$('input#freeesecret').val(config['freeesecret']);
			}
		},function(error){});
	},function(error){});
	/*---------------------------------------------------------------
	 button events
	---------------------------------------------------------------*/
	$('button#submit').on('click',function(e){
		var config=[];
		/* check values */
		if (!$('input#freeeappid').val())
		{
			swal('Error!','FreeeAppIDを入力して下さい。','error');
			return;
		}
		if (!$('input#freeesecret').val())
		{
			swal('Error!','FreeeSecretを入力して下さい。','error');
			return;
		}
		if (!$('select#id').val())
		{
			swal('Error!','部門IDフィールドを選択して下さい。','error');
			return;
		}
		if (!$('select#name').val())
		{
			swal('Error!','部門名フィールドを選択して下さい。','error');
			return;
		}
		/* setup config */
		config['id']=$('select#id').val();
		config['name']=$('select#name').val();
		config['shortcut1']=$('select#shortcut1').val();
		config['shortcut2']=$('select#shortcut2').val();
		config['freeeappid']=$('input#freeeappid').val();
		config['freeesecret']=$('input#freeesecret').val();
		/* save config */
		kintone.plugin.app.setConfig(config);
	});
	$('button#cancel').on('click',function(e){
		history.back();
	});
})(jQuery,kintone.$PLUGIN_ID);
{
								$('select#name').append($('<option>').attr('value',fieldinfo.code).text(fieldinfo.label));
								$('select#shortcut1').append($('<option>').attr('value',fieldinfo.code).text(fieldinfo.label));
								$('select#shortcut2').append($('<option>').attr('value',fieldinfo.code).text(fieldinfo.label));
							}