/**
 * 请求接口格式
 * {
	
 }
 */
layui.define(['jquery', 'form', 'layer'], function (exports) {
	/**
	 *返回当前元素在数组中的下标 
	 * @param {Object} val
	 */
	Array.prototype.indexOf = function (val) {
		for (var i = 0; i < this.length; i++) {
			if (this[i] == val) return i;
		}
		return -1;
	};
	var $ = layui.jquery,
		form = layui.form,
		layer = layui.layer;
	var Class = function (obj) {
		var _this = this;
		_this.elem = obj.elem || 'body';//级联容器
		_this.url = obj.url || {};//级联接口
		_this.formParm = [];//表单参数名
		typeof obj.url == 'object' && ($.each(obj.url, function (key, value) {
			_this.formParm.push(key);
		}));
		_this.box = $('body');//子集容器
		_this.boxs = [];//所有子集
		_this.title = obj.title || null;//标题
		_this.hostParm = {//默认请求参数名
			idName: 'id',
			rowsName: 'rows'
		};
		_this.valObj = {};//初始值对象
		_this[_this.hostParm['rowsName']] = obj[_this.hostParm['rowsName']] || 10000;//请求条数
		for (var i in obj.hostParm) {//自定义请求参数名
			_this.hostParm[i] = obj.hostParm[i];
		}
		_this.data = {};//默认请求参数
		_this.data[_this.hostParm['rowsName']] = _this[_this.hostParm['rowsName']];//赋值参数
		_this.verify = obj.verify || {};

		$.extend(this, obj);
		_this.init();//初始化
	};
	/**
	 * 初始化父下拉列表
	 */
	Class.prototype.init = function () {
		var _this = this;
		_this.box = $('<div class="layui-form-item"></div>');//select 容器
		_this.title && (_this.box.append('<label class="layui-form-label">' + _this.title + '</label>'));//是否显示标题
		//初始化值
		if(JSON.stringify(_this.valObj) != '{}'){
			_this.val();
		}
		_this.element(_this.formParm[0]);
	};
	Class.prototype.element = function (name, code,callback) {
		var _this = this;
		if (!_this.url[name]) {
			// layer.msg("没有配置下级接口",{icon:2,time:600});
			return _this;
		}
		_this.data[_this.hostParm['idName']] = code || null;
		$.ajax({
			url: _this.url[name],
			data: _this.data,
			async: 'false',
			success: function (res) {
				var select_id = "select_" + new Date().getTime();
				_this.boxs[_this.formParm.indexOf(name)] = $(_this.selectHtml(select_id, name, res));
				_this.box.append(_this.boxs[_this.formParm.indexOf(name)]);//往select容器添加包装好的select对象
				$(_this.elem).append(_this.box);//追加到表单容器
				_this.event(name);//绑定事件
				//执行赋值回调函数
				!!callback && callback(name, select_id);
				typeof _this.valObj[name] != 'undefined' && ($("#" + select_id).val(_this.valObj[name]));
				form.render();
			}
		});
	};
	/**
	 * 包装Html
	 */
	Class.prototype.selectHtml = function (select_id, name, res) {
		this.tempSelectId = new Date().getTime();
		var html = '<div class="layui-input-inline"><select lay-search name="' + name + '" lay-verify="' + this.verify[name] + '" lay-filter="' + name + this.tempSelectId + '" id="' +
			select_id + '"><option value="">请选择……</option>';
		for (var xyz in res.data) {
			html += '<option value="' + res.data[xyz].id + '">' + res.data[xyz].name + '</option>';
		}
		return (html + '</select></div>');
	};
	Class.prototype.event = function (name) {
		var _this = this;
		var x = _this.formParm.indexOf(name);
		form.on("select(" + name + this.tempSelectId + ")", function (obj) {
			_this.removeBox(x);
			_this.element(_this.formParm[x - 0 + 1], obj.value);
			_this.click(obj);
			return false;
		});
	};
	Class.prototype.click = function (obj) {

	};
	Class.prototype.removeBox = function (i) {
		var _this = this;
		for (var x = i + 1; x < _this.boxs.length; x++) {
			_this.boxs[x].remove();
		}
	};
	/**
	 * 初始值
	 */
	Class.prototype.val = function (obj) {
		var _this = this;
		$.extend(_this.valObj, obj);
		(function temp(i) {
			if (_this.valObj[_this.formParm[i + 1]]) {
				_this.element(_this.formParm[i - 0 + 1], _this.valObj[_this.formParm[i]], function (name, select_id) {
					if (i == -1) {
						_this.removeBox(i);
					}
					temp(++i);
				});
			}
		})(-1);
		return _this;
	};
	var cascade = {};
	cascade.render = function (e) {
		var i = new Class(e = e || {});
		t = $(e.elem);
		return t[0] ? i : layui.hint().error("layui.cascade 没有找到" + e.elem + "元素");
	};
	exports('cascade', cascade);
});
