From ee7984a6c18409ead83c07f3ca7e14a90d01536e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=98=89=E7=A5=A5=20=E8=A9=B9?= Date: Wed, 7 Aug 2024 13:02:56 +0800 Subject: [PATCH] updates --- Controllers/ApiController.cs | 165 ++++++++++ Views/Home/CompanyList.cshtml | 104 +++++++ .../assets/javascript/custom/companylist.js | 293 +++++++++++++++++- 3 files changed, 556 insertions(+), 6 deletions(-) diff --git a/Controllers/ApiController.cs b/Controllers/ApiController.cs index dd2f5b96..bcdf6579 100644 --- a/Controllers/ApiController.cs +++ b/Controllers/ApiController.cs @@ -25,6 +25,171 @@ namespace QuotationMaker.Controllers } + [Route("addEditDelGetContactPerson")] + public ActionResult AddEditDelContactPerson(IFormCollection obj) + { + contactPersonListResult ret = new contactPersonListResult(); + + authToken token = new authToken(this._httpContextAccessor); + if (token.user_isLogin == false) + { + HttpContext.Response.Cookies.Delete("token_key"); + ret.ret = "no"; + ret.err_code = "99999"; + ret.message = "非登入狀態!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + + + string company_uid = obj["company_uid"].ToString(); + string contactPerson_uid = obj["contactPerson_uid"].ToString(); + string contactPerson_name = obj["contactPerson_name"].ToString(); + string contactPerson_email = obj["contactPerson_email"].ToString(); + string contactPerson_tel = obj["contactPerson_tel"].ToString(); + + string method = obj["method"].ToString(); + + + + + if (method == "") + { + ret.ret = "no"; + ret.err_code = "0001"; + ret.message = "沒有method!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + + + + + if (method == "add") + { + if (contactPerson_name.Trim() == "") + { + ret.ret = "no"; + ret.err_code = "0003"; + ret.message = "沒有contactPerson_name!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + contactPerson_uid = GlobalClass.CreateRandomCode(24); + contactPerson newItem = new contactPerson(); + newItem.contactPerson_name = contactPerson_name; + newItem.contactPerson_uid = contactPerson_uid; + newItem.company_uid = company_uid; + + newItem.contactPerson_tel = contactPerson_tel; + newItem.contactPerson_email = contactPerson_email; + + newItem.contactPerson_lastmodify_uid = token.user_uid; + newItem.contactPerson_createdate = DateTime.Now; + newItem.contactPerson_modifydate = DateTime.Now; + + conn.Insert(newItem); + ret.contactPersons.Add(newItem); + ret.ret = "yes"; + + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + if (contactPerson_uid.Trim() == "") + { + ret.ret = "no"; + ret.err_code = "0002"; + ret.message = "沒有contactPerson_uid!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + + + contactPerson editItem = conn.QueryFirstOrDefault("select * from contactPerson where contactPerson_isdel = 'N' and contactPerson_uid = @contactPerson_uid ", new { contactPerson_uid = contactPerson_uid }); + + if (editItem == null) + { + ret.ret = "no"; + ret.err_code = "0004"; + ret.message = "沒有contactPerson_uid此筆資料!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + if (method == "edit") + { + + + if (contactPerson_name.Trim() == "") + { + ret.ret = "no"; + ret.err_code = "0002"; + ret.message = "沒有contactPerson_name!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + editItem.contactPerson_name = contactPerson_name; + + + editItem.contactPerson_tel = contactPerson_tel; + editItem.contactPerson_email = contactPerson_email; + + editItem.contactPerson_lastmodify_uid = token.user_uid; + editItem.contactPerson_modifydate = DateTime.Now; + + conn.Update(editItem); + ret.contactPersons.Add(editItem); + ret.ret = "yes"; + + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + if (method == "get") + { + ret.contactPersons.Add(editItem); + ret.ret = "yes"; + + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + if (method == "del") + { + editItem.contactPerson_isdel = "Y"; + editItem.contactPerson_lastmodify_uid = token.user_uid; + editItem.contactPerson_modifydate = DateTime.Now; + + conn.Execute("update contactPerson set contactPerson_isdel = 'Y' where contactPerson_uid = @contactPerson_uid ", new { contactPerson_uid = contactPerson_uid }); + + conn.Update(editItem); + ret.ret = "yes"; + + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + [Route("contactPersonList")] + public ActionResult ContactPersonList(IFormCollection obj) + { + contactPersonListResult ret = new contactPersonListResult(); + + authToken token = new authToken(this._httpContextAccessor); + if (token.user_isLogin == false) + { + HttpContext.Response.Cookies.Delete("token_key"); + ret.ret = "no"; + ret.err_code = "99999"; + ret.message = "非登入狀態!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + string company_uid = obj["company_uid"].ToString(); + + ret.contactPersons = conn.Query("select * from contactPerson where contactPerson_isdel = 'N' and company_uid = @company_uid ", new { company_uid = company_uid }).ToList(); + ret.ret = "yes"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + [Route("addEditDelGetCompany")] public ActionResult AddEditDelSubItem(IFormCollection obj) { diff --git a/Views/Home/CompanyList.cshtml b/Views/Home/CompanyList.cshtml index 1201d2fd..6f9842c7 100644 --- a/Views/Home/CompanyList.cshtml +++ b/Views/Home/CompanyList.cshtml @@ -113,4 +113,108 @@ + + +
+ +
+ + +
+
\ No newline at end of file diff --git a/wwwroot/assets/javascript/custom/companylist.js b/wwwroot/assets/javascript/custom/companylist.js index 6f326945..7486d4ab 100644 --- a/wwwroot/assets/javascript/custom/companylist.js +++ b/wwwroot/assets/javascript/custom/companylist.js @@ -8,6 +8,12 @@ var subItemRowPos; $(document).ready(function () { loadDataTable(); + loadSubItemDataTable(); + + $('#subItemListNewBtn').on('click', function () { + $("#clientSubItem_method").val('add'); + $('#clientSubItemModal').modal('toggle'); + }); $('#maintItemNewModal').on('click', function () { $("#company_method").val('add'); @@ -70,8 +76,157 @@ $(document).ready(function () { } }); }); + + $('#clientSubItemSaveBtn').on('click', function () { + var contactPerson_uid = $('#clientSubItem_contactPerson_uid').val(); + var company_uid = $('#clientSubItem_company_uid').val(); + var method = $('#clientSubItem_method').val(); + var contactPerson_name = $('#modal_contactPerson_name').val(); + var contactPerson_email = $('#modal_contactPerson_email').val(); + var contactPerson_tel = $('#modal_contactPerson_tel').val(); + + var msg = ''; + + if (contactPerson_name == '') { + msg += '請輸入窗口姓名!\n'; + } + + if (contactPerson_tel == '') { + msg += '請輸入窗口電話!\n'; + } + + if (msg != '') { + alert(msg); + return; + } + + var formData = { + company_uid: company_uid, + contactPerson_uid: contactPerson_uid, + method: method, + contactPerson_name: contactPerson_name, + contactPerson_email: contactPerson_email, + contactPerson_tel: contactPerson_tel + } + + $.ajax({ + url: "/Api/addEditDelGetContactPerson", + type: "post", + data: formData, + success: function (data, textStatus, jqXHR) { + if (data.ret == "yes") { + var obj = data.contactPersons[0]; + + if (method == "add") { + subItemTable.fnAddData(obj); + } + + if (method == "edit") { + subItemTable.fnUpdate(obj, subItemRowPos); + } + + + + $('#clientSubItemModal').modal('toggle'); + } else { + alert(data.message); + + if (data.err_code == "99999") { + location.href = "/Root/Login"; + } + } + }, + error: function (jqXHR, textStatus, errorThrown) { + alert('網路或伺服器發生錯誤,請稍後重試!'); + } + }); + }); }); +function buttonSubItemClick(obj) { + var type = obj.getAttribute('data-method'); + var uid = obj.getAttribute('data-uid'); + var company_uid = $('#clientSubItem_company_uid').val(); + + subItemRowID = $('#' + uid); + + subItemRowPos = subItemTable.fnGetPosition($('#' + uid)[0]); + + if (type == "edit") { + var formData = { + method: 'get', + company_uid: company_uid, + contactPerson_uid: uid + } + + $.ajax({ + url: "/Api/addEditDelGetContactPerson", + type: "post", + data: formData, + success: function (data, textStatus, jqXHR) { + if (data.ret == "yes") { + var obj = data.contactPersons[0]; + + $("#clientSubItem_method").val('edit'); + $("#clientSubItem_contactPerson_uid").val(obj.contactPerson_uid); + $("#modal_contactPerson_name").val(obj.contactPerson_name).trigger('change'); + + $("#modal_contactPerson_email").val(obj.contactPerson_email).trigger('change'); + $("#modal_contactPerson_tel").val(obj.contactPerson_tel).trigger('change'); + + $('#clientSubItemModal').modal('toggle'); + } else { + alert(data.message); + + if (data.err_code == "99999") { + location.href = "/Root/Login"; + } + } + }, + error: function (jqXHR, textStatus, errorThrown) { + alert('網路或伺服器發生錯誤,請稍後重試!'); + } + }); + + + } + + if (type == "del") { + if (confirm('確定刪除此筆子項目資料? ')) { + + var formData = { + method: 'del', + company_uid: company_uid, + contactPerson_uid: uid + } + + $.ajax({ + url: "/Api/addEditDelGetContactPerson", + type: "post", + data: formData, + success: function (data, textStatus, jqXHR) { + if (data.ret == "yes") { + var row = subItemTable.api().row(subItemRowID).remove().draw(false); + alert('刪除成功'); + + } else { + alert(data.message); + + if (data.err_code == "99999") { + location.href = "/Root/Login"; + } + } + }, + error: function (jqXHR, textStatus, errorThrown) { + alert('網路或伺服器發生錯誤,請稍後重試!'); + } + }); + + + } + } +} + function buttonClick(obj) { var type = obj.getAttribute('data-method'); @@ -84,26 +239,27 @@ function buttonClick(obj) { if (type == "preview") { var mainItem_name = obj.innerText.trim(); var formData = { - dept_uid: dept_uid, - mainItem_uid: uid + company_uid: uid } $.ajax({ - url: "/AuthApi/authSubItemList", + url: "/Api/contactPersonList", type: "post", data: formData, success: function (data, textStatus, jqXHR) { if (data.ret == "yes") { - var obj = data.subItems; + var obj = data.contactPersons; + $('#dt-responsive-subItem').dataTable().fnClearTable(); if (obj.length > 0) { $('#dt-responsive-subItem').dataTable().fnAddData(obj); } - $('#mainItemTitle').val(mainItem_name + ' 的子項目列表').trigger('change'); + $('#mainItemTitle').val(mainItem_name + ' 的窗口列表').trigger('change'); - $('#subItemList_mainItem_uid').val(uid); + $('#clientSubItem_company_uid').val(uid); + $('#subItemList_company_uid').val(uid); $('#clientSubItemListModal').modal('toggle'); } else { alert(data.message); @@ -325,5 +481,130 @@ function loadDataTable() { } } + dataTables.init(); +} + +function loadSubItemDataTable() { + var dataTables = { + init: function init() { + + this.bindUIActions(); + }, + bindUIActions: function bindUIActions() { + + // event handlers + this.table = this.handleDataTables(); + + // add buttons + //this.table.buttons().container().appendTo('#dt-buttons').unwrap(); + }, + handleDataTables: function handleDataTables() { + //$('#myTable').append(""); + return $('#dt-responsive-subItem').DataTable({ + dom: '<\'text-muted\'Bif>\n <\'table-responsive\'trl>\n <\'mt-4\'p>', + lengthChange: true, + lengthMenu: [[25, 50, 100, -1], [25, 50, 100, "All"]], + pageLength: 25, + buttons: [ + //{ + // text: '休假設定', + // action: function (e, dt, node, config) { + // vacationBtnFun(); + + // } + //}, + //'excelHtml5' + ], + language: { + paginate: { + previous: '', + next: '' + }, + buttons: { + copyTitle: 'Data copied', + copyKeys: 'Use your keyboard or menu to select the copy command' + } + }, + autoWidth: false, + rowId: 'contactPerson_uid', + deferRender: true, + initComplete: function () { + subItemTable = $('#dt-responsive-subItem').dataTable(); + $('#dt-responsive-subItem').on('click', 'a', function () { + buttonSubItemClick(this); + }); + + $('#dt-responsive-subItem').on('click', 'button', function () { + buttonSubItemClick(this); + }); + + + }, + order: [[0, 'desc']], + info: true, + search: "搜尋:", + searching: true, + columns: [ + { data: 'contactPerson_name', className: 'align-top text-left', orderable: true, searchable: true }, + { data: 'contactPerson_email', className: 'align-top text-right', orderable: true, searchable: true }, + { data: 'contactPerson_tel', className: 'align-top text-right', orderable: true, searchable: true }, + { data: 'contactPerson_uid', className: 'align-top text-center', orderable: false, searchable: false } + ], + columnDefs: [ + { + targets: 0, + className: 'align-middle text-left', + orderable: false, + searchable: true, + render: function render(data, type, row, meta) { + + return row.contactPerson_name; + //return '' + row.subItem_name + ''; + + } + }, + { + targets: 3, + className: 'align-middle text-center', + orderable: false, + searchable: false, + render: function render(data, type, row, meta) { + var ret = ''; + + ret += ''; + ret += ''; + return ret; + } + } + + ], + responsive: { + details: { + display: $.fn.dataTable.Responsive.display.childRowImmediate, + type: '' + } + } + }); + }, + handleSearchRecords: function handleSearchRecords() { + var self = this; + + $('#table-search, #filterBy').on('keyup change focus', function (e) { + var filterBy = $('#filterBy').val(); + var hasFilter = filterBy !== ''; + var value = $('#table-search').val(); + + self.table.search('').columns().search('').draw(); + + if (hasFilter) { + self.table.columns(filterBy).search(value).draw(); + } else { + self.table.search(value).draw(); + } + + }); + } + } + dataTables.init(); } \ No newline at end of file