<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>仓别</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
    <style type="text/css">
        .layui-form-select dl {
            max-height: 150px;
        }
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" pad15>
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">

                    <form class="layui-form" action="" lay-filter="component-form-element">
                        <div class="layui-row layui-col-space10 layui-form-item">

                            <div class="layui-col-lg6">
                                <label class="layui-form-label">收支类别：</label>
                                <div class="layui-input-block">
                                    <select id="id_category" name="category" lay-filter="categoryChange">
                                        <option value="">请选择</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-col-lg6">
                                <label class="layui-form-label"><font color='red' size="4">*</font>收支用途：</label>
                                <div class="layui-input-block">
                                    <select id="id_purpose" name="purpose" lay-verify="required">
                                        <option value="">请选择</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-col-lg6">
                                <label class="layui-form-label"><font color='red' size="4">*</font>收支账户：</label>
                                <div class="layui-input-block">
                                    <select id="id_account" name="account" lay-verify="required">
                                        <option value="">请选择</option>
                                    </select>
                                </div>
                            </div>

                            <div class="layui-col-lg6">
                                <label class="layui-form-label"><font color='red' size="4">*</font>收支金额：</label>
                                <div class="layui-input-block">
                                    <input type="text" name="amount" placeholder="" autocomplete="off"
                                           lay-verify="number|required"
                                           class="layui-input">
                                </div>
                            </div>

                            <div>
                                <label class="layui-form-label">备注：</label>
                                <div class="layui-input-block">
                                    <input type="text" name="notes" placeholder="请输入备注"
                                           autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <button id="submit_btn" style="display: none;" class="layui-btn" lay-submit
                                    lay-filter="component-form-element"></button>

                        </div>
                    </form>

                </div>
            </div>
        </div>
    </div>
</div>


<script src="../../layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function () {
        var $ = layui.$
            , admin = layui.admin
            , form = layui.form;

        var id = layui.view.getParameterByName('id');
        admin.req({
            url: '/foundation/search_options/?type=5',
            done: function (res) {
                var data = res.data;
                var order_account = $('#id_account');
                for (var i in data) {
                    var pid = data[i].id;
                    var value = data[i].name;
                    order_account.append("<option value='" + pid + "'>" + value + "</option>");
                }
                form.render();
                loadData()
            }
        });
        var purpose_data = [], purpose_id;
        admin.req({
            url: '/finance/finance_category/dict/',
            done: function (res) {
                var category_data = res.data.category;
                purpose_data = res.data.purpose;
                var category = $('#id_category');
                for (var i in category_data) {
                    var pid = category_data[i].id;
                    var value = category_data[i].name;
                    category.append("<option value='" + pid + "'>" + value + "</option>");
                }
                form.render();
                loadData()
            }
        });
        var loadData = function () {
            if (id) {
                var editdata = JSON.parse(JSON.stringify(parent.layui.table.editdata)); // 框架有Bug所以这么转换
                form.val("component-form-element", editdata);
                purpose_id = editdata.purpose
                categoryChange(editdata.category)
            }
        };
        var categoryChange = function (value) {
            var purpose_option = '';
            for (var i = 0; i < purpose_data.length; i++) {
                if (purpose_data[i].category == parseInt(value)) {
                    if (purpose_data[i].id == purpose_id)
                        purpose_option += "<option value=" + purpose_data[i].id + " selected>" + purpose_data[i].name + "</option>";
                    else
                        purpose_option += "<option value=" + purpose_data[i].id + ">" + purpose_data[i].name + "</option>";
                }
            }
            $("#id_purpose").append(purpose_option);
            form.render();
        };
        form.on('select(categoryChange)', function (data) {
            $("#id_purpose").html('');
            if (!data.value) return;
            categoryChange(data.value)
        });

        form.on('submit(component-form-element)', function (data) {
            data.field.type = 1;

            admin.req({
                url: '/finance/finance_income/save/?id=' + id
                , data: JSON.stringify(data.field)
                , type: 'post'
                , done: function (res) {
                    parent.layui.onSubmitChild(res.data);
                }
            });
            return false;
        });
        parent.layui.submitChild = function () {
            $("#submit_btn").click();
        };

    });
</script>
</body>
</html>
