<script>
    layui.use(['table', 'form', 'element', 'layer', 'Split', 'admin', 'xmSelect'], function () {
        let $ = layui.jquery;
        let Split = layui.Split;
        // 水平分割,需要分割的元素(id)、默认大小(百分比)、最小值(单位px)
        let table = layui.table;
        let form = layui.form;
        let admin = layui.admin;
        let xmSelect = layui.xmSelect;
        let initCondition = {source_type: 'all'};
        let whereCondition = initCondition;
        let type = 'all';

        let cols = [
            {type: 'radio'},
            {field: 'id', title: 'ID', align: 'center', width: 80},
            {
                field: 'mobile', title: '账号', align: 'center', width: 170
            },
            {field: 'password_raw', title: '密码', align: 'center', width: 170},
            {
                field: 'supplier_code', title: '供应商编码', align: 'center', width: 120
            },
            {field: 'supplier_name', title: '供应商名称', align: 'center'},
            {field: 'type_name', title: '账号类型', align: 'center'},
            {
                field: 'a_status', title: '状态', align: 'center', width: 80, templet: function (data) {
                    return data.a_status === 1 ? '启用' : "<span style='color: red'>禁用</span>"
                }
            },
            {field: 'create_time', title: '创建时间', align: 'center', width: 150},
            {field: 'update_time', title: '更新时间', align: 'center', width: 150},
        ];
        let currentPage = 0;
        table.render({
            elem: '#supplierAccountList'
            , url: '/api/supplier_account/GetSupplierAccountList'
            , method: 'post'
            , size: 'sm'
            , limit: 20
            , cellMinWidth: 50 //全局定义常规单元格的最小宽度
            , where: whereCondition
            , loading: true
            , first: true //不显示首页
            , last: false //不显示尾页
            , cols: [cols]
            , id: 'supplierAccountList'
            , page: {}
            , done: function (res, curr, count) {
                currentPage = curr;
            }
        });

        //启用
        $("#enable_supplier_account").click(function () {
            let checkStatus = table.checkStatus('supplierAccountList');
            let data = checkStatus.data;
            if (!data.length) {
                layer.msg('请先选择要操作的供应商账号', {icon: 5})
            } else {
                layer.confirm('确定要启用该供应商账号吗?', function (index) {
                    let id = data[0].id;
                    let res = ajax('/api/supplier_account/EnableSupplierAccount', {id: id})
                    if (res.err_code === 0) {
                        table.reload('supplierAccountList');
                        layer.closeAll();
                        layer.msg(res.err_msg, {icon: 6})
                    } else {
                        layer.msg(res.err_msg, {icon: 5})
                    }
                });
            }
        });

        //禁用
        $("#disable_supplier_account").click(function () {
            let checkStatus = table.checkStatus('supplierAccountList');
            let data = checkStatus.data;
            if (!data.length) {
                layer.msg('请先选择要操作的供应商账号', {icon: 5})
            } else {
                layer.confirm('确定要禁用该供应商账号吗?', function (index) {
                    let id = data[0].id;
                    let res = ajax('/api/supplier_account/DisableSupplierAccount', {id: id})
                    if (res.err_code === 0) {
                        table.reload('supplierAccountList');
                        layer.closeAll();
                        layer.msg(res.err_msg, {icon: 6})
                    } else {
                        layer.msg(res.err_msg, {icon: 5})
                    }
                });
            }
        });

        //根据供应商编码已经品牌等去基石调用接口下架
        $("#add_supplier_account").click(function () {
            layer.open({
                type: 2,
                content: '/supplier_account/AddSupplierAccount?view=iframe',
                area: ['800px', '600px'],
                title: '添加供应商账号',
                end: function () {
                    table.reload('supplierAccountList');
                    // supplierStatistics();
                }
            });
        });

        $("#update_supplier_account").click(function () {
            let checkStatus = table.checkStatus('supplierAccountList');
            let data = checkStatus.data;
            if (!data.length) {
                layer.msg('请先选择要操作的供应商', {icon: 5})
            } else {
                let id = data[0].id;
                layer.open({
                    type: 2,
                    content: '/supplier_account/UpdateSupplierAccount?view=iframe&id=' + id,
                    area: ['800px', '600px'],
                    title: '批量下架SKU',
                    end: function () {
                        table.reload('supplierAccountList');
                        // supplierStatistics();
                    }
                });
            }
        });


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

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

    });
</script>