<script>
    layui.use(['table', 'form', 'element', 'layer', 'admin', 'upload'], function () {
        let table = layui.table;
        let form = layui.form;
        let admin = layui.admin;
        let element = layui.element;
        let upload = layui.upload;
        let supplierId = getQueryVariable('supplier_id')

        table.render({
            elem: '#attachmentList',
            url: '/api/supplier_attachment/getSupplierAttachmentList?supplier_id=' + supplierId,
            method: 'post',
            size: 'sm',
            cellMinWidth: 80,//全局定义常规单元格的最小宽度
            where: {
                supplier_id: supplierId
            },
            width: '70%',
            loading: true,
            first: true,//不显示首页
            last: false,//不显示尾页
            cols: [[
                    @if($operate=='update')
                {
                    type: 'radio',
                },
                    @endif
                {
                    field: 'type_name', title: '<span class="require">* </span>附件类型', align: 'center', width: 150
                },
                {
                    field: 'file_name',
                    title: '<span class="require">* </span>附件名称', align: 'center', width: 270,
                    templet: function (data) {
                        return "<a style='color: blue' href='" + data.file_url + "' target='_blank'>" + data.file_name + "<div style='display:none' class='attachment_fields'>" + data.field_name + "</div></a>";
                    }
                },
                {field: 'description', title: '附件说明', align: 'center', width: 250},
                {field: 'validity_period', title: '<span class="require">* </span>有效期', align: 'center', width: 220},
                {field: 'create_name', title: '上传人', align: 'center', width: 100},
                {field: 'create_time', title: '上传时间', align: 'center', width: 150},
            ]],
            id: 'attachmentList',
            page: {},
        });

        //新增附件弹窗
        $("#add_attachment").click(function () {
            layer.open({
                type: 2,
                content: '/supplier_attachment/AddSupplierAttachment?view=iframe&supplier_id=' + supplierId,
                area: ['50%', '70%'],
                title: '新增附件',
                end: function () {
                    table.reload('attachmentList');
                }
            });
        })

        //修改附件弹窗
        $("#update_attachment").click(function () {
            let checkStatus = table.checkStatus('attachmentList');
            let data = checkStatus.data;
            if (!data.length) {
                layer.msg('请先选择要操作的附件', {icon: 5})
            } else {
                let attachment_id = data[0].attachment_id;
                let supplierId = data[0].supplier_id;
                layer.open({
                    type: 2,
                    content: '/supplier_attachment/UpdateSupplierAttachment?view=iframe&supplier_id=' + supplierId + '&attachment_id=' + attachment_id,
                    area: ['50%', '70%'],
                    title: '修改附件',
                    end: function () {
                        table.reload('attachmentList');
                    }
                });
            }
        })

        $("#delete_attachment").click(function () {
            let checkStatus = table.checkStatus('attachmentList');
            let data = checkStatus.data;
            if (!data.length) {
                layer.msg('请先选择要操作的附件', {icon: 5})
            } else {
                layer.confirm('确定要删除选中的附件吗?', function (index) {
                    let attachmentId = data[0].attachment_id;
                    let res = ajax('/api/supplier_attachment/DeleteSupplierAttachment', {attachment_id: attachmentId})
                    if (res.err_code === 0) {
                        table.reload('attachmentList')
                        layer.msg(res.err_msg, {icon: 6})
                    } else {
                        layer.msg(res.err_msg, {icon: 5})
                    }
                });
            }
        });
    });
</script>