$(document).ready(function() {

	// 고객사 목록
	fncRetrieveOprMng($("#custCode"));

	$("#custCode").on('change', function() {
		fncRetrieveOprMng($(this));
	});


	var opts = {
		columns: [{
			data: "scenarioName"
		}, {
			data: "updateDate"
		}, {
			data: "updateId"
		},
		{
			data: "seq"
		}],
		"order": [1, 'desc'],
		columnDefs: [{
			"defaultContent": "-",
			"targets": "_all",
			"className": "center",
		},
		{
			targets: 0,
			render: function(data, type, row, index) {
				return data;
			}
		},
		{
			targets: 1,
			render: function(data, type, row, index) {
				return data;
			}
		},
		{
			targets: 2,
			render: function(data, type, row, index) {
				return data;
			}
		},
		{
			targets: 3,
			render: function(data, type, row, index) {
				return `<button type="button" class="btn grey" onclick="fncDelScenarioList(${data});">삭제</button>`;
			}
		}],
		createdRow: function(row, data, index, meta) {

		},
		drawCallback: function(settings) {

		},

	}

	$.utils.dataTable.init('/admin/conversation/scenario/scenarioList.do', opts);

	search();

	$("#resultTable").DataTable().on('dblclick', 'tr', function(e, dt, type, indexes) {
		var rowData = $("#resultTable").DataTable().row(this).data();

		var form = $('<form>');
		form.attr("method", "post");
		form.attr("action", "/admin/conversation/scenario/edit.do");

		var serviceCode = $("#oprMngName option:selected").val();
		rowData.serviceCode = serviceCode;

		var custCode = $("#custCode option:selected").val();
		rowData.custCode = custCode;

		$.each(rowData, function(key, value) {
			var field = $('<input>');
			field.attr("type", "hidden");
			field.attr("name", key);
			field.attr("value", value);
			form.append(field);
		});

		$(document.body).append(form);
		form.submit();

	});
});

function fncServiceList() {
	var param = $("#selectCustName").val();

	$.ajax({
		type: "post",
		async: false,
		datatype: "text",
		url: "/admin/system/project/selectServiceNameList.do",
		data: {
			param: param
		},
		success: (function(data) {
			$("#oprMngName").html('')
			for (var k in data.nMap) {

				var obj = data.nMap[k];
				var serviceCode = obj.serviceCode;
				var serviceName = obj.serviceName;
				var selected = getServiceCode == serviceCode ? 'seleted' : '';

				var laborOption = `<option value="${serviceCode}" ${selected}>${serviceName}</option>`;
				$("#oprMngName").append(laborOption);
			}

		})
	});
	$('#oprMngName').niceSelect('update');
}

function fncSendData() {
	search();
}

function search() {
	var scenarioName = $('#oprMngName1').val();
	var custCode = $("#selectCustName").val();
	var serviceCode = $("#oprMngName").val();

	$.ajax({
		type: 'post',
		async: false,
		datatype: 'text',
		url: '/admin/conversation/scenario/scenarioList.do',
		data: {
			custCode: custCode,
			serviceCode: serviceCode,
			scenarioName: scenarioName
		},
		success: function(data) {
			$("#resultTable").renderTable(data.list);

		}
	});

}

function fncAddScenario() {
	var serviceCode = $("#oprMngName").val();
	var custCode = $("#custCode").val();

	var obj = {
		serviceCode,
		custCode
	};

	if (serviceCode != '' && serviceCode != null) {
		var form = $('<form>');
		form.attr("method", "post");
		form.attr("action", "/admin/conversation/scenario/edit.do");

		$.each(obj, function(key, value) {
			var field = $('<input>');
			field.attr("type", "hidden");
			field.attr("name", key);
			field.attr("value", value);
			form.append(field);
		});

		$(document.body).append(form);
		form.submit();
	}
}

function fncRetrieveOprMng(e) {
	$('#oprMngName').empty();
	var param = "custCode=" + e.val();

	$.ajax({
		type: "post",
		async: false,
		datatype: "text",
		url: "/admin/retrieveOprMng.do",
		data: param,
		success: function(data) {
			$.each(data.nMap, function(i, val) {
				var serviceCode = val.oprMngCode;
				var selected = getServiceCode == serviceCode ? 'selected' : '';
				$('#oprMngName').append(`<option value="${val.oprMngCode}" ${selected}>` + val.oprMngName + "</option>");
			});
		}
	});
	$('#oprMngName').niceSelect('update');
}

function fncDelScenarioList(seq) {

	$.utils.confirm('삭제하시겠습니까?', result);

	function result() {
		$.ajax({
			type: "post",
			async: false,
			datatype: "text",
			url: '/admin/conversation/scenario/deleteScenarioList.do',
			data: {
				seq: seq
			},
			success: (function(data) {
				if (data.result) {
					$.utils.success('정상적으로 삭제되었습니다.');
				} else {
					$.utils.warning('삭제 실패');
				}
			})
		});

		search();
	}
}

