<script>
    layui.use(['table', 'form', 'element', 'layer'], function () {
        var table = layui.table;
        var form = layui.form;
        table.render({
            elem: '#list'
            , url: '/api/supplier/GetSupplierList'
            , method: 'post'
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度
            , where: {
                status: 1
            }
            , size: 'sm'
            , limit: 8
            , loading: true
            , first: true //不显示首页
            , last: false //不显示尾页
            , cols: [[
                {field: 'supplier_code', title: '供应商编码', align: 'center'},
                {field: 'supplier_name', title: '供应商名称', align: 'center'},
                {field: 'supplier_group', title: '供应商性质', align: 'center'},
                {field: 'stockup_type', title: '合作类型', align: 'center'},
                {field: 'channel_username', title: '渠道开发员', align: 'center'},
                {field: 'create_name', title: '创建人', align: 'center'},
                {field: 'create_time', title: '创建时间', align: 'center'},
                {field: 'status_name', title: '状态', align: 'center'},
                {field: 'operate', title: '操作', templet: '#operate', align: 'center'},
            ]]
            , id: 'SupplierList'
            , page: false
        });

        form.on('submit(load)', function (data) {
            form.render();
            //执行重载
            table.reload('SupplierList', {
                page: {
                    curr: 1
                }
                , where: data.field
            });
            return false;
        });

        table.on('tool(list)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;
            let datas = {
                group_id: data.group_id,
                id: data.id
            };
            if (layEvent === 'edit') {
                window.location = '/supplier'
            } else if (layEvent === 'Del') {
                datas.status = 3;
                up_status(datas, '删除');
            }

        });

        function up_status(data, info) {
            layer.confirm('确定' + info + '组成员吗', {
                title: info + '组成员'
                , btn: ['确定', '取消']
            }, function () {
                layer.closeAll('dialog'); // 确定时关闭弹框
                let res = ajax('/api/save_group_user', data);
                if (!res) {
                    return layer.msg('禁用失败', {icon: 6});
                } else {
                    layer.msg(res.errmsg, {icon: 6});
                    $('.load').click();
                }
            }, function () {
                layer.close();
            });
        }


        //我的供应商----------------------------------------------------
        table.render({
            elem: '#own'
            , url: '/api/supplier/GetSupplierList'
            , method: 'post'
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度
            , where: {
                source_type: 'own'
            }
            , limit: 8
            , loading: true
            , first: true //不显示首页
            , last: false //不显示尾页
            , cols: [[
                {field: 'supplier_code', title: '供应商编码', align: 'center', width: 120},
                {field: 'supplier_name', title: '供应商名称', align: 'center'},
                {field: 'supplier_group', title: '供应商性质', align: 'center', width: 120},
                {field: 'stockup_type', title: '合作类型', align: 'center', width: 100},
                {field: 'channel_username', title: '采购员', align: 'center', width: 130},
                {field: 'create_time', title: '创建时间', align: 'center', width: 170},
                {field: 'purchase_username', title: '渠道开发员', align: 'center', width: 130},
                {field: 'audit_time', title: '审批时间', align: 'center', width: 170},
                {
                    field: 'status_name', title: '状态', align: 'center', width: 80, templet: function (data) {
                        if (data.status_name === "驳回") {
                            return "<span style='color: red' title='" + data.reject_reason + "'>" + data.status_name + "</sspan>";
                        }
                        return data.status_name;
                    }
                },
                {field: 'operate', title: '操作', templet: '#operate_own', align: 'center', width: 180},
            ]]
            , id: 'SupplierList'
            , page: false
        });

        form.on('submit(load)', function (data) {
            form.render();
            //执行重载
            table.reload('SupplierList', {
                page: {
                    curr: 1
                }
                , where: data.field
            });
            return false;
        });
        table.on('tool(own)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;
            if (layEvent === 'draft') {
                let map = {
                    supplier_id: data.supplier_id,
                    status: -1
                };
                updateStatus(map, '转为草稿');
            }
        });

    });

</script>