define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
function archiveEscHtml(s) {
return String(s == null ? '' : s)
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"');
}
function archiveCan(rule) {
var map = (typeof Config !== 'undefined' && Config.procuremenAuth) ? Config.procuremenAuth : null;
if (!map) {
return false;
}
var key = String(rule || '').toLowerCase().replace(/^procuremen\//, '');
var v = map[key];
return v === true || v === 1 || v === '1';
}
var Controller = {
index: function () {
Table.api.init({
extend: {
index_url: 'procuremenarchive/index' + location.search,
table: 'purchase_order',
}
});
var table = $("#table");
function archiveCellText(value) {
var text = archiveEscHtml(value == null ? '' : value);
return "
" + text + "
";
}
var archiveColumns = [
{checkbox: true, width: 42},
{
field: 'order_batch_no',
title: '批次号',
operate: 'LIKE',
width: 125,
align: 'left',
halign: 'left',
formatter: function (value) {
var v = value == null ? '' : String(value).trim();
return v !== '' ? archiveCellText(v) : '';
}
},
{field: 'CCYDH', title: '订单号', operate: 'LIKE', width: 110, align: 'left', halign: 'left'},
{
field: 'CYJMC',
title: '印件名称',
operate: 'LIKE',
width: 360,
align: 'left',
halign: 'left',
class: 'autocontent',
formatter: function (value) {
return archiveCellText(value);
}
},
{
field: 'CGYMC',
title: '工序名称',
operate: 'LIKE',
width: 120,
align: 'left',
halign: 'left',
class: 'autocontent',
formatter: function (value) {
return archiveCellText(value);
}
},
{
field: 'pick_company_name',
title: '中标供应商',
operate: 'LIKE',
width: 200,
align: 'left',
halign: 'left',
class: 'autocontent',
formatter: function (value) {
return archiveCellText(value);
}
},
{
field: 'inbound_result',
title: '是否合格',
operate: false,
width: 100,
align: 'center',
halign: 'center',
formatter: function (value) {
var v = value == null ? '' : String(value).trim();
if (v === '合格') {
return '合格';
}
if (v === '不合格') {
return '不合格';
}
if (v.indexOf('合格') >= 0 || v.indexOf('不合格') >= 0) {
return archiveCellText(v);
}
return '';
}
},
{
field: 'delivery_status',
title: '交货情况',
operate: false,
width: 90,
align: 'center',
halign: 'center',
formatter: function (value) {
var v = value == null ? '' : String(value).trim();
if (v === '准时' || v === '滞后') {
return '' + archiveEscHtml(v) + '';
}
return '';
}
},
{
field: 'createtime',
title: '审批时间',
operate: 'RANGE',
addclass: 'datetimerange',
autocomplete: false,
width: 145,
align: 'left',
halign: 'left',
sortable: true,
formatter: function (value, row) {
var text = row && row.createtime_text != null ? String(row.createtime_text).trim() : '';
if (text && text !== '-' && text !== '—') {
return text;
}
if (value == null || value === '' || value === 0 || value === '0') {
return '';
}
var n = parseInt(value, 10);
if (!isNaN(n) && n > 946684800) {
return Table.api.formatter.datetime.call(this, value, row);
}
var s = String(value).trim();
return (s === '-' || s === '—') ? '' : s;
}
},
{
field: 'complete_time',
title: '完结时间',
operate: false,
sortable: true,
width: 160,
align: 'left',
halign: 'left',
formatter: function (value, row) {
var text = row && row.complete_time_text != null ? String(row.complete_time_text).trim() : '';
if (text && text !== '-' && text !== '—') {
return text;
}
if (value == null || value === '' || value === 0 || value === '0') {
return '';
}
var n = parseInt(value, 10);
if (!isNaN(n) && n > 946684800) {
return Table.api.formatter.datetime.call(this, value, row);
}
var s = String(value).trim();
return (s === '-' || s === '—') ? '' : s;
}
},
{
field: 'operate',
title: '操作',
width: 100,
align: 'center',
halign: 'center',
operate: false,
table: table,
formatter: function (value, row) {
var sid = row && row.scydgy_id;
if (!sid) {
return '';
}
if (!archiveCan('details')) {
return '';
}
return ' 详情';
},
events: {}
}
];
/** 表头/表体共用固定列宽,避免内容撑开导致竖线错位 */
function archiveGetColWidths() {
var widths = [];
$.each(archiveColumns, function (_, col) {
if (!col || col.visible === false) {
return;
}
var w = parseInt(col.width, 10);
widths.push((!w || isNaN(w)) ? (col.checkbox ? 42 : 120) : w);
});
return widths;
}
function archiveApplyColgroup($tbl, widths) {
if (!$tbl || !$tbl.length || !widths.length) {
return;
}
var total = 0;
var i;
for (i = 0; i < widths.length; i++) {
total += widths[i];
}
// 撑满容器 100%,列宽作比例;窄屏时至少保留合计最小宽
$tbl.css({
tableLayout: 'fixed',
width: '100%',
minWidth: total + 'px'
});
var $cg = $tbl.children('colgroup');
if (!$cg.length) {
$cg = $('').prependTo($tbl);
}
$cg.empty();
for (i = 0; i < widths.length; i++) {
$cg.append($('').attr('style', 'width:' + widths[i] + 'px;min-width:' + widths[i] + 'px;'));
}
$tbl.find('thead tr:first > th').each(function (idx) {
if (idx >= widths.length) {
return;
}
var w = widths[idx];
var $th = $(this);
$th.css({width: w + 'px', minWidth: w + 'px', maxWidth: ''});
$th.find('.fht-cell').css({width: w + 'px', minWidth: w + 'px', maxWidth: ''});
});
$tbl.find('tbody tr:first-child:not(.no-records-found) > *').each(function (idx) {
if (idx >= widths.length) {
return;
}
var w = widths[idx];
$(this).css({width: w + 'px', minWidth: w + 'px', maxWidth: ''});
});
}
function archiveSyncHeaderBodyAlign() {
if ($('body').hasClass('fa-sidebar-resizing')) {
return;
}
var $bt = table.closest('.bootstrap-table');
if (!$bt.length) {
return;
}
var widths = archiveGetColWidths();
if (!widths.length) {
return;
}
var $headerWrap = $bt.find('> .fixed-table-container > .fixed-table-header').first();
if (!$headerWrap.length) {
$headerWrap = $bt.find('.fixed-table-header').first();
}
var $bodyWrap = $bt.find('> .fixed-table-container > .fixed-table-body').first();
if (!$bodyWrap.length) {
$bodyWrap = $bt.find('.fixed-table-body').first();
}
var $headerTable = $headerWrap.length ? $headerWrap.children('table').first() : $();
var $bodyTable = $bodyWrap.length ? $bodyWrap.children('table').first() : table;
if (!$bodyTable.length) {
$bodyTable = $bt.find('.fixed-table-body > table').first();
}
if ($headerTable.length && $bodyTable.length && $headerTable[0] !== $bodyTable[0]) {
var bodyEl = $bodyWrap[0];
var sb = bodyEl ? Math.max(0, bodyEl.offsetWidth - bodyEl.clientWidth) : 0;
$headerWrap.css('margin-right', sb > 0 ? sb + 'px' : '');
archiveApplyColgroup($headerTable, widths);
archiveApplyColgroup($bodyTable, widths);
$headerWrap.scrollLeft($bodyWrap.scrollLeft() || 0);
} else if ($bodyTable.length) {
archiveApplyColgroup($bodyTable, widths);
}
}
function archiveScheduleAlign() {
if ($('body').hasClass('fa-sidebar-resizing')) {
return;
}
setTimeout(archiveSyncHeaderBodyAlign, 0);
setTimeout(archiveSyncHeaderBodyAlign, 80);
}
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'scydgy_id',
sortName: 'createtime',
sortOrder: 'desc',
commonSearch: true,
search: true,
pagination: true,
sidePagination: 'server',
smartDisplay: false,
clickToSelect: false,
pageSize: Config.pagesize || localStorage.getItem('pagesize') || 20,
pageList: [10, 15, 20, 25, 50],
showJumpto: true,
columns: [archiveColumns]
});
Table.api.bindevent(table);
table.on('post-body.bs.table load-success.bs.table post-header.bs.table', function () {
archiveScheduleAlign();
});
table.closest('.bootstrap-table').find('.fixed-table-body').off('scroll.procuremenArchive')
.on('scroll.procuremenArchive', function () {
var $bt = table.closest('.bootstrap-table');
var $headerWrap = $bt.find('> .fixed-table-container > .fixed-table-header').first();
if ($headerWrap.length) {
$headerWrap.scrollLeft($(this).scrollLeft() || 0);
}
});
$(window).off('resize.procuremenArchive').on('resize.procuremenArchive', function () {
archiveScheduleAlign();
});
$(document).off('fa.sidebar.resized.procuremenArchive fa.table.relayout.procuremenArchive')
.on('fa.sidebar.resized.procuremenArchive fa.table.relayout.procuremenArchive', function () {
archiveScheduleAlign();
});
$(document).off('click.procuremenArchiveCommonSearch', 'button[name="commonSearch"]').on('click.procuremenArchiveCommonSearch', 'button[name="commonSearch"]', function () {
setTimeout(archiveScheduleAlign, 150);
});
$(document).off('click.procuremenArchiveDetails', '.btn-archive-details').on('click.procuremenArchiveDetails', '.btn-archive-details', function (e) {
e.preventDefault();
var sid = $(this).data('scydgy-id') || $(this).attr('data-scydgy-id');
if (!sid) {
return;
}
var url = Fast.api.fixurl('procuremen/details?ids=' + encodeURIComponent(String(sid)));
Backend.api.open(url, '详情', {area: ['100%', '100%']});
});
$(document).off('click.procuremenArchiveAbandon', '#btn-archive-abandon').on('click.procuremenArchiveAbandon', '#btn-archive-abandon', function (e) {
e.preventDefault();
if (!archiveCan('auditabandon') && !archiveCan('archiveabandon')) {
Toastr.error('无重新下发权限');
return;
}
var sel = table.bootstrapTable('getSelections') || [];
if (!sel.length) {
Toastr.warning('请先勾选至少 1 条订单');
return;
}
var idSet = {};
var rowsHtml = ''
+ '| 订单号 | 工序名称 | 印件名称 |
';
$.each(sel, function (i, row) {
var sid = row && row.scydgy_id;
if (sid != null && sid !== '' && String(sid) !== '0') {
idSet[String(sid)] = true;
}
rowsHtml += '| ' + archiveEscHtml(String(row.CCYDH || '').trim() || '—') + ' | '
+ '' + archiveEscHtml(String(row.CGYMC || '').trim() || '—') + ' | '
+ '' + archiveEscHtml(String(row.CYJMC || '').trim() || '—') + ' |
';
});
rowsHtml += '
';
var idList = Object.keys(idSet);
if (!idList.length) {
Toastr.warning('所选行无效,请重新勾选');
return;
}
var abandonMsg = ''
+ '
即将从历史存证退回供应商初选并重新下发以下 ' + idList.length + ' 个订单(历史记录会保留):
'
+ rowsHtml
+ '
提示:退回后需重新选择供应商下发。
';
Layer.confirm(abandonMsg, {
title: '重新下发确认',
area: ['560px', 'auto'],
icon: 3,
btn: ['确认重新下发', '取消']
}, function (idx) {
Layer.close(idx);
Fast.api.ajax({
url: 'procuremen/auditabandon',
type: 'POST',
data: {
scydgy_ids_json: JSON.stringify(idList),
__token__: $('input[name=\'__token__\']').val() || Config.token
}
}, function () {
table.bootstrapTable('refresh');
return false;
});
});
});
},
api: {
bindevent: function () {
Form.api.bindevent($("form[role=form]"));
}
}
};
return Controller;
});