<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: select2-css"/>
    <th:block th:include="include :: header('收款列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>合同名称：</label>
                            <input type="text" name="name"/>
                        </li>
                        <li>
                            <label>负责人：</label>
                            <select name="leaderId" class="form-control m-b">
                                <option value="">所有</option>
                                <option th:each="user:${users}" th:text="${user.userName}"
                                        th:value="${user.userId}">
                                </option>
                            </select>
                        </li>
                        <li>
                            <label>甲方：</label>
                            <input type="text" name="buyers"/>
                        </li>
                        <li>
                            <label>乙方：</label>
                            <input type="text" name="sellers"/>
                        </li>
                        <li>
                            <label>签约日期：</label>
                            <input type="text" class="time-input" placeholder="请选择签约日期" name="signDate"/>
                        </li>
                        <li>
                            <label>起始日期：</label>
                            <input type="text" class="time-input" placeholder="请选择起始日期" name="startDate"/>
                        </li>
                        <li>
                            <label>结束日期：</label>
                            <input type="text" class="time-input" placeholder="请选择结束日期" name="endDate"/>
                        </li>
                        <li>
                            <label>金额：</label>
                            <input type="text" name="amount"/>
                        </li>
                        <li>
                            <label>付款方式：</label>
                            <select name="payMode" th:with="type=${@dict.getType('cnt_pay_mode')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>收款状态：</label>
                            <select name="payStatus" th:with="type=${@dict.getType('cnt_pay_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <!--            <a class="btn btn-success" onclick="addSettlePlan()" shiro:hasPermission="system:settlement:add">-->
            <!--                <i class="fa fa-plus"></i> 添加收款计划-->
            <!--            </a>-->
            <!--            <a class="btn btn-success" onclick="addSettlement()" shiro:hasPermission="system:settlement:add">-->
            <!--                <i class="fa fa-plus"></i> 确认收款-->
            <!--            </a>-->
            <a class="btn btn-success single disabled" onclick="editSettlePlan()"
               shiro:hasPermission="system:contract:edit">
                <i class="fa fa-edit"></i> 收款计划管理
            </a>
            <a class="btn btn-primary single disabled" onclick="editSettlement()"
               shiro:hasPermission="system:contract:edit">
                <i class="fa fa-edit"></i> 确认收款管理
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:contract:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: select2-js"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:contract:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:contract:remove')}]];
    //var statusDatas = [[${@dict.getType('cnt_status')}]];
    var payModes = [[${@dict.getType('cnt_pay_mode')}]];
    var payStatusDatas = [[${@dict.getType('cnt_pay_status')}]];

    var prefix = ctx + "system/settlement";

    $(function () {
        for (var i = 0; i < payModes.length; i++) {
            if (payModes[i].dictValue == "3") {
                payModes[i].dictLabel = payModes[i].dictLabel + "（人/月）";
            } else if (payModes[i].dictValue == "4" || payModes[i].dictValue == "5") {
                payModes[i].dictLabel = payModes[i].dictLabel + "（%）";
            }
        }
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add/{id}",
            updateUrl: prefix + "/edit/{id}",
            detailUrl: prefix + "/detail/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "收款",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: 'ID',
                    visible: false
                },
                {
                    field: 'name',
                    title: '名称'
                },
                {
                    field: 'buyers',
                    title: '甲方名称'
                },
                {
                    field: 'payMode',
                    title: '付款方式',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(payModes, value);
                    }
                },
                {
                    field: 'amount',
                    title: '合同金额（元）',
                    formatter: numFormat,
                    cellStyle: cellNeedRed
                },
                {
                    field: 'payedAmount',
                    title: '已收款金额（元）',
                    formatter: numFormat,
                    cellStyle: cellNeedRed
                },
                {
                    title: '剩余金额（元）',
                    formatter: function (value, row, index) {
                        if (row.payMode == "3" || row.payMode == "4" || row.payMode == "5") {
                            return "-";
                        }
                        return numFormat(row.amount - row.payedAmount);
                    }
                },
                {
                    field: 'matureAmount',
                    title: '最近到期金额（元）',
                    formatter: numFormat,
                    cellStyle: cellNeedRed
                },
                {
                    field: 'matureDate',
                    title: '最近到期时间',
                    cellStyle: cellNeedRed
                },
                {
                    field: 'payStatus',
                    title: '状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(payStatusDatas, value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="editPayStatus(\'' + row.id + '\')"><i class="fa fa-edit"></i>设置状态</a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function numFormat(value) {
        if (!value) {
            return '0';
        }
        if (value == '-') {
            return '0';
        }
        return value.toFixed(2);
    }

    function cellNeedRed(value, row, index) {
        var day = row.matureDate;
        if (!day) {
            return {};
        }
        var matureDate = new Date(day);
        if (!matureDate) {
            return {};
        }
        var timeSpan = matureDate - (new Date());
        if (timeSpan < 10 * 24 * 60 * 60 * 1000) {
            return {
                css: {
                    'color': 'red'
                }
            };
        }
        return {};
    }

    function addSettlePlan() {
        var id = getContractId();
        id = id + "?settleType=0";
        $.operate.addTabWithName(id, "计划收款");
    }

    function addSettlement() {
        var id = getContractId();
        id = id + "?settleType=1";
        $.operate.addTabWithName(id, "确认收款");
    }

    function editSettlePlan() {
        var id = getContractId();
        id = id + "?settleType=0";
        $.operate.editTabWithName(id, "计划收款管理");
    }

    function editSettlement() {
        var id = getContractId();
        id = id + "?settleType=1";
        $.operate.editTabWithName(id, "确认收款管理");
    }

    function getContractId() {
        var row = $("#" + table.options.id).bootstrapTable('getSelections')[0];
        var id = "";
        if ($.common.isEmpty(row)) {
            id = "00000000-0000-0000-0000-000000000000";
        } else {
            id = row.id;
        }
        return id;
    }

    function editPayStatus(id) {
        $.modal.open('设置付款状态', prefix + "/editPayStatus/" + id, 800, 400);
    }
</script>
</body>
</html>