<script>
    layui.use(['table', 'form', 'element', 'upload', 'layer', 'Split', 'admin', 'xmSelect'], function () {
        let $ = layui.jquery;
        let Split = layui.Split;
        let table = layui.table;
        let upload = layui.upload;
        let form = layui.form;
        let admin = layui.admin;
        let xmSelect = layui.xmSelect;
        let initCondition = {source_type: 'all'};
        let whereCondition = initCondition;
        let type = 'all';

        $(document).on("click", ".layui-table-body table.layui-table tbody tr", function () {
            let index = $(this).attr('data-index');
            let tableBox = $(this).parents('.layui-table-box');
            let tableDiv = null;
            if (tableBox.find(".layui-table-fixed.layui-table-fixed-l").length > 0) {
                tableDiv = tableBox.find(".layui-table-fixed.layui-table-fixed-l");
            } else {
                tableDiv = tableBox.find(".layui-table-body.layui-table-main");
            }
            let checkCell = tableDiv.find("tr[data-index=" + index + "]").find("td div.laytable-cell-checkbox div.layui-form-checkbox I");
            if (checkCell.length > 0) {
                checkCell.click();
            }
        });


        $(document).on("click", "td div.laytable-cell-checkbox div.layui-form-checkbox", function (e) {
            e.stopPropagation();
        });

        //监听复选框事件,被选中的行高亮显示
        table.on('checkbox(supplierExaminationList)', function (obj) {
            //拉黑就不用变色了
            if (obj.data.status === -3) {
                return
            }
            if (obj.checked === true && obj.type === 'all') {
                //点击全选,拉黑的不用选上
                $('.layui-table-body table.layui-table tbody tr:not(.block-class)').addClass('layui-table-click');
                $('.layui-table-body table.layui-table tbody').find('.block-class').find('.layui-form-checkbox').remove();
                $('.layui-table-body table.layui-table tbody tr .block-class').addClass('layui-table-click');
            } else if (obj.checked === false && obj.type === 'all') {
                //点击全不选
                $('.layui-table-body table.layui-table tbody tr').removeClass('layui-table-click');
            } else if (obj.checked === true && obj.type === 'one') {
                //点击单行
                if (obj.checked === true) {
                    obj.tr.addClass('layui-table-click');
                } else {
                    obj.tr.removeClass('layui-table-click');
                }
            } else if (obj.checked === false && obj.type === 'one') {
                //点击全选之后点击单行
                if (obj.tr.hasClass('layui-table-click')) {
                    obj.tr.removeClass('layui-table-click');
                }
            }
        });

        let cols = [
            {type: 'checkbox'},
            {field: 'id', title: 'ID', align: 'center', width: 80},
            {field: 'order_sn', title: '销售订单号', align: 'center', width: 150},
            {field: 'purchase_sn', title: '采购订单号', align: 'center', width: 150},

            {
                field: 'examine_time', title: '检货时间', align: 'center', width: 130
            },
            {field: 'sales_name', title: '销售', align: 'center', width: 100},
            {field: 'sales_department', title: '销售部门', align: 'center', width: 100},
            {field: 'purchase_name', title: '采购员', align: 'center', width: 100},
            {field: 'purchase_department', title: '采购部门', align: 'center', width: 100},
            {field: 'ticket_type', title: 'A/B单', align: 'center', width: 100},
            {
                field: 'supplier_name', title: '供应商', align: 'center', width: 200
            },
            {field: 'sku_name', title: '型号', align: 'center', width: 160},
            {field: 'brand_name', title: '品牌', align: 'center', width: 160},
            {field: 'amount', title: '数量', align: 'center', width: 80},
            {field: 'batch', title: '批次', align: 'center', width: 80},
            {field: 'producing_area', title: '产地', align: 'center', width: 80},
            {field: 'stock_in_date', title: '入库日期', align: 'center', width: 80},
            {field: 'income_sn', title: '来货单号', align: 'center', width: 80},
            {field: 'delivery_sn', title: '送货单', align: 'center', width: 80},
            {field: 'tally_request', title: '理货要求', align: 'center', width: 80},
            {field: 'examine_request', title: '验货要求', align: 'center', width: 100},
            {field: 'abnormal_level', title: '异常等级', align: 'center', width: 80},
            {field: 'unhealthy_content', title: '不良现象', align: 'center', width: 150},
            {field: 'examine_result', title: '检验结果', align: 'center', width: 150},
            {field: 'remark', title: '备注', align: 'center', width: 150},
        ];
        let currentPage = 0;
        let url = '/api/supplier_examination/GetSupplierExaminationList';
        if (getQueryVariable('supplier_id')) {
            url = '/api/supplier_examination/GetSupplierExaminationList?supplier_id=' + getQueryVariable('supplier_id');
        }
        table.render({
            elem: '#supplierExaminationList'
            , url: url
            , method: 'post'
            , size: 'sm'
            , limit: 20
            , height: 600
            , cellMinWidth: 50 //全局定义常规单元格的最小宽度
            , where: whereCondition
            , loading: true
            , first: true //不显示首页
            , last: false //不显示尾页
            , cols: [cols]
            , id: 'supplierExaminationList'
            , page: {}
            , done: function (res, curr, count) {
                currentPage = curr;
            }
        });


        $("#add_supplier_examination").click(function () {
            layer.open({
                type: 2,
                content: '/supplier_examination/AddSupplierExamination?view=iframe&supplier_id=' + getQueryVariable('supplier_id'),
                area: ['80%', '90%'],
                title: '添加IQC检测记录',
                end: function () {
                    table.reload('supplierExaminationList');
                }
            });
            return false;
        });

        $("#update_supplier_examination").click(function () {
            let checkStatus = table.checkStatus('supplierExaminationList');
            let data = checkStatus.data;
            if (data.length > 1) {
                layer.msg('该操作不支持多选', {icon: 5});
                return;
            }
            if (!data.length) {
                layer.msg('请先选择要操作的记录', {icon: 5});
            } else {
                let id = data[0].id;
                layer.open({
                    type: 2,
                    content: '/supplier_examination/UpdateSupplierExamination?view=iframe&id=' + id + '&supplier_id=' + getQueryVariable('supplier_id'),
                    area: ['80%', '90%'],
                    title: '添加IQC检测记录',
                    end: function () {
                        table.reload('supplierExaminationList');
                    }
                });
            }
            return false;
        });

        //启用
        $("#delete_supplier_examination").click(function () {
            let checkStatus = table.checkStatus('supplierExaminationList');
            let data = checkStatus.data;
            if (!data.length) {
                layer.msg('请先选择要操作的检测数据', {icon: 5})
            } else {
                layer.confirm('是否删除所选记录?', function (index) {
                    let ids = [];
                    $.each(data, function (index, value) {
                        ids.push(value.id);
                    });
                    ids = ids.join(',');
                    let res = ajax('/api/supplier_examination/DeleteSupplierExaminations', {ids: ids})
                    if (res.err_code === 0) {
                        table.reload('supplierExaminationList');
                        layer.closeAll();
                        layer.msg(res.err_msg, {icon: 6})
                    } else {
                        layer.msg(res.err_msg, {icon: 5})
                    }
                });
            }
        });

        form.on('submit(load)', function (data) {
            whereCondition = $.extend(false, initCondition, data.field);
            //执行重载
            table.reload('supplierExaminationList', {
                page: {
                    curr: 1
                }
                , where: whereCondition
            });
            return false;
        });

        form.on('submit(reset)', function (data) {
            layer.load(1);
            location.reload();
        });


        //执行实例
        var uploadInst = upload.render({
            elem: '#import_supplier_examination', //绑定元素
            url: '/api/supplier_examination/ImportSupplierExamination', //上传接口
            auto: true,
            exts: 'csv',
            before: function (obj) { //obj参数包含的信息,跟 choose回调完全一致,可参见上文。
                layer.msg('上传中', {icon: 16}); //上传loading
            },
            done: function (res) {
                if (!res) {
                    return layui.msg('上传失败', {icon: 5});
                }
                if (res.err_code === 0) {
                    layer.msg(res.err_msg, {icon: 6});
                    table.reload('supplierExaminationList');
                } else {
                    layer.msg(res.err_msg, {icon: 5});
                }
            },
            error: function () {
                return layer.msg('上传失败', {icon: 5});
            }
        });

    });
</script>