<#include "/common/defaultEngine.html"/>
<@pageTheme mark="${config.optimize()?string('true', 'false')}">
<@header title="代码生成列表" bodyClass="gray-bg" libs=["table"]/>
<div class="container-div">
	<div class="row">
		<div class="col-sm-12 search-collapse">
			<@f.form id="gen-form" class="form-inline">
				<div class="select-list">
					<ul>
						<li>
							表名称：
							<@f.select name="tableName" items=genTables itemLabel="tableName" itemValue="tableName" blankValue="" blankLabel="所有"/>
						</li>
						<li>
							表描述：
							<@f.select name="tableComment" items=genTables itemLabel="tableComment" itemValue="tableComment" blankValue="" blankLabel="所有"/>
						</li>
						<li class="select-time">
							<label>表时间： </label>
							<input type="text" class="form-control time-input" id="startTime" placeholder="开始时间" name="startTime"/>
							<span>-</span>
							<input type="text" class="form-control time-input" id="endTime" placeholder="结束时间" name="endTime"/>
						</li>
						<li>
							<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
							<a class="btn btn-warning btn-rounded btn-sm" onclick="opt.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
						</li>
					</ul>
				</div>
			</@f.form>
		</div>
		<@table id="bootstrap-table">
			<@shiro.hasPermission name="tool:gen:code">
				<a class="btn btn-success multiple disabled" onclick="javascript:batchGenCode()">
					<i class="fa fa-download"></i> 生成
				</a>
			</@shiro.hasPermission>
				<a class="btn btn-info" onclick="selectDb()">
					<i class="fa fa-upload"></i> 导入
				</a>
			<@shiro.hasPermission name="tool:gen:edit">
				<a class="btn btn-primary single disabled" onclick="opt.operate.editTab()">
					<i class="fa fa-edit"></i> 修改
				</a>
			</@shiro.hasPermission>
			<@shiro.hasPermission name="tool:gen:del">
				<a class="btn btn-danger multiple disabled" onclick="opt.operate.delAll()">
					<i class="fa fa-remove"></i> 删除
				</a>
			</@shiro.hasPermission>

			<@shiro.hasPermission name="tool:gen:code">
				<a class="btn btn-warning" onclick="reloadMaper()">
					<i class="fa fa-refresh"></i> 刷新Mapper.xml配置文件
				</a>
			</@shiro.hasPermission>
		</@table>
	</div>
</div>
<@footer>
<script type="text/javascript">
	var prefix = baseURL + "tool/gen";
	var editFlag = "${permission.hasPermi('tool:gen:edit')}";
	var delFlag = "${permission.hasPermi('tool:gen:del')}";
	var previewFlag =  "${permission.hasPermi('tool:gen:preview')}";
	var codeFlag = "${permission.hasPermi('tool:gen:code')}";
	var genType = ${dict.getDictListJson('sys_gen_type')};

	$(function() {
		var options = {
			url: prefix + "/list",
			editUrl: prefix + "/edit/{id}",
			delUrl: prefix + "/del",
			sortName: "createTime",
			sortOrder: "desc",
			showExport: true,
			modalName: "生成配置",
			columns: [{
				checkbox: true
			},
			{
				field: 'id',
				title: '<@ctx.i18n text = "编号"/>',
				visible: false
			},
			{
				title: "<@ctx.i18n text = "序号"/>",
				formatter: function (value, row, index) {
					return $.table.serialNumber(index);
				}
			},
			 {
				field: 'dbName',
				title: '<@ctx.i18n text = "数据源(库)"/>',
				sortable: true
			},
			{
				field: 'tableName',
				title: '<@ctx.i18n text = "表名"/>',
				sortable: true
			},
			{
				field: 'tableComment',
				title: '<@ctx.i18n text = "表描述"/>'
			},
			{
				field: 'tplCategory',
				title: '<@ctx.i18n text = "模板名称"/>',
				formatter: function(value, row, index) {
					return $.table.selectDictLabel(genType, value)
				}
			},
			{
				field: 'className',
				title: '<@ctx.i18n text = "实体类名称"/>',
				sortable: true
			},
			{
				field: 'createTime',
				title: "<@ctx.i18n text = "创建时间"/>",
				sortable: true
			},
			{
				field: 'updateTime',
				title: "<@ctx.i18n text = "更新时间"/>",
				sortable: true
			},
			{
				title:"<@ctx.i18n text = "操作"/>",
				align: 'left',
				width: 120,
				class:'action',
				formatter: function(value, row, index) {
					/*var actions = [];
					actions.push('<a class="btn btn-info btn-xs ' + previewFlag + '" href="javascript:void(0)" onclick="preview(\'' + row.id + '\')"><i class="fa fa-search"></i>预览</a> ');
					actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="opt.operate.editTab(\'' + row.id + '\')"><i class="fa fa-edit"></i><@ctx.i18n text = "编辑"/></a> ');
					actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="opt.operate.del(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a> ');
					actions.push('<a class="btn btn-primary btn-xs ' + codeFlag + '" href="javascript:void(0)" onclick="genCode(\'' + row.id + '\')"><i class="fa fa-bug"></i>生成代码</a> ');
					return actions.join('');*/
					var actions = [];
					if(row.tplCategory != "child"){
						actions.push('<a class="' + previewFlag + '" href="javascript:void(0)" title="<@ctx.i18n text = "预览"/>" onclick="preview(\'' + row.id + '\')"><i class="fa fa-search"></i></a> ');
					}
					actions.push('<a class="' + editFlag + '" href="javascript:void(0)" title="<@ctx.i18n text = "编辑"/>" onclick="opt.operate.editTab(\'' + row.id + '\')"><i class="fa fa-edit"></i></a> ');
					actions.push('<a class="' + delFlag + '" href="javascript:void(0)" title="<@ctx.i18n text = "删除"/>" onclick="opt.operate.del(\'' + row.id + '\')"><i class="fa fa-trash-o"></i></a> ');
					if(row.tplCategory != "child"){
						var info = "<a  class='btn btn-default btn-xs "+codeFlag+"' onclick=codeDownload(\'" + row.tableName + "\');><i class='fa icon-cloud-download'></i> <@ctx.i18n text = "下载代码"/></a>&nbsp;";
						info += "<a  class='btn btn-default btn-xs "+codeFlag+"' onclick=genCode(\'"+row.tableName+"\',\'" + row.id + "\',\'"+row.isCover+"\');><i class='fa fa-bug'></i> <@ctx.i18n text = "生成代码"/></a>&nbsp;";
						actions.push('<a tabindex="0"  role="button" data-container="body" data-placement="left" data-toggle="popover" data-html="true" data-trigger="hover" data-content="'+info+'"><i class="fa fa-chevron-circle-right" title="<@ctx.i18n text = "更多操作"/>"></i></a>');
					}
					return actions.join('');
				}
			}]
		};
		$.table.init(options);
	});

	/*预览代码*/
	function preview(id) {
		var options = {
			url: prefix + "/preview/" + id,
			title:'预览代码',
			btn:['<i class="fa fa-close"></i> '+$.i18n.prop("取消")],
		};
		opt.modal.openMainWin(options);
	}

	/*生成代码*/
	function codeDownload(tableName) {
		opt.modal.confirm("确定要下载" + tableName + "生成表代码吗?", function() {
			location.href = prefix + "/download/" + tableName;
			opt.modal.msg('执行成功,正在生成代码请稍后…');
		})
	}

	function genCode(tableName,id,isCover){
		var c = "确定要生成表["+tableName+"]代码";
		if(isCover === "Y"){
			c+=",<span style='color: red;font-weight: bold'>覆盖替换原有文件</span>"
		}
		opt.modal.confirm(c+"吗?", function() {
			var data = {'tableId':id};
			var url = prefix + "/genCode";
			opt.operate.post(url,data);
		})
	}


	function reloadMaper() {
	   opt.modal.confirm("确定要刷新Mapper.xml配置文件吗?", function() {
		   var url = prefix + "/mapper/reload";
		   opt.operate.post(url);
		})
	}



	/*批量生成代码*/
	function batchGenCode() {
		var rows = $.table.selectColumns("tableName");
		if (rows.length == 0) {
			opt.modal.alertWarning("请选择要生成的数据");
			return;
		}
		opt.modal.confirm("确认要生成选中的" + rows.length + "条数据吗?", function() {
			location.href = prefix + "/batchGenCode?tables=" + rows;
			opt.modal.msg('执行成功,正在生成代码请稍后…');
		});
	}

	/*导入表结构*/
	function importTable(id) {
		var importTableUrl = prefix + "/importTable/" +id;
		opt.modal.open("导入表结构", importTableUrl);
	}

	/* 用户管理-重置密码 */
	function selectDb() {
		var options = {
			title: "选择数据库",
			width: "800",
			height: "300",
			url: prefix + '/selectDb',
			callBack: DeptDoSubmit
		};
		opt.modal.openOptions(options);
	}

	function DeptDoSubmit(index, layero) {
		var body = layer.getChildFrame('body', index);
		importTable(body.find('#dbName').val());
		layer.close(index);
	}
</script>
</@footer>
</@pageTheme>