/**
 *
 * @author BOGON
 * @version 1.0
 */
let el = {
    tb: '#tb',
    searchForm: '#searchForm',
    invitation: '#invitation',
    copy: '#copy',
    regType: '#regType',
    regState: '#regState',
    userName: '#userName',
    authorize: '#authorize',
    shortName: '#shortName',
    showLink: '#showLink'
};
var page = {
    initData: function () {
        $("#tb").datagrid({
            method: 'GET',
            url: g.ctx + 'pc/supplier/invitation/rest/list',
            iconCls: 'icon-list',
            height: document.documentElement.clientHeight - $("#searchForm").outerHeight() - 10,
            singleSelect: true,
            pagination: true,
            rownumbers: true,
            nowrap: false,
            striped: true,
            page: 1,
            pageSize: 10,
            columns: [[{
                field: 'ck', checkbox: true
            }, {
                field: 'shortName', title: '供应商简称', width: '200', align: 'left'
            }, {
                field: 'supplierName', title: '供应商全称', width: '200', align: 'left'
            }, {
                    field: 'infoState', title: '注册状态', formatter: function (value, row) {
                        switch (value) {
                            case 0:
                                return "<span>初创</span>";
                            case 1:
                                return "<span style='color: #0B61A4'>第一步完成</span>";
                            case 2:
                                if (row.enableQuestionnaire) {
                                    return "<span style='color: green'>信息填写完成</span>";
                                } else {
                                    return "<span style='color: #0c91e5'>第二步完成</span>";
                                }
                            case 3:
                                return "<span style='color: green'>信息填写完成</span>";
                            case 4:
                                return "<span style='color: blue'>注册完成</span>";
                            default:
                                return "<span></span>";
                        }
                    }, width: '90', align: 'left'
                }, {
                    field: 'signUpThrough', title: '注册审批状态', formatter: function (value) {
                        switch (value) {
                            case 0:
                                return "<span>未审核</span>";
                            case 1:
                                return "<span style='color: green'>通过</span>";
                            case 2:
                                return "<span style='color: red'>驳回</span>";
                            case 3:
                                return "<span style='color: yellowgreen'>驳回后审批</span>";
                            default:
                                return "<span></span>";
                        }
                    }, width: '90', align: 'left'
                }, {
                    field: 'templateName', title: '注册模板', width: '160', align: 'left'
                }, {
                    field: 'registerType',
                    title: '供应商区分',
                    width: '160',
                    align: 'left',
                    formatter: function (value) {
                        switch (value) {
                            case 0:
                                return '<span style="color:green">邀请注册</span>';
                            case 1:
                                return '<span style="color:blue">自主注册</span>';
                            default:
                                return '<span style="color:grey">未知</span>';
                        }
                    }
                }, {
                    field: 'purchaseGroupCode',
                    title: '对账采购组',
                    width: '170',
                    align: 'left',
                    formatter: function (value) {
                        if (value) {
                            return getNecessaryDataName("pc/sys/purchaseGroup/rest/listAll", value)
                        }
                        return ''
                    }
                }, {
                    field: 'supplierType', title: '供应商类型', width: '90', formatter: function (value) {
                        return getDicName('supplier_classify', value)
                    }
                }, {
                    field: 'createName', title: '创建人', width: '100',
                }, {
                    field: 'createTime', title: '注册时间', width: '150'
                }]],
            toolbar: [{
                text: '新增', iconCls: 'icon-add', id: 'btn_add', hidden: true, handler: function () {
                    page.detailDialog();
                }
            }, "-", {
                text: '编辑', iconCls: 'icon-edit', id: 'btn_edit', handler: function () {
                    var row = $('#tb').datagrid('getSelected');
                    if (!row) {
                        topWin.layer.msg("请选中一条记录");
                        return;
                    }
                    //需要判断是否已经审批通过，如果审批通过不允许修改
                    if (row.signUpThrough === 1) {
                        topWin.layer.msg("已通过记录无法修改");
                        return;
                    }
                    page.detailDialog(row.id);
                }
            }, "-", {
                text: '查看', iconCls: 'icon-book', id: 'btn_look', handler: function () {
                    var row = $('#tb').datagrid('getSelected');
                    if (!row) {
                        topWin.layer.msg("请选中一条记录");
                        return;
                    }
                    page.detailDialog(row.id, 1);
                }
            }, "-", {
                text: '查看详情', iconCls: 'icon-large-smartart', id: 'btn_info', handler: function () {

                    page.look();
                }
            }, "-", {
                text: '删除', hide: true, iconCls: 'icon-remove', id: 'btn_remove', handler: function () {
                    page.remove();
                }
            }, "-", {
                text: '强制删除', hide: true, iconCls: 'icon-remove', id: 'btn_remove_force', handler: function () {
                    page.forceRemove();
                }
            }, '-', {
                text: '变更供应商模板', id: 'btn-modify-template', iconCls: 'icon-edit', handler: function () {
                    page.changeTemplate();
                }
            }, "-", {
                text: '生成链接', iconCls: 'icon-undo', id: 'btn_hyperlinks', handler: function () {
                    page.invitation();
                }
            }, {
                text: '审批', id: 'btn_examine', iconCls: 'icon-ok', handler: function () {
                    var row = $('#tb').datagrid('getSelected');
                    if (!row) {
                        topWin.layer.msg("请选中一条记录");
                        return;
                    }
                    //需要判断创建人是否存在，如果存在说明已经编辑了，如果不存在就无法审核
                    if (row.createId === '') {
                        topWin.layer.msg("请先完成信息补充再审核");
                        return;
                    }
                    if (row.signUpThrough === 1) {
                        topWin.layer.msg("审核通过无需重复审核");
                        return;
                    }
                    if (row.infoState !== 3) {
                        topWin.layer.msg("注册未完成无法审核");
                        return;
                    }
                    page.approval(row.supplierId, row.id);
                }
            },{
                text: '评审方式', id: 'btn_review_method', iconCls: 'icon-ok', handler: function () {
                    var row = $('#tb').datagrid('getSelected');
                    if (!row) {
                        topWin.layer.msg("请选中一条记录");
                        return;
                    }
                    page.reviewMethod(row.id);
                }
            }, "-",{
                text: '更新状态', iconCls: 'icon-ok', id: 'btn_copy', hidden: true, handler: function () {
                    page.detailTcDialog();
                }
                },
            ],
            onDblClickRow: function (index, row) {
                page.look();
            },
            onLoadSuccess: function () {
                displayButtonByUserRole('Supplier Invitation');
            }

        });
    }, approval: function (supplierId, id) {
        var url = g.ctx + 'pc/supplier/detail?id=' + supplierId + "&typeStr=inviationApproval"
        var invitationApprovalDialog = openDialog('invitationApprovalDialog', url, {
            title: '查看供应商信息',
            width: "90%",
            height: "100%",
            cache: false,
            modal: true,
            border: false,
            draggable: true,
            shadow: false,
            onClose: function () {
                invitationApprovalDialog.dialog('destroy');
            },
            buttons: [{
                text: itablei8n.tg, handler: function () {
                    topWin.frames['invitationApprovalDialog'].signUpRemark(function (val) {
                        $.ajax({
                            url: g.ctx + 'pc/supplier/invitation/rest/agree',
                            type: 'POST',
                            data: {id: id, remark: val},
                            success: function (data) {
                                topWin.layer.alert("审批成功", {icon: 1}, function (idx) {
                                    invitationApprovalDialog.dialog('close');
                                    $('#tb').datagrid('reload');
                                    topWin.layer.close(idx);
                                });
                            },
                            error: function (data) {
                                topWin.layer.alert(data.responseJSON.message, {icon: 2});
                            }
                        })
                    });
                }
            }, {
                text: itablei8n.bh, handler: function () {
                    topWin.frames['invitationApprovalDialog'].signUpRemark(function (val) {
                        $.ajax({
                            url: g.ctx + 'pc/supplier/invitation/rest/reject',
                            type: 'POST',
                            data: {id: id, remark: val},
                            success: function (data) {
                                topWin.layer.alert("审批成功", {icon: 1}, function (idx) {
                                    invitationApprovalDialog.dialog('close');
                                    $('#tb').datagrid('reload');
                                    topWin.layer.close(idx);
                                });
                            },
                            error: function (data) {
                                topWin.layer.alert(data.responseJSON.message, {icon: 2});
                            }
                        })
                    });
                }
            }, {
                text: itablei8n.close, handler: function () {
                    invitationApprovalDialog.dialog('close');
                }
            }]
        });
    },reviewMethod: function (id) {
        let url = g.ctx + 'pc/supplier/invitation/page/updateReviewMethod?id=' + id;
        var reviewMethodDialog = openDialog('reviewMethodDialog', url, {
            title: "修改评审方式",
            width: "50%",
            height: "50%",
            cache: false,
            modal: true,
            border: false,
            draggable: true,
            shadow: false,
            onClose: function () {
                reviewMethodDialog.dialog('destroy');
            },
            buttons: [{
                text: itablei8n.save, handler: function () {
                    topWin.frames['reviewMethodDialog'].page.update(function () {
                        reviewMethodDialog.dialog('close');
                        $('#tb').datagrid('reload');
                    });
                }
            }, {
                text: itablei8n.close, handler: function () {
                    reviewMethodDialog.dialog('close');
                }
            }]
        });

    }, detailDialog: function (id, lookupFlag) {
        let title = ''
        if (id != undefined && lookupFlag != undefined) {
            title = '查看'
        } else if (id != undefined) {
            title = '编辑'
        } else {
            title = '新增'
        }
        let url = g.ctx + 'pc/supplier/invitation/page/detail?lookupFlag=' + (lookupFlag == undefined ? '0' : '1') + '&id=' + (id == undefined ? '' : id);
        var invitationEditDialog = openDialog('invitationEditDialog', url, {
            title: title,
            width: "100%",
            height: "100%",
            cache: false,
            modal: true,
            border: false,
            draggable: true,
            shadow: false,
            onClose: function () {
                invitationEditDialog.dialog('destroy');
            },
            buttons: [{
                text: itablei8n.save, handler: function () {
                    topWin.frames['invitationEditDialog'].page.saveOrUpdate(function () {
                        invitationEditDialog.dialog('close');
                        $('#tb').datagrid('reload');
                    });
                }
            }, {
                text: itablei8n.close, handler: function () {
                    invitationEditDialog.dialog('close');
                }
            }]
        });
    },//同创
    detailTcDialog: function () {
        let row = $(el.tb).datagrid('getSelected');
        if (!row) {
            topWin.layer.msg("请选中一条记录");
            return
        }
        topWin.layer.confirm("是否更新初始状态", {icon: 3}, function (index) {
            $.ajax({
                url: g.ctx + 'pc/supplier/invitation/rest/updateState?id='+row.supplierId,
                type: 'GET',
                success: function (data) {
                    topWin.layer.alert("状态更新成功", {icon: 1}, function (idx) {
                        $(el.tb).datagrid("reload")
                        topWin.layer.close(index);
                        topWin.layer.close(idx);
                    });
                },
                error: function (data) {
                    topWin.layer.alert(data.responseJSON.message, {icon: 2});
                }
            })
        })
    },
//生成链接
    invitation: function () {
        let row = $("#tb").datagrid('getSelected');
        if (!row) {
            topWin.layer.msg("您未选中任何数据");
            return;
        }

        let link;
        $.ajax({
            url: g.ctx + "pc/supplier/invitation/rest/generateUrl", data: {
                supplierId: row.supplierId, templateId: row.templateId
            }, type: "GET", success: function (data) {
                link = data;

                if (!link || link === '') {
                    topWin.layer.msg('请求超时，请刷新页面', {icon: 0});
                    return;
                }

                let test = window.location.href;
                let url = link;
                $(el.copy).val(url);
                $(el.showLink).html("邀请码链接为:" + url);

                layer.open({
                    title: "邀请链接",
                    type: 1,
                    area: ['800px', '400px'],
                    content: $(el.invitation),
                    btn: ['一键发送邮件', '复制', '关闭'],
                    success: function (la, layerIndex) {
                        $(la.find('.layui-layer-btn a')[1]).on('click', function () {
                            let _input = $(el.copy);
                            _input.select();
                            document.execCommand("Copy");
                            layer.close(layerIndex);
                            parent.layer.msg("已复制");
                        });
                    },
                    yes: function (layerIndex) {
                        let loadIndex = topWin.layer.msg('发送中...', {icon: 16, time: 0});
                        $.ajax({
                            url: g.ctx + 'pc/supplier/invitation/rest/sendEmail?id=' + row.id,
                            type: 'POST',
                            data: {id: row.id},
                            complete: function () {
                                topWin.layer.close(loadIndex)
                            },
                            success: function (res) {

                                topWin.layer.alert('发送成功', function (index) {
                                    topWin.layer.close(index);
                                    layer.close(layerIndex);
                                });
                            },
                            error: function (data) {
                                topWin.layer.alert(data.responseJSON.message, {icon: 2});
                            }
                        });
                        return false
                    }
                });
            }, error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        })

    }, forceRemove: function () {
        let row = $(el.tb).datagrid('getSelected');
        if (!row) {
            topWin.layer.msg("请选中一条记录");
            return
        }
        topWin.layer.confirm("是否强制删除", {icon: 3}, function (index) {
            $.ajax({
                url: g.ctx + 'pc/supplier/invitation/rest/forceRemove',
                type: 'POST',
                data: {id: row.id},
                success: function (data) {
                    topWin.layer.alert("删除成功", {icon: 1}, function (idx) {
                        $(el.tb).datagrid("reload")
                        topWin.layer.close(index);
                        topWin.layer.close(idx);
                    });
                },
                error: function (data) {
                    topWin.layer.alert(data.responseJSON.message, {icon: 2});
                }
            })
        })
    }, remove: function () {
        let row = $(el.tb).datagrid('getSelected');
        if (!row) {
            topWin.layer.msg("请选中一条记录");
            return
        }
        topWin.layer.confirm(tipsi18n.sfsc, {icon: 3}, function (index) {
            $.ajax({
                url: g.ctx + 'pc/supplier/invitation/rest/remove',
                type: 'POST',
                data: {id: row.id},
                success: function (data) {
                    topWin.layer.alert("删除成功", {icon: 1}, function (idx) {
                        $(el.tb).datagrid("reload")
                        topWin.layer.close(index);
                        topWin.layer.close(idx);
                    });
                },
                error: function (data) {
                    topWin.layer.alert(data.responseJSON.message, {icon: 2});
                }
            })
        })
    }, /**
     * 查看供应商注册信息
     */
    look: function () {
        let row = $(el.tb).datagrid('getSelected');
        if (!row) {
            topWin.layer.msg("请选中一条记录");
            return;
        }
        if (row.infoState === 0) {
            topWin.layer.msg("初创无法查看供应商详情!");
            return;
        }
        let id = row.supplierId;
        var url = g.ctx + 'pc/supplier/detail?id=' + id
        var invitationEditDialog = openDialog('invitationEditDialog', url, {
            title: '查看供应商信息',
            width: "90%",
            height: "100%",
            cache: false,
            modal: true,
            border: false,
            draggable: true,
            shadow: false,
            onClose: function () {
                invitationEditDialog.dialog('destroy');
            },
            buttons: [{
                text: itablei8n.close, handler: function () {
                    invitationEditDialog.dialog('close');
                }
            }]
        });
    }, changeTemplate: function () {
        let row = $(el.tb).datagrid('getSelected');
        if (!row) {
            topWin.layer.msg("请选中一条记录");
            return;
        }
        var url = g.ctx + 'pc/supplier/template/page/index'
        var invitationTemplateDialog = openDialog('invitationTemplateDialog', url, {
            title: '变更供应商模板',
            width: "100%",
            height: "100%",
            cache: false,
            modal: true,
            border: false,
            draggable: true,
            shadow: false,
            onClose: function () {
                invitationTemplateDialog.dialog('destroy');
            },
            buttons: [{
                text: itablei8n.save, handler: function () {
                    topWin.frames['invitationTemplateDialog'].page.getChecked(function (templateId) {
                        invitationTemplateDialog.dialog('close');
                        $.ajax({
                            url: g.ctx + 'pc/supplier/invitation/rest/updateTemplate',
                            type: 'POST',
                            data: {id: row.id, templateId: templateId},
                            success: function (data) {
                                topWin.layer.alert("修改成功", {icon: 1}, function (idx) {
                                    $('#tb').datagrid('reload');
                                    topWin.layer.close(idx);
                                });
                            },
                            error: function (data) {
                                topWin.layer.alert(data.responseJSON.message, {icon: 2});
                            }
                        })
                    });
                }
            }, {
                text: itablei8n.close, handler: function () {
                    invitationTemplateDialog.dialog('close');
                }
            }]
        });
    }
}
$(function () {
    initDict("supplier_classify")
    initNecessaryData("pc/sys/purchaseGroup/rest/listAll", 'purchaseGroupCode', 'purchaseGroupName')
    page.initData()
})
