/***********************************************DATA_STRUCT_
g_viewmode: global variable decide view param
mixed: if value is not null, item is product, has materials
merge: combine same value
subinfo: array of child indexs
subinfoclass: css class for span contain subinfo
parent:index of val in vals array containt parent id (first in vals array value is id)
visible: 0-hide, 1: show, 2: show in row 2, 3: hien thi trong form edit/add
fordisplay: array {id,name}, gia tri thuc la id, hien thi la name
rowheading_visible: xac dinh gia tri visible nao lam tieu de (1/2), dung cho hien thi nhieu dong - heading mang dien giai dong <>0
type:text,number,check,select
highlight: column cuoi co name=hightlight,type=highlight, gia tri=1 se duoc highlight (unread)
*/
var ds_contact_c = [
    { name: "id", head: "", visible: 0 },
    { name: "name", head: "Tên đối tác", visible: 1, type: "text", required: 1, width: ["100%", "45%"], subinfo: (g_viewmode == 0) ? [3, 2] : null, maininfoclass: "bb" },
    { name: "phone", head: "Điện thoại", visible: [0, 1], type: "text", required: 0, width: "25%" },
    { name: "address", head: "Địa chỉ", visible: [0, 1], type: "text", required: 0, width: "30%" },
];
var ds_contact_combo = [
    { name: "id", head: "", visible: 0 },
    { name: "name", head: "Tên đối tác", visible: 1, type: "text", required: 1, width: "100%", subinfo: [3, 2], maininfoclass: "bb" },
    { name: "phone", head: "Điện thoại", visible: 0, type: "text", required: 0, width: "" },
    { name: "address", head: "Địa chỉ", visible: 0, type: "text", required: 0, width: "" },
];
var ds_contact = [
    { name: "id", head: "", visible: 0 },
    { name: "name", head: "Tên đối tác", visible: 1, type: "text", required: 1, width: "40%" },
    { name: "code", head: "Mã", visible: 0, type: "text", required: 0 },
    { name: "address", head: "Địa chỉ", visible: 1, type: "text", required: 0, width: "40%" },
    { name: "phone", head: "Điện thoại", visible: 1, type: "text", required: 0, width: "20%" },
    { name: "birth", head: "Ngày sinh", visible: 3, type: "text", required: 0, prompt: "(ngày/tháng/năm, ví dụ: 20/12/2000)" },
    { name: "email", head: "Email", visible: 2, type: "text", required: 0 },
    { name: "mst", head: "Mã số thuế", visible: 2, type: "text", required: 0 },
    { name: "cmnd", head: "Số căn cước", visible: 2, type: "text", required: 0 },
    { name: "bank", head: "Số tài khoản", visible: 2, type: "text", required: 0 },
    { name: "type", head: "Kiểu đối tác", visible: 2, type: "select", required: 1, data: g_list_contacttype },
    { name: "sex", head: "Giới tính", visible: 2, type: "select", required: 0, data: g_list_gender },
    { name: "comment", head: "Ghi chú", visible: 2, type: "text", required: 0 },
];

var ds_contacts_trans_goods = [
    { name: "goods_name", type: "text", visible: 1, width: "70%" },
    { name: "quantity", type: "number", visible: 1, width: "30%", subinfo: [2, 3] },
    { name: "unit", type: "text", visible: 0, subinfobreak: " " },
    { name: "unit_price", type: "number", visible: 0, subinfobreak: " * " },
];
var ds_contact_trans = [
    { name: "id", head: "", visible: 0 },
    { name: "date", head: "Ngày", type: "text", visible: 1, width: ["25%", "10%"], subinfo: (g_viewmode == 0) ? [3] : null, maininfoclass: "bb", subinfoclass: "subinfo" },
    { name: "reason", head: "Lý do", type: "text", visible: 1, width: ["20%", "8%"], fordisplay: g_list_reason },
    { name: "no", head: "Số phiếu", type: "text", visible: [0, 1], width: "10%" },
    { name: "comment", head: "Ghi chú", type: "text", visible: [0, 1], width: "24%" },
    { name: "totalcost", head: "Tổng", type: "number", visible: 1, width: ["25%", "12%"], subinfo: (g_viewmode == 0) ? [7] : null, class: "bg", subinfoclass: "subinfo" },
    { name: "pay", head: "Thanh toán", type: "number", visible: [0, 1], width: "12%" },
    { name: "debit", head: "Ghi nợ", type: "number", visible: [0, 1], width: "12%", class: "br" },
    { name: "mustreceive", head: "Phải thu", type: "number", visible: 1, width: ["30%", "12%"], class: "bp" },
    { name: "goods", head: "Tên hàng", type: "text", visible: 1, rowsub: 1, rowsub_ds: ds_contacts_trans_goods, width: "100%" },
];

var ds_goods_category = [
    { name: "id", head: "", visible: 0 },
    { name: "name", head: "Tên nhóm", visible: 1, editable: 1, type: "text", required: 1, width: "55%" },
    { name: "code", head: "Mã nhóm", visible: 1, editable: 1, type: "text", required: 0, width: "20%" },
    { name: "discount", head: "% CK", visible: 1, editable: 1, type: "number", required: 0, width: "25%" },
];

var ds_goods_categoryls = [
    { name: "category_id", head: "", visible: 0 },
    { name: "category_name", head: "Tên nhóm", visible: 1, type: "text", required: 1 },
    { name: "category_code", head: "Mã nhóm", visible: 0, type: "text", required: 0 },
    { name: "default_discount", head: "%.CK", visible: 0, type: "number", required: 0 },
    { name: "res_doan", head: "Đồ ăn?", visible: 1, type: "check", required: 0, width: "15%" },
    { name: "res_douong", head: "Đồ uống?", visible: 1, type: "check", required: 0, width: "15%" },
];

//data - array, truyen duoi dang ten bien chua array va dung eval khi can du lieu de lay du lieu thay doi
//neu chi la ten bien thi data se chi la du lieu tai thoi diem khai bao, khong cap nhat thay doi
//goods_id,unit_id,category_id
var ds_goods = [
    { name: "id", head: "", visible: 0, idsub: 1 },
    { name: "idunit", head: "", visible: 0 },
    { name: "idnhom", head: "Nhóm hàng", visible: 3, type: "select", required: 1, data: "g_list_goodscategory" },
    { name: "tenhang", head: "Tên hàng", visible: 1, type: "text", required: 1, merge: 1, editable: 1, width: ["100%", "40%"] },
    { name: "mahang", head: "Mã hàng", visible: [3, 1], type: "text", required: 0, editable: 1, width: "10%" },
    { name: "dvt", head: "ĐVT", visible: [3, 1], type: "text", required: 0, editable: 1, width: "10%" },
    { name: "giale", head: "Giá lẻ", visible: 2, type: "number", required: 0, editable: 1, width: ["25%", "10%"] },
    { name: "giabuon", head: "Giá buôn", visible: 2, type: "number", required: 0, editable: 1, width: ["25%", "10%"] },
    { name: "gianhap", head: "Giá nhập", visible: 3, type: "number", required: 0 },
    { name: "nguyenlieu", head: "Nguyên liệu?", visible: 3, type: "check", required: 0, },
    { name: "thanhpham", head: "Thành phẩm?", visible: 3, type: "check", required: 0 },
    { name: "ghichu", head: "Ghi chú", visible: [2, 1], type: "text", required: 0, editable: 1, width: ["50%", "20%"] },
    { name: "continued", head: "", visible: 0, type: "number", required: 0 },
];
var ds_goods_web = [
    { name: "goodsid", head: "", visible: 0, idsub: 1 },
    { name: "unitid", head: "", visible: 0 },
    { name: "categoryid", head: "Nhóm hàng", visible: 3, type: "select", required: 1, data: "g_list_goodscategory" },
    { name: "goodsname", head: "Tên hàng", visible: 1, type: "text", required: 1, merge: 1, editable: 1, width: ["54%", "15%"] },
    { name: "goodsnameweb", head: "Tên hàng online", visible: 1, type: "text", required: 1, merge: 1, editable: 1, width: ["46%", "25%"] },
    { name: "goodscode", head: "Mã hàng", visible: [0, 1], type: "text", required: 0, editable: 0, width: "8%" },
    { name: "unit", head: "ĐVT", visible: [0, 1], type: "text", required: 0, editable: 1, width: "6%" },
    { name: "exportprice", head: "Giá bán", visible: [2, 1], type: "number", required: 0, editable: 1, width: ["27%", "8%"] },
    { name: "exportpricesale", head: "Giá sale", visible: [2, 1], type: "number", required: 0, editable: 1, width: ["27%", "8%"] },
    { name: "online", head: "Online?", visible: [2, 1], type: "check", editable: 0, required: 0, width: ["20%", "5%"] },
    { name: "thumb", head: "Ảnh", visible: [2, 1], type: "image", editable: 0, required: 0, width: ["26%", "5%"] },
    { name: "comment", head: "Ghi chú", visible: [0, 1], type: "text", required: 0, editable: 1, width: "20%" },
    { name: "continued", head: "", visible: 0, type: "number", required: 0 },
];
var ds_goods_exim = [
    { name: "id", head: "", visible: 0, parent: 14 },
    { name: "goods_id", head: "", visible: 0 },
    { name: "unit_id", head: "", visible: 0 },
    { name: "name", head: "Tên hàng", visible: 1, type: "text", required: 1, merge: 0, width: ["75%", "38%"] },
    { name: "code", head: "Mã hàng", visible: 0, type: "text", required: 0, merge: 0 },
    { name: "unit_name", head: "ĐVT", visible: 1, type: "text", required: 0, width: ["25%", "8%"] },
    { name: "price", head: "Đơn giá", visible: [2, 1], type: "number", required: 0, editable: 1, width: ["25%", "15%"] },
    { name: "quantity", head: "SL", visible: [2, 1], type: "number", required: 1, editable: 1, width: ["15%", "8%"] },
    { name: "discount", head: "%CK", visible: [2, 1], type: "number", required: 0, editable: 1, width: ["15%", "7%"] },
    { name: "serial", head: "Serial", visible: [2, 1], type: "text", required: 0, editable: 1, width: ["20%", "10%"] },
    { name: "warranty", head: "", visible: 0, type: "number", required: 0, editable: 1 },
    { name: "extra_id", head: "", visible: 0, type: "number", required: 0, },
    { name: "color_name", head: "Màu", visible: [2, 1], type: "text", required: 0, editable: 1, width: ["25%", "14%"], subinfobreak: "&nbsp;", class: "divcell_color" },
    { name: "template_id", head: "", visible: 0, type: "number", required: 0, mixed: 1 },
    { name: "parent_id", head: "", visible: 0, type: "number", required: 0, },
    { name: "quantity_max", head: "", visible: 0, type: "number", required: 0, },
];

var ds_goods_exim_storeswap = [
    { name: "id", head: "", visible: 0 },
    { name: "goods_id", head: "", visible: 0 },
    { name: "unit_id", head: "", visible: 0 },
    { name: "name", head: "Tên hàng", visible: 1, type: "text", required: 1, merge: 0, width: "45%" },
    { name: "code", head: "Mã hàng", visible: 0, type: "text", required: 0, merge: 0 },
    { name: "unit_name", head: "ĐVT", visible: 1, type: "text", required: 0, width: "15%" },
    { name: "quantity", head: "SL", visible: 1, type: "number", required: 1, editable: 1, width: "15%" },
    { name: "serial", head: "Serial", visible: 1, type: "text", required: 0, editable: 1, width: "25%" },
];
var ds_goods_unit = [
    { name: "unit_id", head: "", visible: 0 },
    { name: "tendvt", head: "Tên ĐVT", visible: 1, type: "text", required: 1 },
    { name: "madvt", head: "Mã phụ", visible: 1, type: "text", required: 0 },
    { name: "heso", head: "Hệ số", visible: 1, type: "text", required: 1 },
    { name: "giale", head: "Giá lẻ", visible: 1, type: "number", required: 0 },
    { name: "giabuon", head: "Giá buôn", visible: 1, type: "number", required: 0 },
    { name: "gianhap", head: "Giá nhập", visible: 1, type: "number", required: 0 },
];

var ds_goodscallqueue = [
    { name: "id", head: "", visible: 0 },
    { name: "obj_id", head: "", visible: 0 },
    { name: "obj_name", head: "Bàn/phòng", visible: 1, type: "text", required: 1, width: "20%" },
    { name: "goods_name", head: "Tên hàng", visible: 1, type: "text", required: 0, width: "35%" },
    { name: "quantity", head: "SL", visible: 1, type: "number", required: 0, width: "10%" },
    { name: "unit", head: "ĐVT", visible: 1, type: "text", required: 0, width: "10%" },
    { name: "status", head: "Trạng thái", visible: 0, type: "number", required: 0 },
    { name: "hourinday_do", head: "Thời điểm", visible: 1, type: "text", required: 0, width: "15%" },
];

var ds_goods_mix_template = [
    { name: "id", head: "", visible: 0 },
    { name: "name", head: "", visible: 1, type: "text", required: 1, width: "100%" },
];

var ds_goods_mix_material = [
    { name: "id", head: "", type: "text", visible: 0 },
    { name: "goods_child_id", head: "", visible: 0, type: "text", required: 0, editable: 1, width: "" },
    { name: "unit_id", head: "", visible: 0, type: "text", required: 0, editable: 1, width: "" },
    { name: "goods_name", head: "Tên hàng thành phần", visible: 1, type: "text", required: 0, editable: 1, width: (g_viewmode == 0) ? "50%" : "29%" },
    { name: "unit", head: "ĐVT", visible: (g_viewmode == 0) ? 0 : 1, type: "text", required: 0, editable: 1, width: "8%" },
    { name: "quantity", head: "SL.cần", visible: 1, type: "number", required: 0, editable: 1, width: (g_viewmode == 0) ? "15%" : "6%", class: "bb" },
    { name: "quantity_stock", head: "SL.Tồn", visible: 1, type: "number", required: 0, editable: 1, width: (g_viewmode == 0) ? "15%" : "6%", class: "bg" },
    { name: "price_average", head: "Giá nhập/vốn", visible: (g_viewmode == 0) ? 0 : 1, type: "number", required: 0, editable: 1, width: "9%" },
    { name: "unitpriceautoget", head: "Tính giá?", visible: (g_viewmode == 0) ? 0 : 1, type: "check", required: 0, editable: 1, width: "9%" },
    { name: "label", head: "Loại", visible: 1, type: "text", required: 1, width: (g_viewmode == 0) ? "20%" : "33%" },
    { name: "category_id", head: "Nhóm hàng", visible: 0, type: "number", required: 0, editable: 1, width: "" },
];


var ds_goods_stock = [
    { name: "id", head: "", visible: 0, type: "text" },
    { name: "goods_id", head: "", visible: 0 },
    { name: "unit_id", head: "", visible: 0 },
    { name: "tenhang", head: "Tên hàng", visible: 1, type: "text", required: 1, merge: 0, subinfo: (g_viewmode == 0) ? [8, 4] : [8], subinfoclass: "subinfo italic", width: ["70%", "50%"] },
    { name: "mahang", head: "Mã hàng", visible: [0, 1], type: "text", required: 0, merge: 1, width: "15%", subinfoclass: "goods_code" },
    { name: "dvt", head: "ĐVT", visible: [0, 1], type: "text", required: 0, subinfobreak: "/", width: "10%" },
    { name: "ton", head: "Tồn", visible: 1, type: "number", required: 1, editable: 0, subinfo: (g_viewmode == 0) ? [7, 5] : null, subinfoclass: "subinfo italic", width: ["30%", "10%"], class: "bg" },
    { name: "dongia", head: "Đơn giá", visible: [0, 1], type: "number", required: 0, editable: 1, width: "15%" },
    { name: "mausac", head: "Màu sắc", visible: 0, type: "text", required: 0, editable: 0, width: "0%", subinfobreak: " ", subinfoclass: "goods_color" },
    { name: "mausacid", head: "", visible: 0, type: "text", required: 0, editable: 0 },
    { name: "kichthuoc", head: "Kích thước", visible: 0, type: "text", required: 0, editable: 0 },
    { name: "anh", head: "Hình ảnh", visible: 0, type: "text", required: 0, editable: 0 },
    { name: "template_id", head: "", visible: 0, type: "number", required: 0, editable: 0, mixed: 1 },
];

var ds_goods_stock_exim = [
    { name: "id", head: "", visible: 0, type: "text" },
    { name: "date", head: "Ngày", visible: 1, type: "text", required: 1, width: "25%" },
    { name: "no", head: "Số phiếu", visible: 1, type: "text", required: 1, width: "25%" },
    { name: "import_quantity", head: "SL nhập", visible: 1, type: "number", required: 1, width: "15%" },
    { name: "export_quantity", head: "SL xuất", visible: 1, type: "number", required: 1, width: "15%" },
    { name: "dongia", head: "Đơn giá", visible: 1, type: "number", required: 0, width: "20%" },
    { name: "ghichuhang", head: "Ghi chú hàng", visible: 0, type: "number", required: 0, width: "", rowsub: 2 },
    { name: "tendoitac", head: "Tên đối tác", visible: 0, type: "text", required: 0, width: "", rowsub: 1 },
    { name: "diachi", head: "Địa chỉ", visible: 0, type: "text", required: 0, width: "", rowsub: 1 },
    { name: "dienthoai", head: "Điện thoại", visible: 0, type: "text", required: 0, width: "", rowsub: 2 },
    { name: "ghichuphieu", head: "Ghi chú phiếu", visible: 0, type: "text", required: 0, width: "", rowsub: 1 },
];

var ds_employee = [
    { name: "id", head: "", visible: 0 },
    { name: "name", head: "Tên", visible: 1, type: "text", required: 1 },
]

var ds_employee_c = [
    { name: "id", head: "", visible: 0 },
    { name: "name", head: "Tên", visible: 1, type: "text", required: 1, width: "30%" },
    { name: "phone", head: "Điện thoại", visible: 1, type: "text", merge: 0, required: 1, width: "30%" },
    { name: "comment", head: "Ghi chú", visible: 1, type: "text", required: 1, width: "40%" },
]
var ds_invoice = [
    { name: "id", head: "", visible: 0 },
    { name: "invoice_no", head: "Số phiếu", visible: 1, type: "text", required: 1, width: "30%" },
    { name: "invoice_date", head: "Ngày phiếu", visible: 1, type: "text", required: 1, width: "30%" },
    { name: "vat", head: "VAT", visible: 1, type: "text", required: 1, width: "30%" },
    { name: "invoice_money_discount", head: "Bớt tiền", visible: 1, type: "text", required: 1, width: "30%" },
    { name: "total_cost", head: "Tổng tiền", visible: 1, type: "text", required: 1, width: "30%" },
    { name: "comment", head: "Ghi chú", visible: 1, type: "text", required: 1, width: "30%" },
    { name: "money_debit", head: "Ghi nợ", visible: 1, type: "text", required: 1, width: "30%" },
    { name: "invoice_discount", head: "Chiết khấu", visible: 1, type: "text", required: 1, width: "30%" },
    { name: "store_id", head: "Kho hàng", visible: 1, type: "text", required: 1, width: "30%" },
    { name: "store_name", head: "Kho hàng", visible: 1, type: "text", required: 1, width: "30%" },
    { name: "bank_account_id", head: "Số TK", visible: 1, type: "text", required: 1, width: "30%" },
    { name: "bank_account_number", head: "Số TK", visible: 1, type: "text", required: 1, width: "30%" },
];

var ds_invoice_print_type = [
    { name: "type" },
    { name: "size" }
]
var ds_invoice_eiv = [
    { name: "invoice_id", head: "", visible: 0 },
    { name: "invoice_no", head: "Số phiếu", visible: 1, type: "text", required: 1, width: (g_viewmode == 0) ? "30%" : "12%", subinfo: (g_viewmode > 0) ? [2] : [2, 3, 4] },
    { name: "invoice_date", head: "Ngày phiếu", visible: 0, type: "text", required: 1, subinfobreak: "<br>", class: "subinfo" },
    { name: "vat", head: "VAT", visible: 0, type: "text", required: 1, subinfobreak: "<br>", class: "subinfo bb", subfix: "%" },
    { name: "total_cost", head: "Tổng tiền", visible: (g_viewmode == 0) ? 0 : 1, type: "text", required: 1, width: "10%", subinfo: [3] },
    { name: "contact_name", head: "Tên đối tác", visible: 1, type: "text", required: 1, width: (g_viewmode == 0) ? "70%" : "40%", subinfo: (g_viewmode > 0) ? [6, 7] : [6, 7, 9, 12, 11] },
    { name: "contact_address", head: "Địa chỉ", visible: 0, type: "text", required: 1, subinfobreak: "<br>", class: "subinfo" },
    { name: "contact_person", head: "Tên người mua", visible: 0, type: "text", required: 1, subinfobreak: "<br>", class: "subinfo" },
    { name: "id_attr", head: "ID", visible: 0, type: "text", required: 1 },
    { name: "lookup_code", head: "Mã tra cứu", visible: (g_viewmode == 0) ? 0 : 1, type: "text", required: 1, width: (g_viewmode == 0) ? "38%" : "15%", subinfo: [10] },
    { name: "create_time", head: "Ngày xuất", visible: 0, type: "text", required: 1, subinfobreak: "<br>", class: "subinfo" },
    { name: "status", head: "Trạng thái", visible: (g_viewmode == 0) ? 0 : 1, type: "text", required: 1, width: "23%", subinfo: [12], tip: "Mới khỏi tạo<br>-> Đã ký số<br>-> Đã gửi CQT<br>-> Cấp mã thành công" },
    { name: "no", head: "Số HĐ", visible: 0, type: "text", required: 1, subinfobreak: "<br>", class: "subinfo bp" },
    { name: "status_val", head: "Trạng thái", visible: 0, type: "number", required: 1 },
];

var ds_obj = [
    { name: "id", head: "", visible: 0 },
    { name: "objcategory", head: "Loại bàn/phòng", visible: 2, type: "select", required: 1, data: "g_list_objcategory" },
    { name: "name", head: "Tên bàn/phòng", visible: 1, type: "text", required: 1, width: "35%" },
    { name: "order", head: "Thứ tự", visible: 1, type: "number", required: 0, width: "15%" },
    { name: "type", head: "Kiểu", visible: 1, type: "text", required: 0, readonly: 1, width: "20%" },
    { name: "typeid", head: "Kiểu", visible: 2, type: "select", required: 0, readonly: 0, data: "g_list_objtype", viewname: "type" },
    { name: "group", head: "Nhóm", visible: 1, type: "text", required: 0, readonly: 1, width: "20%" },
    { name: "groupid", head: "Nhóm", visible: 2, type: "select", required: 0, readonly: 0, data: "g_list_objgroup", viewname: "group" },
    { name: "seat", head: "Số chỗ", visible: 2, type: "number", required: 0 },
];
var ds_store_c = [
    { name: "id", head: "", visible: 0 },
    { name: "name", head: "Tên", visible: 1, type: "text", required: 1, width: "45%" },
    { name: "address", head: "Nơi/địa chỉ", visible: 1, type: "text", required: 1, width: "55%" },

];


function dsGetLastVisible(ds) {
    for (var i = (ds.length - 1); i >= 0; i--) {
        if (ds[i].visible == 1) return i;
    }
}

function dsIsVisible(ds, index) {
    if (typeof ds[index].visible === "undefined") return false;
    if (Array.isArray(ds[index].visible)) {
        if (g_viewmode == 0) return (ds[index].visible[0] > 0);//visible = 1, 2 (second row)
        return (ds[index].visible[1] == 1);
    } else {
        return (ds[index].visible == 1);
    }
}
function dsGetVisible(ds, index) {
    if (typeof ds[index].visible === "undefined") return 0;
    if (Array.isArray(ds[index].visible)) {
        if (g_viewmode == 0) return ds[index].visible[0];
        return ds[index].visible[1];
    } else {
        return (ds[index].visible);
    }
}
function dsGetType(ds, name) {
    for (var i = 0; i < ds.length; i++) {
        if (ds[i].name == name) {
            return ds[i].type;
        }
    }
    return "";
}

function dsGetWidth(ds, index) {
    if (typeof ds[index].width === "undefined") return "";
    if (Array.isArray(ds[index].width)) {
        if (g_viewmode == 0) return ds[index].width[0];
        return ds[index].width[1];
    } else {
        return (ds[index].width);
    }
}