/*
 * Author: 众产®  https://ciy.cn/code
 * Version: 0.1.0
 */
'use strict';
ciyfn.markdown = function (markdown) {
	// #,!,`,@lcr,|表格
	if (markdown.substring(0, 4) == '[MD]')
		markdown = markdown.substring(4).trim();
	var mds = markdown.split('\n');
	var html = '';
	var ispvs = 0; //0文本，1html，2表格
	var isol = false;
	var titnum = [0, 0, 0, 0, 0];
	var tabalign = {};
	for (var m in mds) {
		if (mds[m].length == 0) {
			if (ispvs == 0)
				html += '<br/>';
			continue;
		}
		if (mds[m].length > 0 && mds[m].trim() == '') {
			if (ispvs == 2)
				html += '</table>';
			if (isol) {
				isol = false;
				html += '</ol>';
			}
			ispvs = 0;
			html += '<br/>';
			continue;
		}
		if (mds[m][0] == '#') { //标题1/2/3
			if (ispvs == 2)
				html += '</table>';
			ispvs = 0;
			if (isol) {
				isol = false;
				html += '</ol>';
			}
			var cr = mds[m].substring(1, 2).toLowerCase();
			if (mds[m].substring(0, 5) == '#####') {
				oladd(4, titnum);
				if (mds[m].substring(5, 6) == '.')
					html += '<h5 class="md-h5">' + olnum(4, titnum, '、') + convertcode(mds[m].substring(6)) + '</h5>';
				else if (mds[m].substring(5, 6) == ':')
					html += '<div class="md-content">' + olnum(4, titnum, ' ') + convertcode(mds[m].substring(6)) + '</div>';
				else
					html += '<h5 class="md-h5">' + convertcode(mds[m].substring(5)) + '</h5>';
			} else if (mds[m].substring(0, 4) == '####') {
				oladd(3, titnum);
				if (mds[m].substring(4, 5) == '.')
					html += '<h4 class="md-h4">' + olnum(3, titnum, '、') + convertcode(mds[m].substring(5)) + '</h4>';
				else if (mds[m].substring(4, 5) == ':')
					html += '<div class="md-content">' + olnum(3, titnum, ' ') + convertcode(mds[m].substring(5)) + '</div>';
				else
					html += '<h4 class="md-h4">' + convertcode(mds[m].substring(4)) + '</h4>';
			} else if (mds[m].substring(0, 3) == '###') {
				oladd(2, titnum);
				if (mds[m].substring(3, 4) == '.')
					html += '<h3 class="md-h3">' + olnum(2, titnum, '、') + convertcode(mds[m].substring(4)) + '</h3>';
				else if (mds[m].substring(3, 4) == ':')
					html += '<div class="md-content">' + olnum(2, titnum, ' ') + convertcode(mds[m].substring(4)) + '</div>';
				else
					html += '<h3 class="md-h3">' + convertcode(mds[m].substring(3)) + '</h3>';
			} else if (mds[m].substring(0, 2) == '##') {
				oladd(1, titnum);
				if (mds[m].substring(2, 3) == '.')
					html += '<h2 class="md-h2">' + olnum(1, titnum, '、') + convertcode(mds[m].substring(3)) + '</h2>';
				else if (mds[m].substring(2, 3) == ':')
					html += '<div class="md-content">' + olnum(1, titnum, ' ') + convertcode(mds[m].substring(3)) + '</div>';
				else
					html += '<h2 class="md-h2">' + convertcode(mds[m].substring(2)) + '</h2>';
			} else {
				oladd(0, titnum);
				if (cr == 'c')
					html += '<h1 class="md-h1" style="text-align:center;">' + convertcode(mds[m].substring(2)) + '</h1>';
				else if (cr == 'r')
					html += '<h1 class="md-h1" style="text-align:right;">' + convertcode(mds[m].substring(2)) + '</h1>';
				else if (cr == '.')
					html += '<h1 class="md-h1">' + olnum(0, titnum, '、') + convertcode(mds[m].substring(2)) + '</h1>';
				else if (cr == ':')
					html += '<div class="md-content">' + olnum(0, titnum, ' ') + convertcode(mds[m].substring(2)) + '</div>';
				else
					html += '<h1 class="md-h1">' + convertcode(mds[m].substr(1)) + '</h1>';
			}
		} else if (mds[m][0] == '@') {//c居中，r靠右
			if (ispvs == 2)
				html += '</table>';
			ispvs = 0;
			var cr = mds[m].substring(1, 2).toLowerCase();
			if (cr == 'c')
				html += '<div class="md-content" style="text-align:center;">' + convertcode(mds[m].substring(2)) + '</div>';
			else if (cr == 'r')
				html += '<div class="md-content" style="text-align:right;margin-right:1em;">' + convertcode(mds[m].substring(2)) + '</div>';
			else if (cr == '.') {
				if (!isol) {
					isol = true;
					html += '<ol>';
				}
				html += '<li>' + convertcode(mds[m].substr(2)) + '</li>';
			} else
				html += mds[m].substring(1);

		} else if (mds[m][0] == '!') {//图片
			if (ispvs == 2)
				html += '</table>';
			ispvs = 0;
			var mis = mds[m].split('|');
			var url = ciyfn.file_stor(mis[0].substring(1));
			if (url.substring(url.length - 4) == '.mp4' || url.substring(url.length - 5) == '.m3u8') {
				var alt = '';
				if (mis[1])
					alt = ' alt="' + mis[1].replace('"', "") + '"';
				html += '<div style="font-size:0;text-align: center;"><video src="' + url + '" controls autoplay style="width:100%;display: block;"></video></div>';
			} else if (url.substring(url.length - 4) == '.mp3') {
				var alt = '';
				if (mis[1])
					alt = ' alt="' + mis[1].replace('"', "") + '"';
				html += '<div style="font-size:0;text-align: center;"><audio src="' + url + '" controls style="display: block;margin: auto;"></audio></div>';
			} else {
				var alt = '';
				if (mis[1])
					alt = ' alt="' + mis[1].replace('"', "") + '"';
				html += '<div style="font-size:0;text-align: center;"><img src="' + url + '" style="max-width:100%;"' + alt + '/></div>';
			}
		} else if (mds[m][0] == '|' && mds[m].substring(mds[m].length - 1) == '|') { //表格
			var trs = mds[m].substring(1, mds[m].length - 1).split('|');
			if (ispvs != 2) {
				html += '<table class="md-table"><tr style="font-weight:bold;">';
				for (var i in trs) {
					var align = 'left';
					if (trs[i].substring(trs[i].length - 1) == ':') {
						align = 'right';
						trs[i] = trs[i].substring(0, trs[i].length - 1);
					}
					if (trs[i].substring(0, 1) == ':') {
						align = 'center';
						trs[i] = trs[i].substring(1);
					}
					tabalign[i] = align;
					html += '<td style="text-align:' + align + ';">' + convertcode(trs[i].trim()) + '</td>';
				}
			} else {
				html += '<tr>';
				for (var i in trs) {
					html += '<td style="text-align:' + tabalign[i] + ';">' + convertcode(trs[i].trim()) + '</td>';
				}
			}
			html += '</tr>';
			ispvs = 2;
		} else if (mds[m][0] == '<') { //原始HTML
			if (ispvs == 2)
				html += '</table>';
			ispvs = 1;
			html += mds[m];
		} else {
			if (ispvs == 2)
				html += '</table>';
			ispvs = 0;
			if (isol) {
				isol = false;
				html += '</ol>';
			}
			html += '<div class="md-content">' + convertcode(mds[m]) + '</div>';
		}
	}
	if (ispvs == 2)
		html += '</table>';
	if (isol) {
		isol = false;
		html += '</ol>';
	}
	return html;
	function oladd(idx, olnum) {
		olnum[idx]++;
		for (var i = idx + 1; i < olnum.length; i++)
			olnum[i] = 0;
	}
	function olnum(idx, olnum, end) {
		for (var i = 0; i <= idx; i++) {
			if (olnum[i] == 0)
				continue;
			var ols = [];
			for (var j = i; j <= idx; j++) {
				ols.push(olnum[j]);
			}
			return ols.join('.') + end;
		}
	}
	function convertcode(md) {
		var bcode = false;
		md = md.replace(/  /g, '　');
		while (true) {
			var ind = md.indexOf('`');
			if (ind == -1)
				break;
			var el = '<kbd class="rich-code">';
			if (bcode) {
				bcode = false;
				el = '</kbd>';
			}
			else
				bcode = true;
			md = md.substring(0, ind) + el + md.substring(ind + 1);
		}
		if (bcode)
			md += '</kbd>';
		bcode = false;
		while (true) {
			var ind = md.indexOf('**');
			if (ind == -1)
				break;
			var el = '<b>';
			if (bcode) {
				bcode = false;
				el = '</b>';
			}
			else
				bcode = true;
			md = md.substring(0, ind) + el + md.substring(ind + 2);
		}
		if (bcode)
			md += '</b>';
		while (true) {
			let ind = md.indexOf('[');
			if (ind === -1)
				break;
			let ind2 = md.indexOf(')', ind + 1);
			if (ind2 === -1)
				break;
			let mis = md.substring(ind + 1, ind2).split('](');
			if (mis.length != 2)
				break;
			let url = ciyfn.file_stor(mis[1]);
			let text = mis[0];
			if (text == '')
				text = url;
			var html;
			if (text[0] === '!') {
				text = text.substring(1).trim();
				if (text == '')
					text = url;
				else
					text = `<img src="${text}" style="max-width:100%;"/>`;
			}
			html = `<a href="${url}" target="_blank" nofollow>${text}</a>`;
			md = md.substring(0, ind) + html + md.substring(ind2 + 1);
		}
		return md;
	}
}
ciycmpfunc.ciyselcas = function (opn) {
	var thos = this;
	this.onchange = opn.onchange;
	opn.initevent = opn.initevent || opn.dom.hasattr('initevent');
	opn.hasmore = opn.hasmore || opn.dom.hasattr('hasmore');
	opn.chkuse = opn.chkuse || opn.dom.hasattr('chkuse');
	opn.minsearch = toint(opn.minsearch || opn.dom.attr('minsearch') || 0);
	opn.all = opn.all || opn.dom.attr('all') || '';
	opn.placeholder = ciyfn.lang(opn.placeholder || opn.dom.attr('placeholder') || '请选择');
	opn.rootupid = opn.rootupid || opn.dom.attr('rootupid') || 0;
	var html = '<input type="hidden" name="' + opn.name + '"/>';
	opn.dom.html(html);
	var valuedom = opn.dom.children(0);
	var values;
	this.setvalue = function (newvalue, from) {
		if (this.range == null)
			return console.error('ciy_selcas range null', opn.name);
		if (newvalue !== undefined) {
			this.value = newvalue;
		}
		values = [];
		if (this.value) {
			findvalue(this.value);
			for (var i in thos.range) {
				if (thos.range[i].upid == this.value) {
					values.push(this.value);
					break;
				}
			}
		} else {
			values.push(0);
		}
		if (values.length <= 1) {
			fillselect(opn.rootupid, 0, 0, false);
		} else {
			for (var i = 0; i < values.length - 1; i++) {
				fillselect(values[i], i, values[i + 1], false);
			}
		}
		valuedom.val(this.value);
		if (from && typeof (this.onchange) == 'function')
			this.onchange({ name: opn.name, dom: opn.dom, from: from, value: this.value });
	}
	this.setrange = function (newrange) {
		opn.range = ciyfn.getdictdata(newrange);
		if (opn.range == null)
			return console.error('ciy_selcas range null', opn.name);
		this.range = [];
		for (var i in opn.range) {
			if (opn.chkuse && opn.range[i].isuse == 2)
				continue;
			if (!opn.range[i].upid)
				opn.range[i].upid = 0;
			this.range.push(opn.range[i]);
		}
		if (this.value !== undefined)
			this.setvalue();
	}
	this.setrange(opn.range);
	this.setvalue(opn.value, opn.initevent ? 'init' : '');
	function findvalue(id) {
		for (var i in thos.range) {
			if (thos.range[i].id == id) {
				findvalue(thos.range[i].upid);
				values.push(thos.range[i].id);
				return;
			}
		}
		values.push(0);
	}
	function fillselect(upid, deep, thvalue, autoshow) {
		var bup = false;
		for (var i in opn.range) {
			if (opn.range[i].upid == upid) {
				bup = true;
				break;
			}
		}
		if (!bup)
			return 0;
		var seldom = document.createElement('ciy-select');
		if (opn.hasmore) {
			seldom.setAttribute('com', opn.name + '_cas');
			seldom.setAttribute('hasmore', '1');
		}
		opn.dom.append(seldom);
		var com = ciycmp({
			dom: seldom
			, value: thvalue
			, range: opn.range
			, minwidth: 'auto'
			, first: (opn.all ? { id: upid, name: opn.all } : null)
			, placeholder: opn.placeholder
			, filter: { field: 'upid', value: upid }
			, onchange: function (e) {
				var val = e.value;
				// if (val == thvalue)
				// 	return;
				$5(seldom).next(9999).remove();
				if (val != upid) {
					valuedom.val(0);
					if (fillselect(val, deep + 1, values[deep], true) == 0) {
						valuedom.val(val);
						if (typeof (thos.onchange) == 'function')
							thos.onchange({ name: opn.name, dom: opn.dom, value: val });
					}
				} else {
					valuedom.val(val);
					if (typeof (thos.onchange) == 'function')
						thos.onchange({ name: opn.name, dom: opn.dom, value: val });
				}
			}
		});
		if (autoshow)
			com.showoption();
		return 1;
	}
}
ciycmpfunc.ciyselmulti = function (opn) {
	var thos = this;
	this.onchange = opn.onchange;
	opn.initevent = opn.initevent || opn.dom.hasattr('initevent');
	opn.hasmore = opn.hasmore || opn.dom.hasattr('hasmore');
	opn.chkuse = opn.chkuse || opn.dom.hasattr('chkuse');
	opn.minsearch = toint(opn.minsearch || opn.dom.attr('minsearch') || 0);
	opn.minwidth = opn.minwidth || opn.dom.attr('minwidth') || '';
	opn.placeholder = opn.placeholder || opn.dom.attr('placeholder') || '请选择';
	if (typeof (opn.fn_li) != 'function')
		opn.fn_li = function (range, thos) { return range.name };
	if (typeof (opn.fn_show) != 'function') {
		opn.fn_show = function (ranges, showdom, thos) {
			var html = '';
			var tit = '';
			for (var i = 0; i < ranges.length; i++) {
				var range = ranges[i];
				html += '<div>' + range.name + '</div>';
				tit += range.name + ".\n";
			}
			showdom.html(html ? html : '<span style="color:var(--txt1);">' + opn.placeholder + '</span>');
			showdom.attr('title', tit);
		};
	}
	opn.dom.setstyle('ciy_selmulti', `
		ciy-selmulti{display: block;}
		ciy-selmulti.mini{display: inline-block;vertical-align:middle;}
		ciy-selmulti>._show{display: flex;align-items: center;position: relative;min-height: 2.1em;line-height: 2.1em;vertical-align: top;width: 100%;cursor: pointer;min-height: 2.4em;color: var(--txt8);background-color: var(--bg1);border: 1px solid var(--bg6);border-radius: 4px;box-sizing: border-box;}
		ciy-selmulti>._show>i{width: 1em;height: 1em;margin-right:0.5em;background-image: url("data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgMTAyNCAxMDI0IiB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHBhdGggZD0iTTExNC4wNDg0OTkgNTk2LjkyNDE4MWwzMzcuNTI2MjkzIDMzNy41MjYyOTNjMzMuMzcwMDE1IDMzLjM3MDAxNSA4Ny40NzkzNzcgMzMuMzcwMDE1IDEyMC44NDkzOTIgMGwzMzcuNTI2MjkzLTMzNy41MjYyOTNMMTE0LjA0ODQ5OSA1OTYuOTI0MTgxek01NzIuNDI0MTg0IDg4LjQ5NzU2N2MtMzMuMzcwMDE1LTMzLjM3MDAxNS04Ny40NzkzNzctMzMuMzcwMDE1LTEyMC44NDkzOTIgMEwxMTQuMDU2Njg2IDQyNi4wMjQ4ODNsNzk1Ljg4NTYwNiAwTDU3Mi40MjQxODQgODguNDk3NTY3eiIgZmlsbD0iIzc3Nzc3NyI+PC9wYXRoPjwvc3ZnPg==");}
		ciy-selmulti>._show>div{gap:0.3em;flex-wrap: wrap;}
		ciy-selmulti>._pullul{position: fixed;top: 0;left: 0;right: 0;bottom: 0;display:none;z-index:100000;}
		ciy-selmulti>._pullul>ul{display: inline-flex;height: auto;flex-direction: column;position: fixed;overflow: auto;background:var(--bg1);box-shadow: rgb(204 204 204) 1px 1px 2px;border-radius: 5px;}
		ciy-selmulti>._pullul input{position: fixed;border-radius: 3px;line-height:1.5em;padding-left:0.5em;border:1px solid var(--bg4);border-radius:0;}
		ciy-selmulti>._pullul input:focus{box-shadow:none;border:1px solid var(--bg4);}
		ciy-selmulti>._pullul input::placeholder{color:var(--bg4);}
		ciy-selmulti>._pullul>ul>li{white-space: nowrap;line-height:1.3em;padding:0.5em 1em;border-left: 4px solid var(--bg1);background:var(--bg1);color:var(--txt9);cursor:pointer;}
		ciy-selmulti>._pullul>ul>li.selected{background:var(--bg3);border-left: 4px solid var(--man4);}
		ciy-selmulti>._pullul>ul>li:hover{background:var(--bg5);}
	`);
	var html = '';
	html += '<input name="' + opn.name + '" value="" type="hidden"/>';
	html += '<div class="_show"><div class="flex flex1 txt-over txt-left" style="padding: 0 0.5em;' + (opn.minwidth ? 'min-width:' + opn.minwidth + ';' : '') + '"></div><i></i></div>';
	html += '<div class="_pullul">';
	html += '<input type="text" act="search" placeholder="输入文字或拼音筛选"/>';
	html += '<ul></ul>';
	html += '</div>';
	if (opn.hasmore)
		html += '<input name="' + opn.name + '_name" value="" type="hidden"/>';
	opn.dom.html(html);
	if (opn.minwidth)
		opn.dom.addClass('mini');
	var valuedom = opn.dom.children(0);
	var showmaindom = opn.dom.children(1);
	var showdom = showmaindom.children(0);
	var ulpdom = opn.dom.children(2);
	var searchdom = ulpdom.children(0);
	var uldom = ulpdom.children(1);
	var valnamedom = opn.dom.children(3);
	var nowvalue = [];
	var lishow = false;
	this.setvalue = function (newvalue, from) {
		if (this.range == null)
			return console.error('ciy_select range null', opn.name);
		if (newvalue !== undefined) {
			var vals = (newvalue + '').split(',');
			nowvalue = [];
			for (var i in vals) {
				if (!vals[i])
					continue;
				nowvalue.push(vals[i] + '');
			}
		}
		var selnames = [];
		var ranges = [];
		for (var i in this.range) {
			var range = this.range[i];
			if (opn.chkuse && range.isuse == 2)
				continue;
			if (opn.filter && range[opn.filter.field] != opn.filter.value)
				continue;
			if (nowvalue.indexOf(range.id + '') > -1) {
				selnames.push(range.name);
				ranges.push(range);
			}
		}
		this.value = nowvalue.length > 0 ? ',' + nowvalue.join(',') + ',' : '';
		valuedom.val(this.value);//两边加逗号，便于字符串like检索。但规模化后应避免like检索。
		valnamedom.val(selnames.length > 0 ? ',' + selnames.join(',') + ',' : '');
		opn.fn_show(ranges, showdom);
		if (from && typeof (this.onchange) == 'function')
			this.onchange({ name: opn.name, dom: opn.dom, from: from, value: this.value });
	}
	this.setrange = function (newrange) {
		var ranges = ciyfn.getdictdata(newrange);
		if (ranges == null)
			return console.error('ciy_select range null', opn.name);
		this.range = [];
		for (var i in ranges) {
			if (opn.chkuse && ranges[i].isuse == 2)
				continue;
			if (!ranges[i]._nofilter && opn.filter && ranges[i][opn.filter.field] != opn.filter.value)
				continue;
			ranges[i].name = ranges[i].name + '';
			this.range.push(ranges[i]);
		}
		if (opn.first && typeof (opn.first) == 'object') {
			opn.first._nofilter = true;
			this.range.unshift(opn.first);
		}
		if (opn.all) {
			this.range.unshift({ id: '', name: opn.all + '', _nofilter: true });
		}
		if (ciyfn.topinyin) {
			for (var i in thos.range)
				thos.range[i]._py = ciyfn.topinyin(thos.range[i].name);
		}
		if (this.value !== undefined)
			this.setvalue();
	}
	this.setrange(opn.range);
	this.setvalue(opn.value, opn.initevent ? 'init' : '');
	showmaindom.on('click', function (e) {
		searchdom.val('');
		thos.showoption();
		searchdom[0].focus();
	});
	searchdom.on('keydown', function (e) {
		if (e.keyCode != 13) return;
		e.preventDefault();
		if (uldom.children().length == 1)
			selvalue(uldom.children(0));
		return false;
	});
	searchdom.on('keyup', function (e) {
		if (e.keyCode == 13) return;
		thos.showoption();
	});
	uldom.on('click', 'li', function (e) {
		selvalue(e.currentTarget);
	});
	ulpdom.on('click', function (e) {
		if (e.target == e.currentTarget)
			thos.hideoption();
	});
	function selvalue(lidom) {
		lidom = $5(lidom);
		var clkvalue = lidom.attr('data-id') || '';
		var ind = nowvalue.indexOf(clkvalue);
		if (ind > -1) {
			lidom.removeClass('selected');
			nowvalue.splice(ind, 1);
		}
		else {
			lidom.addClass('selected');
			nowvalue.push(clkvalue);
		}
		thos.setvalue(undefined, 'select');
	}
	function panelrect() {
		var pos = showmaindom.rect();
		var ulwidth = uldom.width();
		var ulheight = 2;
		for (var i = 0; i < uldom.children().length; i++) {
			ulheight += uldom.children(i).rect().height;
		}
		var twidth = pos.width;
		var gulwidth = Math.max(145, ulwidth, twidth);

		var lef = pos.left;
		var lefw = $5(opn.dom).winwidth() - lef - 15;
		if (gulwidth > lefw)
			lef -= gulwidth - lefw;
		if (lef <= 0)
			lef = 0;
		uldom.css({ left: lef + 'px', width: gulwidth + 'px' });
		searchdom.css({ left: lef + 'px', width: gulwidth + 'px' });
		var searchheight = 0;
		if (thos.range.length > opn.minsearch) {
			searchheight = searchdom.rect().height;
			searchdom.css({ display: 'block' });
			searchdom[0].focus();
		} else {
			searchdom.css({ display: 'none' });
		}
		if (window.innerHeight * 0.6 > pos.top) {
			// 下方显示
			var hei = pos.top + pos.height;
			if (hei < 0)
				hei = 0;
			if (searchheight > 0)
				searchdom.css({ top: hei + 'px' });
			uldom.css({ top: (hei + searchheight) + 'px' });
			hei = window.innerHeight - hei - 50 - searchheight;
			uldom.css({ height: (ulheight > hei ? hei : ulheight) + 'px', flexDirection: 'column' });
		} else {
			// 上方显示
			var hei = pos.top;
			if (searchheight > 0)
				searchdom.css({ top: (hei - searchheight - 3) + 'px' });
			uldom.css({ flexDirection: 'column-reverse' });
			if (ulheight > hei) {
				uldom.css({ top: '3px', height: (hei - searchheight - 6) + 'px' });
			} else {
				uldom.css({ top: (hei - ulheight - searchheight - 3) + 'px', height: ulheight + 'px' });
			}
		}
		ulpdom.css({ display: 'block', opacity: 1 });
	}
	this.hideoption = function () {
		if (!lishow)
			return;
		window.removeEventListener('scroll', thos.hideoption);
		lishow = false;
		ulpdom.aniout('translateY(30px)', 0.3);
	}
	this.showoption = function () {
		var keystr = searchdom.val();
		var html = '';
		for (var i in thos.range) {
			if ((thos.range[i].name.toUpperCase().indexOf(keystr.toUpperCase()) == -1) && (tostr(thos.range[i]._py).indexOf(keystr.toUpperCase()) == -1))
				continue;
			html += '<li class="txt-left';
			if (nowvalue.includes(thos.range[i].id + ''))
				html += ' selected';
			html += '" data-id="' + thos.range[i].id + '"';
			html += '>';
			html += opn.fn_li(thos.range[i], thos);
			html += '</li>';
		}
		uldom.html(html);
		if (!lishow) {
			lishow = true;
			ulpdom.css({ opacity: 0, display: 'block' });
			window.addEventListener('scroll', thos.hideoption);
		}
		panelrect();
	}
}
ciycmpfunc.ciyinputcyc = function (opn) {
	var thos = this;
	this.onchange = opn.onchange;
	opn.initevent = opn.initevent || opn.dom.hasattr('initevent');
	opn.hasmore = opn.hasmore || opn.dom.hasattr('hasmore');
	opn.enableunit = opn.enableunit || opn.dom.attr('enableunit') || '';
	var eus = opn.enableunit.split(',');
	opn.enableunit = [];
	for (var i in eus) {
		var eu = eus[i].trim().toLowerCase();
		if (eu == 'month' || eu == 'day' || eu == 'hour' || eu == 'min' || eu == 'sec')
			opn.enableunit.push(eu);
	}
	if (opn.enableunit.length == 0)
		opn.enableunit = ['month', 'day', 'hour', 'min', 'sec'];
	var html = '<input type="hidden" name="' + opn.name + '"/>';
	html += '<input style="width:4em;text-align:right;margin-right:0.2em;border-radius: 4px 0 0 4px;" type="text"' + (opn.hasmore ? ' name="' + opn.name + '_num"' : '') + '/>';
	html += '<select' + (opn.hasmore ? ' name="' + opn.name + '_unit"' : '') + ' style="border-radius: 0 0.5em 0.5em 0!important;margin-left: -4px;">';
	if (opn.enableunit.indexOf('month') >= 0) html += '<option value="month">月</option>';
	if (opn.enableunit.indexOf('day') >= 0) html += '<option value="day">天</option>';
	if (opn.enableunit.indexOf('hour') >= 0) html += '<option value="hour">小时</option>';
	if (opn.enableunit.indexOf('min') >= 0) html += '<option value="min">分钟</option>';
	if (opn.enableunit.indexOf('sec') >= 0) html += '<option value="sec">秒</option>';
	html += '</select>';
	opn.dom.html(html).css({ display: 'inline-flex' });
	var valuedom = opn.dom.children(0);
	var showinpdom = opn.dom.children(1);
	var showseldom = opn.dom.children(2);

	var data = { num: 0, unit: '' };
	this.setvalue = function (newvalue, from) {
		if (newvalue !== undefined) {
			var dat = toint(newvalue);
			if (dat < 0) {
				data.unit = 'month';
				data.num = -dat;
			} else if (dat % 86400 == 0) {
				data.unit = 'day';
				data.num = toint(dat / 86400);
			} else if (dat % 3600 == 0) {
				data.unit = 'hour';
				data.num = toint(dat / 3600);
			} else if (dat % 60 == 0) {
				data.unit = 'min';
				data.num = toint(dat / 60);
			} else {
				data.unit = 'sec';
				data.num = toint(dat);
			}
			this.value = dat;
			if (data.unit == 'month' && opn.enableunit.indexOf('month') == -1) {
				data.num *= 30;
				data.unit = 'day';
			}
			if (data.unit == 'day' && opn.enableunit.indexOf('day') == -1) {
				data.num *= 24;
				data.unit = 'hour';
			}
			if (data.unit == 'hour' && opn.enableunit.indexOf('hour') == -1) {
				data.num *= 60;
				data.unit = 'min';
			}
			if (data.unit == 'min' && opn.enableunit.indexOf('min') == -1) {
				data.num *= 60;
				data.unit = 'sec';
			}
			if (data.unit == 'sec' && opn.enableunit.indexOf('sec') == -1) {
				if (opn.enableunit.indexOf('min') > -1) {
					data.num = Math.ceil(data.num / 60);
					data.unit = 'min';
				}
				else if (opn.enableunit.indexOf('hour') > -1) {
					data.num = Math.ceil(data.num / 3600);
					data.unit = 'hour';
				}
				else if (opn.enableunit.indexOf('day') > -1) {
					data.num = Math.ceil(data.num / 86400);
					data.unit = 'day';
				}
				else if (opn.enableunit.indexOf('month') > -1) {
					data.num = -Math.floor(data.num / 86400 / 30);
					data.unit = 'month';
				}
			}
			showinpdom.val(data.num);
			showseldom.val(data.unit);
		} else {
			var dat = data.num;
			if (data.unit == 'min')
				dat = dat * 60;
			else if (data.unit == 'hour')
				dat = dat * 3600;
			else if (data.unit == 'day')
				dat = dat * 86400;
			else if (data.unit == 'month')
				dat = -dat;
			this.value = dat;
		}
		valuedom.val(this.value);
		if (from && typeof (this.onchange) == 'function')
			this.onchange({ name: opn.name, dom: opn.dom, from: from, value: this.value });
	}
	this.setvalue(opn.value, opn.initevent ? 'init' : '');
	showinpdom.on('keyup', function () {
		data.num = toint(showinpdom.val());
		thos.setvalue(undefined, 'num');
	});
	showseldom.on('change', function () {
		data.unit = showseldom.val();
		thos.setvalue(undefined, 'unit');
	});
}
ciycmpfunc.ciyinputunitedit = function (opn) {
	var thos = this;
	this.onchange = opn.onchange;
	opn.initevent = opn.initevent || opn.dom.hasattr('initevent');
	opn.hasmore = opn.hasmore || opn.dom.hasattr('hasmore');
	var html = '<input type="hidden" name="' + opn.name + '"/>';
	html += '<input type="text"' + (opn.hasmore ? ' name="' + opn.name + '_a1"' : '') + ' style="text-align: center;width: 4em;padding: .5em 0;" />';
	html += '<input type="text"' + (opn.hasmore ? ' name="' + opn.name + '_n1"' : '') + ' style="font-size:0.7em;text-align:center;width: 2.5em;padding: .2em 0;min-height: 1em;border-radius:0;border-bottom: 2px solid #000000;" />';
	html += '<input type="text"' + (opn.hasmore ? ' name="' + opn.name + '_a2"' : '') + ' style="text-align: center;width: 4em;padding: .5em 0;" />';
	html += '<input type="text"' + (opn.hasmore ? ' name="' + opn.name + '_n2"' : '') + ' style="font-size:0.7em;text-align:center;width: 2.5em;padding: .2em 0;min-height: 1em;border-radius:0;border-bottom: 2px solid #000000;" />';
	html += '<input type="text"' + (opn.hasmore ? ' name="' + opn.name + '_a3"' : '') + ' style="text-align: center;width: 4em;padding: .5em 0;" />';
	opn.dom.html(html);
	var valuedom = opn.dom.children(0);
	var show1dom = opn.dom.children(1);
	var show2dom = opn.dom.children(2);
	var show3dom = opn.dom.children(3);
	var show4dom = opn.dom.children(4);
	var show5dom = opn.dom.children(5);
	var lastval = '';
	this.setvalue = function (newvalue, from) {
		if (newvalue !== undefined) {
			var us = (newvalue + '').split('|');//瓶|24|盒|20|箱
			show1dom.val(us[0]);
			us[1] = toint(us[1]);
			show2dom.val(us[1] == 0 ? '' : us[1]);
			show3dom.val(us[2] ? us[2] : '');
			us[3] = toint(us[3]);
			show4dom.val(us[3] == 0 ? '' : us[3]);
			show5dom.val(us[4] ? us[4] : '');
		}
		this.value = show1dom.val();
		if (toint(show2dom.val()) > 0 && show3dom.val()) {
			this.value += '|' + toint(show2dom.val()) + '|' + show3dom.val();
			if (toint(show4dom.val()) > 0 && show5dom.val())
				this.value += '|' + toint(show4dom.val()) + '|' + show5dom.val();
		}
		valuedom.val(this.value);
		if (this.value == lastval)
			return;
		lastval = this.value;
		if (from && typeof (this.onchange) == 'function')
			this.onchange({ name: opn.name, dom: opn.dom, from: from, value: this.value });
	}
	this.setvalue(opn.value, opn.initevent ? 'init' : '');
	opn.dom.on('keyup', function () {
		thos.setvalue(undefined, 'keyup');
	});
}
ciycmpfunc.ciymap = function (opn) {
	var thos = this;
	this.onchange = opn.onchange;
	opn.initevent = opn.initevent || opn.dom.hasattr('initevent');
	opn.bet = toint(opn.bet || opn.dom.attr('bet') || 10000000);
	opn.mapkey = opn.mapkey || opn.dom.hasattr('mapkey');
	if (opn.bet < 1)
		opn.bet = 1;
	var html = '';
	html += '<input type="hidden" name="' + (opn.name == 'loc' ? 'lat' : opn.name + '_lat') + '"/>';
	html += '<input type="hidden" name="' + (opn.name == 'loc' ? 'lng' : opn.name + '_lng') + '"/>';
	html += '<a class="btn">地图</a>';
	html += '<span style="font-size:0.7em;"></span>';
	opn.dom.html(html);
	var valuedom_lat = opn.dom.children(0);
	var valuedom_lng = opn.dom.children(1);
	var showbtndom = opn.dom.children(2);
	var showspandom = opn.dom.children(3);
	function caldectoint(dec) {
		if (opn.bet <= 1)
			return dec;
		return toint(dec * opn.bet);
	}
	this.setvalue = function (newvalue, from) {
		if (typeof (newvalue) == 'object') {
			this.value.lat = newvalue.lat;
			this.value.lng = newvalue.lng;
		} else if (newvalue !== undefined) {
			var ll = (newvalue + '').split(',');
			if (ll.length > 1) {
				this.value.lat = ll[0];
				this.value.lng = ll[1];
			}
		}
		valuedom_lat.val(this.value.lat);
		valuedom_lng.val(this.value.lng);
		if (ciyfn.isfloat0(this.value.lat)) {
			showspandom.html('未定位');
		} else {
			showspandom.html(from == 'select' ? '已选择位置' : '已定位');
		}
		if (from && typeof (this.onchange) == 'function')
			this.onchange({ name: opn.name, dom: opn.dom, from: from, value: this.value });
	}
	if (!this.value)
		this.value = { lat: 0, lng: 0 };
	this.setvalue(opn.value, opn.initevent ? 'init' : '');
	showbtndom.on('click', function () {
		ciyfn.alert({
			title: '选择地图位置'
			, width: 'max'
			, height: 'max'
			, frame: '/jscss/map_loc.html?edit=true&addr=' + opn.mapkey + '&lat=' + thos.value.lat / opn.bet + "&lng=" + thos.value.lng / opn.bet
			, cb: function (opnmap) {
				opnmap.close();
				thos.value.lat = caldectoint(opnmap.inputs.lat);
				thos.value.lng = caldectoint(opnmap.inputs.lng);
				thos.setvalue(undefined, 'select');
			}
		});
	});
}
ciycmpfunc.ciytimepoint = function (opn) {
	var thos = this;
	this.onchange = opn.onchange;
	opn.initevent = opn.initevent || opn.dom.hasattr('initevent');
	opn.hasmore = opn.hasmore || opn.dom.hasattr('hasmore');
	opn.placeholder = ciyfn.lang(opn.placeholder || opn.dom.attr('placeholder') || '请选择');
	opn.type = opn.type || opn.dom.attr('type') || 'min'; //min,sec
	opn.inpwidth = '4em';
	if (opn.type == 'sec')
		opn.inpwidth = '5.5em';
	var csstxt = '';
	csstxt += 'ciy-timepoint>._pulldt{user-select: none;position: fixed;top: 0;left: 0;right: 0;bottom: 0;display:none;z-index:100000;}';
	csstxt += 'ciy-timepoint>._pulldt.top>._dt{flex-direction: column-reverse;}';
	csstxt += 'ciy-timepoint>._pulldt.top>._mainsel{flex-direction: column-reverse;}';
	csstxt += 'ciy-timepoint>._pulldt.top>._dt>._timebtn{align-items: flex-end;}';
	csstxt += 'ciy-timepoint>._pulldt.top>._dt>._timebtn>._timerit{flex-direction: column-reverse;}';
	csstxt += 'ciy-timepoint>._pulldt>._dt{display: flex;flex-direction: column;width: 25em;padding: 0.5em;position: fixed;background:var(--bg1);box-shadow: rgb(204 204 204) 1px 1px 2px;}';
	csstxt += 'ciy-timepoint>._pulldt>._dt>._mainsel{display: flex;flex-direction: column;height: 19em;}';
	csstxt += 'ciy-timepoint>._pulldt>._dt>._timebtn{display: flex;text-align:center;}';
	csstxt += 'ciy-timepoint>._pulldt>._dt>._timebtn>._time{width: 5.5em;border: 1px solid var(--bg4);border-radius: 0.5em;margin: 0 0.2em;}';
	csstxt += 'ciy-timepoint>._pulldt>._dt>._timebtn>._time.active{border: 1px solid var(--man5);}';
	csstxt += 'ciy-timepoint>._pulldt>._dt>._timebtn>._time>div{height:3em;}';
	csstxt += 'ciy-timepoint>._pulldt>._dt>._timebtn>._timerit{display: flex;flex-direction: column;margin-left:auto;text-align: right;}';
	csstxt += 'ciy-timepoint>._pulldt>._dt>._timebtn>._time>span{font-size:0.8em;}';
	csstxt += 'ciy-timepoint>._pulldt>._dt>._timebtn>._time>div>span{font-size: 2em;padding-top: 0.3em;display: block;}';
	csstxt += 'ciy-timepoint>._pulldt>._dt>._mainsel>._datehour{display: flex;flex-wrap: wrap;}';
	csstxt += 'ciy-timepoint>._pulldt>._dt>._mainsel>._datehour>div{width: 16.66%;padding: 0.5em;line-height: 2em;text-align: center;font-size: 1.5em;}';
	csstxt += 'ciy-timepoint>._pulldt>._dt>._mainsel>._datehour>div.active{color:var(--man5);}';
	csstxt += 'ciy-timepoint>._pulldt>._dt>._mainsel>._date601{display: flex;flex-wrap: wrap;padding: 2em 0;}';
	csstxt += 'ciy-timepoint>._pulldt>._dt>._mainsel>._date601>div{width: 16.66%;padding: 0.5em;line-height: 1.65em;text-align: center;font-size: 1.5em;}';
	csstxt += 'ciy-timepoint>._pulldt>._dt>._mainsel>._date601>div.active{color:var(--man5);}';
	csstxt += 'ciy-timepoint>._pulldt>._dt>._mainsel>._date602{display: flex;flex-wrap: wrap;padding-top:2em;}';
	csstxt += 'ciy-timepoint>._pulldt>._dt>._mainsel>._date602>div{width: 20%;padding: 0.5em;line-height: 1.65em;text-align: center;font-size: 1.5em;}';
	csstxt += 'ciy-timepoint>._pulldt>._dt>._mainsel>._date602>div.active{color:var(--man5);}';
	opn.dom.setstyle('ciy_timepoint', csstxt);
	var html = '';
	html += '<input name="' + opn.name + '" type="hidden"/>';
	html += '<input ' + (opn.hasmore ? 'name="' + opn.name + '_dt"' : '') + ' type="text" placeholder="' + opn.placeholder + '" autocomplete="off" style="width:' + opn.inpwidth + '"/>';
	html += '<div class="_pulldt"><div class="_dt"><div class="_timebtn"></div><div class="_mainsel"></div></div></div>';
	opn.dom.html(html);
	var valuedom = opn.dom.children(0);
	var valnamedom = opn.dom.children(1);
	var pulldom = opn.dom.children(2);
	var dtdom = pulldom.children(0);
	var timebtndom = dtdom.children(0);
	var mainseldom = dtdom.children(1);
	var lishow = false;
	var dtimeshow = [0, 0, 0];//界面时间 h,i,s
	var dtimevalue = [0, 0, 0];//选中时间
	var idxms = 1;
	function arr_timepoint(time) {
		if (time <= 0 || time > 86400)
			return [0, 0, 0];
		time--;
		var hour = toint(time / 3600);
		var minute = toint((time - hour * 3600) / 60);
		var second = time - hour * 3600 - minute * 60;
		return [hour, minute, second];
	}
	function str_timepoint(time) {
		if (/^\d+$/.test(time)) {
			time = toint(time);
		} else if (typeof (time) == 'string') {
			var vs = time.split(':');
			if (vs.length == 1)
				time = 0;
			else
				time = toint(vs[0]) * 3600 + toint(vs[1]) * 60 + toint(vs[2]) + 1;
		}
		if (time > 0 && time <= 86400)
			return time;
		return 0;
	}
	this.setvalue = function (newvalue, from) {
		if (newvalue !== undefined) {
			this.value = str_timepoint(newvalue);
		}
		var dat = ciyfn.totimepoint(this.value, opn.type == 'sec');
		valuedom.val(this.value);
		valnamedom.val(dat);
		if (this.value != 0) {
			dtimeshow = arr_timepoint(this.value);
			dtimevalue = arr_timepoint(this.value);
		}
		if (from && typeof (this.onchange) == 'function')
			this.onchange({ name: opn.name, dom: opn.dom, from: from, value: this.value });
	}
	this.setvalue(opn.value, opn.initevent ? 'init' : '');
	valnamedom.on('click', function (e) {
		thos.showdt();
	});
	valnamedom.on('keydown', function (e) {
		thos.hidedt();
	});
	valnamedom.on('blur', function (e) {
		var val = str_timepoint(e.currentTarget.value);
		if (val == thos.value)
			return;
		thos.value = val;
		thos.setvalue(undefined, 'blur');
	});
	pulldom.on('click', function (e) {
		e.stopPropagation();
		thos.hidedt();
	});
	dtdom.on('click', function (e) {
		e.stopPropagation();
	});
	dtdom.on('click', '._timehour', function (e) {
		showhour();
	});
	dtdom.on('click', '._time60', function (e) {
		show60(e.currentTarget.getAttribute('data-idx'));
	});
	dtdom.on('click', '._datehour>div', function (e) {
		var hour = toint(e.currentTarget.getAttribute('data-hour'));
		dtimeshow[0] = hour;
		dtimevalue[0] = hour;
		showhour();
	});
	dtdom.on('click', '._date601>div', function (e) {
		var ms60 = toint(e.currentTarget.getAttribute('data-601')) + (dtimevalue[idxms] % 10);
		dtimeshow[idxms] = ms60;
		dtimevalue[idxms] = ms60;
		show60();
	});
	dtdom.on('click', '._date602>div', function (e) {
		var ms60 = toint(e.currentTarget.getAttribute('data-602')) + toint(dtimevalue[idxms] / 10) * 10;
		dtimeshow[idxms] = ms60;
		dtimevalue[idxms] = ms60;
		show60();
	});
	dtdom.on('click', '.btn.clear', function (e) {
		thos.value = 0;
		thos.hidedt();
		thos.setvalue(undefined, 'clear');
	});
	dtdom.on('click', '.btn.done', function (e) {
		thos.value = dtimevalue[0] * 3600 + dtimevalue[1] * 60 + dtimevalue[2] + 1;
		thos.hidedt();
		thos.setvalue(undefined, 'done');
	});
	this.hidedt = function () {
		if (!lishow)
			return;
		window.removeEventListener('scroll', thos.hidedt);
		lishow = false;
		pulldom.hide('up', 0.5);
	}
	this.showdt = function () {
		var html = '';
		html += '<div class="_time _timehour"><div><span>' + dtimevalue[0] + '</span></div><span>' + ciyfn.lang('选择小时') + '</span></div>'
		html += '<div class="_time _time60" data-idx="1"><div><span>' + dtimevalue[1] + '</span></div><span>' + ciyfn.lang('选择分钟') + '</span></div>'
		if (opn.type == 'sec')
			html += '<div class="_time _time60" data-idx="2"><div><span>' + dtimevalue[2] + '</span></div><span>' + ciyfn.lang('选择秒钟') + '</span></div>'
		html += '<div class="_timerit"><div><a class="btn sm done">' + ciyfn.lang('完成') + '</a></div><div><a class="btn sm def clear">' + ciyfn.lang('清空') + '</a></div></div>'
		timebtndom.html(html);
		showhour();
		if (!lishow) {
			lishow = true;
			window.addEventListener('scroll', thos.hidedt);
		}
		pulldom.css({ display: 'block' });
		var valpos = valnamedom.rect();
		var lef = valpos.left;
		var winwidth = opn.dom.winwidth();
		var winheight = opn.dom.winheight();
		if (lef + dtdom.width() + 20 > winwidth)
			lef = winwidth - dtdom.width() - 20;
		if (lef < 20)
			lef = 20;
		if (winheight < valpos.top + valpos.height + dtdom.height()) {
			pulldom.addClass('top');
			dtdom.css({ top: 'auto', bottom: (winheight - valpos.top) + 'px', left: lef + 'px' });
		} else {
			pulldom.removeClass('top');
			dtdom.css({ bottom: 'auto', top: (valpos.top + valpos.height) + 'px', left: lef + 'px' });
		}
		pulldom.show('up', 0.5);
	}
	function showhour() {
		$5('._timehour>div>span', dtdom).html(dtimevalue[0]);
		$5('._time', dtdom).removeClass('active');
		$5('._timehour', dtdom).addClass('active');
		var html = '';
		html += '<div class="_datehour">';
		for (var i = 0; i < 24; i++) {
			if (dtimevalue[0] == i)
				html += '<div class="active" data-hour="' + i + '">' + i + '</div>';
			else
				html += '<div data-hour="' + i + '">' + i + '</div>';
		}
		html += '</div>';
		mainseldom.html(html);
	}
	function show60(idx) {
		if (idx)
			idxms = idx;
		$5('._time60[data-idx="' + idxms + '"]>div>span', dtdom).html(dtimevalue[idxms]);
		$5('._time', dtdom).removeClass('active');
		$5('._time60[data-idx="' + idxms + '"]', dtdom).addClass('active');
		var html = '';
		html += '<div class="_date601">';
		for (var i = 0; i < 60; i += 10) {
			if (toint(dtimevalue[idxms] / 10) * 10 == i)
				html += '<div class="active" data-601="' + i + '">' + i + '</div>';
			else
				html += '<div data-601="' + i + '">' + i + '</div>';
		}
		html += '</div>';
		html += '<div class="ciy-hr"></div>';
		html += '<div class="_date602">';
		for (var i = 0; i < 10; i++) {
			if (dtimevalue[idxms] % 10 == i)
				html += '<div class="active" data-602="' + i + '">' + i + '</div>';
			else
				html += '<div data-602="' + i + '">' + i + '</div>';
		}
		html += '</div>';
		mainseldom.html(html);
	}
}
ciycmpfunc.ciymarkdown = function (opn) {
	var thos = this;
	this.onchange = opn.onchange;
	opn.initevent = opn.initevent || opn.dom.hasattr('initevent');
	opn.stor = opn.stor || opn.dom.attr('stor') || ciy_vars.storselect;
	opn.reviewbg = opn.reviewbg || opn.dom.attr('reviewbg') || '';
	opn.height = opn.height || opn.dom.attr('height') || '';
	if (!opn.height)
		opn.height = window.innerHeight - 210;
	opn.imgwidth = toint(opn.imgwidth || opn.dom.attr('imgwidth'));
	opn.imgheight = toint(opn.imgheight || opn.dom.attr('imgheight'));
	opn.width = opn.width || opn.dom.attr('width');
	if (opn.width == 'pc')
		opn.width = '50%';
	else
		opn.width = '418px';
	opn.maxsize = toint(opn.maxsize || opn.dom.attr('maxsize'));
	opn.zipjpg = toint(opn.zipjpg || opn.dom.attr('zipjpg'));
	opn.waterfont = opn.waterfont || opn.dom.attr('waterfont') || '28px Arial';
	opn.watertext = opn.watertext || opn.dom.attr('watertext') || '';
	opn.fileext = (opn.fileext || opn.dom.attr('fileext') || '.pdf,.zip,.docx,.doc,.pptx,.ppt,.xlsx,.xls,.txt,.rar,.7z,.tar,.glb').toLowerCase().split(',');
	opn.action = opn.action || opn.dom.attr('action');
	if (!opn.action) {
		opn.action = "/z/?func=" + ciy_vars.uploadurl;
	}
	var now = new Date();
	opn.path = opn.path || opn.dom.attr('path') || 'nopath';
	opn.path = opn.path.replace('{Y}', now.getFullYear()).replace('{m}', ciyfn.topad0(now.getMonth() + 1)).replace('{d}', ciyfn.topad0(now.getDate()));

	opn.dom.setstyle('ciy_mdedit', `
		.mdedit_textcont{display: block;overflow-y: hidden;width:100%;font:17px Helvetica Neue, Helvetica, PingFang SC, \\5FAE\\8F6F\\96C5\\9ED1, Tahoma, Arial, sans-serif;}
		.mdedit_retit{position: absolute;left: 1em;top: -1.6em;padding: 0.2em 1em;background: var(--bg6);line-height: 1.3em;font-size: 0.8em;border-radius: 5px 5px 0 0;}
		.mdedit_review{overflow: overlay;border: 1px solid var(--bg6);background:var(--bg2);}
	`);
	var html = `<input type="hidden" name="${opn.name}" /><input type="file" style="display:none;" multiple/>
	<div class="row" style="padding-top:1.5em;">
		<div class="col-12" style="max-width:${opn.width};">
			<div class="lang mdedit_retit">录入区</div>
			<textarea class="mdedit_textcont" style="line-height:35px;"></textarea>
		</div>
		<div class="col-12" style="padding-left: 4px;max-width:${opn.width};">
			<div class="lang mdedit_retit">预览区</div>
			<div class="mdedit_review" style="padding:0 0.5em;background:${opn.reviewbg};"></div>
		</div>
	</div>
	<div style="padding-left: 0.5em;">
		<ciy-switch y="自动换行" n="不换行" style="font-size:0.6em;"></ciy-switch>
		<a class="lang btn sm btn_upimg">图片</a>
		<a class="lang btn sm btn_upav">视频</a>
		<a class="lang btn sm btn_upfile">附件</a>
		<label class="btn sm">
			<span class="lang">更多</span>
			<input type="checkbox">
			<ul class="ciy-popmenu" style="bottom:2em;">
				<li class="lang btn_kh">去掉空行</li>
				<li class="lang btn_kg">去掉两边空格</li>
			</ul>
			<span class="caret"></span>
		</label>
		<a class="lang btn def sm btn_help">帮助</a>
	</div>`;
	opn.dom.html(html);
	var valuedom = opn.dom.children(0);
	var filedom = opn.dom.children(1);
	var textareadom = $5('textarea', opn.dom);
	var reviewdom = $5('.mdedit_review', opn.dom);
	ciycmp({
		dom: $5('ciy-switch', opn.dom), onchange: function (e) {
			textareadom.css({ whiteSpace: (e.value == 1) ? 'initial' : 'nowrap' });
		}
	});
	ciyfn.domlang(opn.dom);
	this.setvalue = function (newvalue, from) {
		if (newvalue !== undefined) {
			this.value = newvalue + '';
			if (this.value.substring(0, 4) != '[MD]')
				this.value = '[MD]' + this.value;
			textareadom.val(this.value.substring(4));
		}
		valuedom.val(this.value);
		textareadom.css({ height: "inherit" });
		var hei = textareadom[0].scrollHeight + 10;
		if (hei < opn.height)
			hei = opn.height;
		textareadom.css({ height: hei + 'px' });
		var rvtop = reviewdom[0].scrollTop;
		reviewdom.html('');
		reviewdom.css({ height: hei + 'px' });
		reviewdom.html(ciyfn.markdown(this.value));
		setTimeout(function () {
			reviewdom[0].scrollTop = rvtop;
		}, 50);
		if (from && typeof (this.onchange) == 'function')
			this.onchange({ name: opn.name, dom: opn.dom, from: from, value: this.value });
	}
	this.setvalue(opn.value, opn.initevent ? 'init' : '');
	textareadom.on('input', function (e) {
		thos.value = '[MD]' + textareadom.val();
		thos.setvalue(undefined, 'input');
	});
	textareadom.on('keydown', function (e) {
		if (e.keyCode == 9) {
			e.preventDefault();
			var indent = '    ';
			var start = textareadom[0].selectionStart;
			var end = textareadom[0].selectionEnd;
			var selected = window.getSelection().toString();
			selected = indent + selected.replace(/\n/g, '\n' + indent);
			var txt = textareadom.val();
			textareadom.val(txt.substring(0, start) + selected + txt.substring(end));
			textareadom[0].setSelectionRange(start + indent.length, start + selected.length);
		}
	});
	textareadom.on('paste', function (e) {
		if (!e.clipboardData)
			return console.log('不支持剪贴板粘贴');
		if (!e.clipboardData.items)
			return console.log('剪贴板粘贴无item');
		var len = e.clipboardData.items.length;
		var bb = [];
		for (var i = 0; i < len; i++) {
			if (e.clipboardData.items[i].type.indexOf("image") !== -1) {
				var blob = e.clipboardData.items[i].getAsFile();
				bb.push(blob);
			}
			else if (e.clipboardData.items[i].type.indexOf("video/mp4") !== -1) {
				var blob = e.clipboardData.items[i].getAsFile();
				bb.push(blob);
			}
			else {
				var blob = e.clipboardData.items[i].getAsFile();
				if (blob) {
					var ind = blob.name.lastIndexOf('.');
					var ext = blob.name.substring(ind).toLowerCase();
					if (opn.fileext.indexOf(ext) > -1)
						bb.push(blob);
					else
						ciyfn.toast(ciyfn.lang('不支持的附件类型') + ': ' + ext);
				}
			}
		}
		opn.type = ['JPG', 'PNG', 'MP4'];
		for (var i = 0; i < opn.fileext.length; i++)
			opn.type.push(opn.fileext[i].substring(1).toUpperCase());
		mdedit_uploadfile(bb);
	});
	opn.dom.on('click', '.btn_help', function (e) {
		var html = '';
		html += '<code>#</code> 一级标题<br/>';
		html += '<code>#c</code> 一级标题居中<br/>';
		html += '<code>#r</code> 一级标题右对齐<br/>';
		html += '<code>##</code> 二级标题 带[n、]编号<br/>';
		html += '<code>###</code> 三级标题 带[n.m、]编号<br/>';
		html += '<code>####</code> 四级标题 带[n.n.k、]编号<br/>';
		html += '<code>@c</code> 正文居中<br/>';
		html += '<code>@r</code> 正文右对齐<br/>';
		html += '<code>!imgurl</code> 引用图片，imgurl为图片地址<br/>';
		html += '<code>`强调`</code> 反引号包裹，<kbd>强调</kbd> 效果<br/>';
		html += '<code>**加粗**</code> 双星号包裹，<b>加粗</b> 效果<br/>';
		html += '<code>[链接文字](网址)</code>，如:[众产](https://ciy.cn/)。<br/>';
		html += '<code>[!imgurl](https://ciy.cn/)</code>，图片链接。<br/>';
		html += '<code>[链接文字](附件链接)</code> 附件下载<br/>';
		html += '可在录入区直接 Ctrl+V 粘贴剪贴板中的图片和视频。<br/>';
		ciyfn.alert({ content: html, width: '30em' });
	});
	opn.dom.on('click', '.btn_kh', function (e) {
		var content = textareadom.val();
		content = content.replace(/\n\n/g, '\n');
		textareadom.val(content);
		textareadom.trigger('input');
	});
	opn.dom.on('click', '.btn_kg', function (e) {
		var content = textareadom.val();
		content = content.replace(/　/g, '  ');
		content = content.split('\n').map(line => line.trim()).join('\n');
		textareadom.val(content);
		textareadom.trigger('input');
	});
	opn.dom.on('click', '.btn_upav', function (e) {
		opn.type = ['MP3', 'MP4'];
		mdedit_selectfile(".mp3,.mp4");
	});
	opn.dom.on('click', '.btn_upimg', function (e) {
		opn.type = ['JPG', 'PNG'];
		mdedit_selectfile(".jpg,.png");
	});
	opn.dom.on('click', '.btn_upfile', function (e) {
		opn.type = [];
		for (var i = 0; i < opn.fileext.length; i++)
			opn.type.push(opn.fileext[i].substring(1).toUpperCase());
		mdedit_selectfile(opn.fileext);
	});
	filedom.on("change", function (e) {
		mdedit_uploadfile(e.target.files);
	});
	function mdedit_selectfile(limitType) {
		if (limitType)
			filedom.attr("accept", limitType);
		filedom.trigger('click');
	};
	function mdedit_uploadfile(files) {
		if (!files || files.length == 0)
			return;
		ciyfn.file_uploads(99, files, opn, {
			success: function (url, file) {
				var startPos = textareadom[0].selectionStart;
				var endPos = textareadom[0].selectionEnd;
				if (startPos === undefined || endPos === undefined)
					return;
				var text = textareadom.val();
				var imgurl = '!' + url;
				var ind = url.lastIndexOf('.');
				var ext = url.substring(ind).toLowerCase();
				if (opn.fileext.indexOf(ext) > -1)
					imgurl = '[' + ciyfn.lang('附件') + ': ' + file.name + '](' + url + ')';
				if (startPos > 0 && text[startPos - 1] != '\n')
					imgurl = '\n' + imgurl;
				if (text[startPos] != '\n')
					imgurl += '\n';
				text = text.substring(0, startPos) + imgurl + text.substring(endPos);
				textareadom.val(text);
				textareadom.trigger('input');
			}
			, fail: function (err) {
				ciyfn.alert(err.errmsg);
			}
		});
	}
}
ciygv.pyzm = " ";
ciygv.pydyz = { "19969": "DZ", "19975": "WM", "19988": "QJ", "20048": "YL", "20056": "SC", "20060": "NM", "20094": "QG", "20127": "QJ", "20167": "QC", "20193": "YG", "20250": "KH", "20256": "ZC", "20282": "SC", "20285": "QJG", "20291": "TD", "20314": "YD", "20340": "NE", "20375": "TD", "20389": "YJ", "20391": "CZ", "20415": "PB", "20446": "YS", "20447": "SQ", "20504": "TC", "20608": "KG", "20854": "QJ", "20857": "ZC", "20911": "PF", "20504": "TC", "20608": "KG", "20854": "QJ", "20857": "ZC", "20911": "PF", "20985": "AW", "21032": "PB", "21048": "XQ", "21049": "SC", "21089": "YS", "21119": "JC", "21242": "SB", "21273": "SC", "21305": "YP", "21330": "ZC", "21333": "SDC", "21345": "QK", "21378": "CA", "21397": "SC", "21414": "XS", "21442": "SC", "21477": "JG", "21480": "TD", "21484": "ZS", "21494": "YX", "21505": "YX", "21512": "HG", "21523": "XH", "21537": "PB", "21542": "PF", "21549": "KH", "21571": "E", "21574": "DA", "21588": "TD", "21589": "O", "21618": "ZC", "21621": "KHA", "21632": "ZJ", "21654": "KG", "21679": "LKG", "21683": "KH", "21719": "YH", "21734": "WOE", "21780": "WN", "21804": "XH", "21899": "ZD", "21903": "RN", "21908": "WO", "21939": "ZC", "21956": "SA", "21964": "YA", "21970": "TD", "22031": "JG", "22040": "XS", "22060": "ZC", "22066": "ZC", "22079": "MH", "22129": "XJ", "22179": "XA", "22237": "NJ", "22244": "TD", "22280": "JQ", "22300": "YH", "22313": "XW", "22331": "YQ", "22343": "YJ", "22351": "PH", "22395": "DC", "22412": "TD", "22484": "PB", "22500": "PB", "22534": "ZD", "22549": "DH", "22561": "PB", "22612": "TD", "22771": "KQ", "22831": "HB", "22841": "JG", "22855": "QJ", "22865": "XQ", "23013": "ML", "23081": "WM", "23487": "SX", "23558": "QJ", "23561": "YW", "23586": "YW", "23614": "YW", "23615": "SN", "23631": "PB", "23646": "ZS", "23663": "ZT", "23673": "YG", "23762": "TD", "23769": "ZS", "23780": "QJ", "23884": "QK", "24055": "XH", "24113": "DC", "24162": "ZC", "24273": "QJ", "24324": "NL", "24377": "TD", "24378": "QJ", "24439": "PF", "24554": "ZS", "24683": "TD", "24694": "WE", "24733": "LK", "24925": "TN", "25094": "ZG", "25100": "XQ", "25103": "XH", "25153": "PB", "25170": "PB", "25179": "KG", "25203": "PB", "25240": "ZS", "25282": "FB", "25303": "NA", "25324": "KG", "25341": "ZY", "25373": "WZ", "25375": "XJ", "25528": "SD", "25530": "SC", "25552": "TD", "25774": "ZC", "25874": "ZC", "26044": "YW", "26080": "WM", "26292": "PB", "26333": "PB", "26355": "ZY", "26366": "CZ", "26397": "ZC", "26399": "QJ", "26415": "ZS", "26451": "SB", "26526": "ZC", "26552": "JG", "26561": "TD", "26588": "JG", "26597": "CZ", "26629": "ZS", "26638": "YL", "26646": "XQ", "26653": "KG", "26657": "XJ", "26727": "HG", "26894": "ZC", "26937": "ZS", "26946": "ZC", "26999": "KJ", "27099": "KJ", "27449": "YQ", "27481": "XS", "27542": "ZS", "27663": "ZS", "27748": "TS", "27784": "SC", "27788": "ZD", "27795": "TD", "27812": "O", "27850": "PB", "27852": "MB", "27895": "SL", "27898": "PL", "27973": "QJ", "27981": "KH", "27986": "HX", "27994": "XJ", "28044": "YC", "28065": "WG", "28177": "SM", "28267": "QJ", "28291": "KH", "28337": "ZQ", "28463": "TL", "28548": "DC", "28601": "TD", "28689": "PB", "28805": "JG", "28820": "QG", "28846": "PB", "28952": "TD", "28975": "ZC", "29325": "QJ", "29575": "SL", "29602": "FB", "30010": "TD", "30044": "CX", "30058": "PF", "30091": "YSP", "30111": "YN", "30229": "XJ", "30427": "SC", "30631": "YQ", "30655": "QJ", "30684": "QJG", "30707": "SD", "30729": "XH", "30796": "LG", "30917": "PB", "31074": "NM", "31085": "JZ", "31109": "SC", "31181": "ZC", "31192": "MLB", "31293": "JQ", "31400": "YX", "31584": "YJ", "31896": "ZN", "31909": "ZY", "31995": "XJ", "32321": "PF", "32327": "ZY", "32420": "XQ", "32421": "HG", "32438": "LG", "32473": "GJ", "32488": "TD", "32521": "QJ", "32527": "PB", "32562": "ZSQ", "32564": "JZ", "32735": "ZD", "32793": "PB", "33071": "PF", "33098": "XL", "33100": "YA", "33152": "PB", "33261": "CX", "33324": "BP", "33333": "TD", "33406": "YA", "33426": "WM", "33432": "PB", "33445": "JG", "33486": "ZN", "33493": "TS", "33507": "QJ", "33540": "QJ", "33544": "ZC", "33564": "XQ", "33617": "YT", "33632": "QJ", "33636": "XH", "33637": "YX", "33694": "WG", "33705": "PF", "33728": "YW", "33882": "SR", "34067": "WM", "34074": "YW", "34121": "QJ", "34255": "ZC", "34259": "XL", "34425": "JH", "34430": "XH", "34485": "KH", "34503": "YS", "34532": "HG", "34552": "XS", "34558": "YE", "34593": "ZL", "34660": "YQ", "34892": "XH", "34928": "SC", "34999": "QJ", "35048": "PB", "35059": "SC", "35098": "ZC", "35203": "TQ", "35265": "JX", "35299": "JX", "35782": "SZ", "35828": "YS", "35830": "E", "35843": "TD", "35895": "YG", "35977": "MH", "36158": "JG", "36228": "QJ", "36426": "XQ", "36466": "DC", "36711": "ZYG", "36767": "PB", "36866": "SK", "36951": "YW", "37034": "YX", "37063": "XH", "37218": "ZC", "37325": "ZC", "38063": "PB", "38079": "TD", "38085": "QY", "38107": "DC", "38116": "TD", "38123": "YD", "38224": "HG", "38241": "XTC", "38271": "ZC", "38415": "YE", "38426": "KH", "38461": "YD", "38463": "AE", "38466": "PB", "38477": "XJ", "38518": "YT", "38551": "WK", "38585": "ZC", "38704": "XS", "38739": "LJ", "38761": "GJ", "38808": "SQ", "39048": "JG", "39049": "XJ", "39052": "HG", "39076": "CZ", "39271": "XT", "39534": "TD", "39552": "TD", "39584": "PB", "39647": "SB", "39730": "LG", "39748": "TPB", "40109": "ZQ", "40479": "ND", "40516": "HG", "40536": "HG", "40583": "QJ", "40765": "YQ", "40784": "QJ", "40840": "YK", "40863": "QJG" };

ciyfn.topinyin = function (str) {//输入 自行车，返回 ZXC,ZHC
	var arr = [];
	for (var i = 0, len = str.length; i < len; i++) {
		var ch = str.charAt(i);
		var uni = ch.charCodeAt(0);
		var rc = '';
		if (uni > 40869 || uni < 19968)
			rc = ch;
		else if (ciygv.pydyz[uni])
			rc = ciygv.pydyz[uni];
		else
			rc = ciygv.pyzm.charAt(uni - 19968);
		arr.push(rc);
	}
	function getCombinations(index, current) {
		if (index === arr.length) {
			return [current];
		}
		var combinations = [];
		for (var i = 0; i < arr[index].length; i++) {
			combinations = combinations.concat(getCombinations(index + 1, current + arr[index][i]));
		}
		return combinations;
	}
	return getCombinations(0, '').join(',');
}