<script>
    layui.use(['table', 'form', 'element', 'layer', 'admin'], function () {
        let table = layui.table;
        let form = layui.form;
        let admin = layui.admin;
        let element = layui.element;
        let supplierId = getQueryVariable('supplier_id');
        table.render({
            elem: '#contactList',
            url: '/api/supplier_contact/GetSupplierContactList',
            method: 'get',
            cellMinWidth: 80,//全局定义常规单元格的最小宽度
            where: {
                supplier_id: getQueryVariable("supplier_id")
            },
            loading: true,
            first: true,//不显示首页
            last: false,//不显示尾页
            cols: [[
                {
                    type: 'radio',
                },
                {
                    field: 'supplier_consignee',
                    title: '<span class="require">* </span>联系人',
                    align: 'center',
                    width: 150
                },
                {field: 'supplier_position', title: '<span class="require">* </span>职位', align: 'center', width: 100},
                {
                    field: 'supplier_email',
                    title: '<span class="require">* </span>邮箱',
                    align: 'center',
                    width: 200,
                    templet: function (data) {
                        return data.supplier_email ? '<span>' + data.supplier_email + '</span><span style="color: dodgerblue" class="viewContact" type="supplier_email" contact_id="' + data.contact_id + '">查看</span>' : '';
                    }
                },
                {
                    field: 'supplier_mobile',
                    title: '<span class="require">* </span>手机号',
                    align: 'center',
                    width: 180,
                    templet: function (data) {
                        return data.supplier_mobile ? '<span>' + data.supplier_mobile + '</span><span style="color: dodgerblue" class="viewContact" type="supplier_mobile" contact_id="' + data.contact_id + '">查看</span>' : '';
                    }
                },
                {
                    field: 'supplier_telephone',
                    title: '<span class="require">* </span>座机',
                    align: 'center',
                    width: 180,
                    templet: function (data) {
                        return data.supplier_telephone ? '<span>' + data.supplier_telephone + '</span><span style="color: dodgerblue" class="viewContact" type="supplier_telephone" contact_id="' + data.contact_id + '">查看</span></span>' : '';
                    }
                },
                {
                    field: 'supplier_qq', title: 'QQ', align: 'center', width: 180, templet: function (data) {
                        return data.supplier_qq ? '<span>' + data.supplier_qq + '</span><span style="color: dodgerblue" class="viewContact" type="supplier_qq" contact_id="' + data.contact_id + '">查看</span>' : '';
                    }
                },
                {field: 'supplier_fax', title: '传真', align: 'center', width: 140},
                {field: 'channel_name', title: '<span class="require">* </span>采购员', align: 'center', width: 110},
                {field: 'working_status', title: '采购员状态', align: 'center', width: 120},
            ]],
            id: 'contactList',
            page: {},
        });

        //新增联系方式
        $("#add_contact").click(function () {
            let supplierId = getQueryVariable('supplier_id');
            layer.open({
                type: 2,
                content: '/supplier_contact/AddSupplierContact?view=iframe&supplier_id=' + supplierId,
                area: ['600px', '525px'],
                title: '新增联系人',
                end: function () {  // 监听弹窗关闭
                    // location.href = '/supplier/UpdateSupplier?view=iframe&tab=contact&supplier_id='+supplierId;
                    table.reload('contactList');
                }
            });
        })

        //设置SKU采购
        $("#set_yunxin_channel_user").click(function () {
            let supplierId = getQueryVariable('supplier_id');
            layer.open({
                type: 2,
                content: '/supplier/SetYunxinChannelUser?view=iframe&supplier_id=' + supplierId,
                area: ['600px', '525px'],
                title: '配置线上采购员',
                end: function () {  // 监听弹窗关闭
                    table.reload('contactList');
                }
            });
        })

        //更新
        $(document).on('click', '#update_contact', function () {
            let checkStatus = table.checkStatus('contactList');
            let data = checkStatus.data;
            if (!data.size) {
                layer.msg('请先选择要操作的联系人', {icon: 5})
            }
            let contactId = data[0].contact_id;
            layer.open({
                type: 2,
                content: '/supplier_contact/UpdateSupplierContact?view=iframe&contact_id=' + contactId,
                area: ['600px', '525px'],
                title: '编辑联系人',
                end: function () {  // 监听弹窗关闭
                    // location.href = '/supplier/UpdateSupplier?view=iframe&tab=contact&supplier_id='+supplierId;
                    table.reload('contactList');
                }
            });
        });

        //删除
        $(document).on('click', '#batchDelete', function () {
            let checkStatus = table.checkStatus('contactList');
            let data = checkStatus.data;
            let contactIds = [];
            $.each(data, function (index, value) {
                contactIds.push(value.contact_id);
            });
            batchDeleteContact(contactIds);
        });

        //批量删除操作
        function batchDeleteContact(contactId) {
            layer.confirm('确定要删除所选联系方式?', function (index) {
                let resp = ajax('/api/supplier_contact/DeleteSupplierContact', {contact_id: contactId});
                if (!resp) {
                    layer.msg('网络连接失败', {'icon': 5});
                    return false;
                }
                if (resp.err_code === 0) {
                    // location.href = '/supplier/UpdateSupplier?view=iframe&tab=contact&supplier_id='+supplierId;
                    table.reload('contactList');
                    layer.closeAll();
                    layer.msg(resp.err_msg, {'icon': 6});
                } else {
                    layer.msg(resp.err_msg, {'icon': 5});
                    return false;
                }
            });
        }

        $(document).on('click', '.viewContact', function () {
            if ($(this).text() === '隐藏') {
                $(this).prev().text($(this).attr('prev_text'));
                $(this).text('查看');
            } else {
                let contactId = $(this).attr('contact_id');
                let type = $(this).attr('type');
                let resp = ajax('/api/supplier_contact/GetSupplierContact', {contact_id: contactId, type: type});
                if (!resp) {
                    layer.msg('网络连接失败', {'icon': 5});
                    return false;
                }
                let prevText = $(this).prev().text();
                $(this).attr('prev_text', prevText);
                if (resp.err_code === 0) {
                    switch (type) {
                        case 'supplier_email':
                            $(this).prev().text(resp.data.supplier_email);
                            break;
                        case 'supplier_qq':
                            $(this).prev().text(resp.data.supplier_qq);
                            break;
                        case 'supplier_mobile':
                            $(this).prev().text(resp.data.supplier_mobile);
                            break;
                        case 'supplier_telephone':
                            $(this).prev().text(resp.data.supplier_telephone);
                            break;
                    }
                    $(this).text('隐藏');
                    $.get(getLogDomain() + "/api/addSensitiveClick", {
                        uid: getCookie("oa_user_id") || 0,
                        sys_id: 4,
                        mask_type: type || 0,
                        origin_id: contactId || 0,
                        source_from: window.location.href
                    });
                    console.log(resp);
                } else {
                    layer.msg(resp.err_msg, {'icon': 5});
                    return false;
                }
            }

        });
    });
</script>