<script>
    layui.use(['table', 'form', 'element', 'layer', 'Split', 'admin'], function () {
        let $ = layui.jquery;
        let Split = layui.Split;
        // 水平分割,需要分割的元素(id)、默认大小(百分比)、最小值(单位px)
        Split(['#s1', '#s2', '#s3', '#s4', '#s5'], {sizes: [9, 12, 12, 12, 55], minSize: 70});
        let table = layui.table;
        let form = layui.form;

        let initCondition = {source_type: 'all'};
        let whereCondition = initCondition;
        let type = 'all';

        //点击罗盘筛选
        $('.main_filter').click(function () {
            $('.main_filter').attr('class', 'main_filter');
            $(this).attr('class', 'main_filter layui-badge layui-bg-green');
            type = $(this).attr('id');
            whereCondition.source_type = type
            table.reload('list', {
                page: {
                    curr: 1
                }
                , where: whereCondition
            });
        });
        let cols = [
            {type: 'radio'},
            {field: 'supplier_id', title: '供应商ID', align: 'center', width: 80},
            {
                field: 'supplier_code', title: '供应商编码', align: 'center', width: 90, templet: function (data) {

                    return "<a ew-href='/supplier/SupplierDetail?view=iframe&supplier_id=" + data.supplier_id +
                        "' style='color: dodgerblue' ew-title='供应商详情 - " + data.supplier_code + "'>" + data.supplier_code + "</a>"
                }
            },
            {field: 'supplier_name', title: '供应商名称', align: 'center', width: 270},
            {field: 'supplier_group', title: '供应商性质', align: 'center', width: 120},
            {
                field: 'stockup_type', title: '合作类型', align: 'center', width: 120, templet: function (data) {
                    return "<span title='" + data.stockup_type + "'>" + data.stockup_type + "</span>"
                }
            },
            {
                field: 'contact_num', title: '联系人', align: 'center', width: 70, templet: function (data) {
                    return "<a ew-href='/supplier/SupplierDetail?view=iframe&tab=contact&supplier_id=" + data.supplier_id +
                        "' style='color: dodgerblue' ew-title='供应商详情 - " + data.supplier_code + "' title='点击跳转查看联系人列表'>" + data.contact_num + "</a>"
                }
            },

            {field: 'has_sku', title: 'SKU上传', align: 'center', width: 80},
            {
                field: 'status_name', title: '状态', align: 'center', width: 80, templet: function (data) {
                    if (data.status === 3) {
                        return "<span style='color: red' title='" + data.reject_reason + "'>" + data.status_name + "</span>"
                    } else {
                        return data.status_name;
                    }
                }
            },
            {field: 'channel_username', title: '采购员', align: 'center',width: 130},
            {field: 'purchase_username', title: '渠道开发员', align: 'center', width: 120},
            {field: 'create_name', title: '创建人', align: 'center', width: 80},
            {field: 'update_time', title: '最近修改时间', align: 'center', width: 160},
            {field: 'create_time', title: '创建时间', align: 'center', width: 150},
        ];

        @if(checkPerm('ViewFakeSupplier'))
        cols.push({
            field: 'is_type', title: '正式供应商', align: 'center', width: 110, templet: function (data) {
                return data.is_type === 0 ? '正式' : '<span style="color: red">竞调</span>';
            }
        })
        @endif

        table.render({
            elem: '#list'
            , url: '/api/supplier/GetSupplierList'
            , method: 'post'
            , size: 'sm'
            , limit: 20
            , cellMinWidth: 50 //全局定义常规单元格的最小宽度
            , where: whereCondition
            , loading: true
            , first: true //不显示首页
            , last: false //不显示尾页
            , cols: [cols]
            , id: 'list'
            , page: {}
        });

        //新增供应商弹窗
        $("#add_supplier").click(function () {
            layer.open({
                type: 2,
                content: '/supplier/AddSupplier?view=iframe',
                area: ['1000px', '97%'],
                title: '新增供应商',
                offset: ['10px'],
                end: function () {
                    table.reload('list');
                    supplierStatistics();
                }
            });
        })

        //审批供应商弹窗
        $("#audit_supplier").click(function () {
            let checkStatus = table.checkStatus('list');
            let data = checkStatus.data;
            if (!data.length) {
                layer.msg('请先选择要操作的供应商', {icon: 5})
            } else {
                let supplierId = data[0].supplier_id;
                let status = data[0].status;
                if (status !== 1 && status !== -1) {
                    layer.msg('该供应商已经被审核', {icon: 5})
                    return
                }
                if (status === -1) {
                    layer.msg('该供应商联系人未补全', {icon: 5});
                    return
                }
                layer.open({
                    type: 2,
                    content: '/supplier/AuditSupplier?view=iframe&supplier_id=' + supplierId,
                    area: ['800px', '50%'],
                    title: '审核供应商',
                    end: function () {
                        table.reload('list');
                        supplierStatistics();
                    }
                });
            }
        })

        //分配渠道员
        $("#allocate_purchase_user").click(function () {
            let checkStatus = table.checkStatus('list');
            let data = checkStatus.data;
            if (!data.length) {
                layer.msg('请先选择要操作的供应商', {icon: 5})
            } else {
                const status = data[0].status;
                if ((status === 3 || status === 2 || status === -3)) {
                    let supplierId = data[0].supplier_id;
                    layer.open({
                        type: 2,
                        content: '/supplier/AllocatePurchaseUser?view=iframe&supplier_id=' + supplierId,
                        area: ['600px', '70%'],
                        title: '配置渠道开发员',
                        end: function () {
                            table.reload('list');
                            supplierStatistics();
                        }
                    });
                } else {
                    layer.msg('只能对已通过/未通过/禁用状态的供应商进行渠道员分配', {icon: 5})
                    return false;
                }

            }
        })


        $("#disable_supplier").click(function () {
            let checkStatus = table.checkStatus('list');
            let data = checkStatus.data;
            if (!data.length) {
                layer.msg('请先选择要操作的供应商', {icon: 5})
            } else {
                const status = data[0].status;
                const hasSku = data[0].sku_num;
                if ((status === 3 || status === 2) && !hasSku) {
                    layer.confirm('对应供应商设为禁用并且审核通过后,猎芯将无法与其进行交易,如要再次启用,则须再次走入驻流程,是否执行当前操作?', function (index) {
                        let supplierId = data[0].supplier_id;
                        let res = ajax('/api/supplier/DisableSupplier', {supplier_id: supplierId})
                        if (res.err_code === 0) {
                            table.reload('list')
                            supplierStatistics();
                            layer.closeAll();
                            layer.msg(res.err_msg, {icon: 6})
                        } else {
                            layer.msg(res.err_msg, {icon: 5})
                        }
                    });
                } else {
                    layer.msg('只有已通过或者未通过状态,并且没有sku的供应商才可以禁用', {'icon': 5});
                }

            }
        });
        $("#change_supplier_is_type").click(function () {
            let checkStatus = table.checkStatus('list');
            let data = checkStatus.data;
            if (!data.length) {
                layer.msg('请先选择要操作的供应商', {icon: 5})
            } else {
                const isType = data[0].is_type;
                if (isType === 0) {
                    layer.msg('该供应商已经是正式供应商', {'icon': 5});
                    return
                }
                layer.confirm('如果转成正式供应商,会进入待审核状态,需要补全相关信息申请审核,确定要转正该供应商吗?', function (index) {
                    let supplierId = data[0].supplier_id;
                    let res = ajax('/api/supplier/ChangeSupplierIsType', {supplier_id: supplierId, is_type: 0})
                    if (res.err_code === 0) {
                        table.reload('list')
                        supplierStatistics();
                        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('list', {
                page: {
                    curr: 1
                }
                , where: whereCondition
            });
            supplierStatistics();
            return false;
        });

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

    });
</script>