<!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">
</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">
                            <div class="layui-col-xs12 layui-col-sm6">
                                <label class="layui-form-label"><font color='red' size="4">*</font>名称：</label>
                                <div class="layui-input-block">
                                    <input type="text" name="name" lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>

                            <div class="layui-col-xs12 layui-col-sm6">
                                <label class="layui-form-label">有效期类型：</label>
                                <div class="layui-input-block">
                                    <select id="off_type" name="off_type" lay-filter="collage" class="layui-select">
                                        <option value="0">固定日期</option>
                                        <option value="1">领取计时</option>
                                    </select>
                                </div>
                            </div>

                            <div class="layui-col-xs12 layui-col-sm6">
                                <label class="layui-form-label">截止日期：</label>
                                <div class="layui-input-block">
                                    <input  type="text" class="layui-input" autocomplete="off"   name="end_date" id="id_end_date">
                                </div>
                            </div>

                            <div class="layui-col-xs12 layui-col-sm6">
                                <label class="layui-form-label">截止天数：</label>
                                <div class="layui-input-block">
                                    <input id="id_end_days" type="text" name="end_days" autocomplete="off" class="layui-input">
                                </div>
                            </div>


                            <div class="layui-col-xs12 layui-col-sm6">
                              <label class="layui-form-label">在用：</label>
                              <div class="layui-input-block">
                                <input type="checkbox" name="enabled" lay-skin="switch" lay-text="是|否" checked="" value="1">
                              </div>
                            </div>
                            <button id="customer_submit" lay-submit lay-filter="component-form-element" style="display: none">保存</button>

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

  <script src="../../layuiadmin/layui/layui.js"></script>
  <script>
  layui.config({
    base: '../../../layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index', //主入口模块
    utils:'utils'

  }).use(['index', 'form', 'utils', 'laydate'], function(){
        var $ = layui.$
        ,admin = layui.admin
        ,form = layui.form
        ,laydate = layui.laydate
        ,utils = layui.utils;

        $('#id_end_days').attr('disabled', true);
        form.on('select(collage)', function (data) {
            var off_type = $('#off_type').val();
            if (off_type == '0') {
                $('#id_end_days').attr('disabled', true);
                $('#id_end_date').attr('disabled', false);
                $('#id_end_days').val('');
            }else {
                $('#id_end_date').attr('disabled', true);
                $('#id_end_days').attr('disabled', false);
                $('#id_end_date').val('');
            }
        });

        var editdata = null;
        var id = layui.view.getParameterByName('id');
        if(id){
              editdata = JSON.parse(JSON.stringify(parent.layui.table.editdata)); // 框架有Bug所以这么转换
              form.val("component-form-element", editdata);
        }

        form.on('submit(component-form-element)', function(data){
            var off_type = $('#off_type').val();
            var end_date = $('#id_end_date').val();
            var end_days = $('#id_end_days').val();

            if (off_type == '0' && end_date === ''){
                layer.msg('请填写截止日期');
                 return false;
            }
            if (off_type == '1' && end_days === ''){
                layer.msg('请填写截止天数');
                return false;
            }
            if (off_type == '0') {
                delete data.field['end_days'];
            } else {
                delete data.field['end_date'];
            }
          admin.req({
            url: '/activity/coupon/save/?id='+id
            ,data: JSON.stringify(data.field)
            ,type: 'post'
            ,done: function (res) {
                  parent.layui.onSubmitChild(res.data);
              }
          });

          return false;
        });

        parent.layui.submitChild = function () {
          $("#customer_submit").click();
        };

        laydate.render({
            elem: '#id_end_date',
            trigger: 'click',
            closeStop: '#id_end_date'
        });

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