@page
@model Waste.Web.Entry.Pages.Device.EditModel
@{
    ViewData["Title"] = "设备编辑";
}
<div class="layui-card">
    <div class="layui-card-body">
        <div class="form-horizontal layui-form">
            <div class="layui-form-item row">
                <label class="layui-form-label col-md-2" for="Name">设备名称</label>
                <div class="col-md-4">
                    <input type="text" class="layui-input" id="Name" name="Name" value="@Model.data.Name" placeholder="请输入出厂编号" lay-verify="required" />
                </div>
            </div>
            <div class="layui-form-item row">
                <label class="layui-form-label col-md-2" for="FacEcode">出厂编号</label>
                <div class="col-md-4">
                    <input type="text" class="layui-input" id="FacEcode" name="FacEcode" value="@Model.data.FacEcode" placeholder="请输入出厂编号" lay-verify="required" />
                </div>
            </div>
            <div class="layui-form-item row">
                <label class="layui-form-label col-md-2" for="Ecode">设备编号</label>
                <div class="col-md-4">
                    <input type="text" class="layui-input" id="Ecode" name="Ecode" value="@Model.data.Ecode" placeholder="请输入设备编号" lay-verify="required" />
                </div>
            </div>
            <div class="layui-form-item row">
                <label class="layui-form-label col-md-2" for="DeviceType">设备类型</label>
                <div class="col-md-4">
                    <select id="DeviceType" name="DeviceType">
                        @foreach (var item in Model.devicetypes)
                        {
                            if (Model.data.DeviceType.ToString() == item.Value)
                            {
                                <option value="@item.Value" selected>@item.Text</option>
                            }
                            else
                            {
                                <option value="@item.Value">@item.Text</option>
                            }
                        }
                    </select>
                </div>
            </div>
            <div class="layui-form-item row">
                <label class="layui-form-label col-md-2" for="Tare">皮重</label>
                <div class="col-md-4">
                    <input type="text" class="layui-input" id="Tare" name="Tare" value="@Model.data.Tare" placeholder="请输入设备皮重" lay-verify="required" />
                </div>
            </div>
            <div class="layui-form-item row">
                <label class="layui-form-label col-md-2" for="SecretHash">SecretHash</label>
                <div class="col-md-4">
                    <input type="text" class="layui-input" id="SecretHash" name="SecretHash" value="@Model.data.SecretHash" placeholder="请输入设备对应的secrethash" lay-verify="required" />
                </div>
            </div>
            <div class="layui-form-item row">
                <label class="layui-form-label col-md-2" for="Secret">Secret</label>
                <div class="col-md-4">
                    <input type="text" class="layui-input" id="Secret" name="Secret" value="@Model.data.Secret" placeholder="请输入设备对应的secret" lay-verify="required" />
                </div>
            </div>
            <div class="layui-form-item row">
                <label class="layui-form-label col-md-2" for="DevId">设备ID</label>
                <div class="col-md-4">
                    <input type="text" class="layui-input" id="DevId" name="DevId" value="@Model.data.DevId" placeholder="请输入设备对应的ID" lay-verify="required" />
                </div>
            </div>
            <div class="layui-form-item row">
                <label class="layui-form-label col-md-2" for="NetType">联网方式</label>
                <div class="col-md-4">
                    <select id="NetType" name="NetType">
                        @foreach (var item in Model.devicenettypes)
                        {
                            if (Model.data.NetType.ToString() == item.Value)
                            {
                                <option value="@item.Value" selected>@item.Text</option>
                            }
                            else
                            {
                                <option value="@item.Value">@item.Text</option>
                            }
                        }
                    </select>
                </div>
            </div>
            <div class="layui-form-item row">
                <label class="layui-form-label col-md-2" for="provincer">地址</label>
                <div class="col-md-4">
                    <cascader id="provincer"></cascader>
                </div>
            </div>
            <div class="layui-form-item row">
                <label class="layui-form-label col-md-2" for="Address">详细地址</label>
                <div class="col-md-8">
                    <textarea class="layui-textarea" placeholder="请输入地址" lay-verify="required" name="Address" id="Address">@Model.data.Address</textarea>
                </div>
            </div>
            <div class="layui-form-item row">
                <label class="layui-form-label col-md-2" for="Remark">备注</label>
                <div class="col-md-8">
                    <textarea class="layui-textarea" id="Remark" name="Remark" value="@Model.data.Remark" placeholder="请输入备注">@Model.data.Remark</textarea>
                </div>
            </div>
            <div class="text-center col-md-7">
                <button class="btn btn-primary btn-lg" lay-submit lay-filter="submit" type="button">提交</button>
            </div>
        </div>
    </div>
</div>
@section Scripts{
    <script type="text/javascript" src="~/js/ajaxCascader/ajaxCascader.js"></script>
    <script type="text/javascript">
             layui.use(['form', 'common'], function () {
                 var form = layui.form,
                     common = layui.common;
                 var cascader = layui.ajaxCascader;
                 var provinceval = '@Model.provinceval';
                 var cityval = '@Model.cityval';
                 var areaval = '@Model.areaval';
                      function areas(options) {
                     var defaults = {
                         elem: '#provincer',
                         height: 32,
                         clicklast: true,
                         width: "100%",
                         value:0,
                         prop: {
                             value: 'value',
                             label: 'label',
                             children: 'children'
                         },
                         placeholder: '请选择地区',
                         getChildren: function (value, callback) {
                             var data = [];
                             if (value !== "") {
                                 common.ajax({
                                     url: '/api/system/GetCityList?code=' + value,
                                     type: 'get',
                                     success: function (res) {
                                         data = res.data;
                                         for (var i in data) {
                                             data[i].value = data[i].code;
                                             data[i].label = data[i].name;

                                             if (data[i].level == 3 ||
                                                 (data[i].level == 2 && data[i].code.substring(4, 6) != "00")) {
                                                 delete data[i].code;
                                                 delete data[i].name;
                                                 data[i].hasChild = false;
                                             } else {
                                                 delete data[i].code;
                                                 delete data[i].name;
                                                 data[i].hasChild = true;
                                             }
                                         }
                                         callback(data);
                                     }
                                 });
                             }
                         }
                     };
                     options = $.extend(defaults, options);
                     cascader.load(options);
                 }
                 if ('@Model.data.Id' != "00000000-0000-0000-0000-000000000000") {
                     if (areaval == 0) {
                         areas({
                             checkData: [provinceval, cityval], chooseData: [provinceval, cityval]
                         });
                     }
                     else {
                         areas({
                             checkData: [provinceval, cityval, areaval], chooseData: [provinceval, cityval, areaval]
                         });
                     }
                 } else {
                     areas({});
                 }
                 form.on("submit(submit)", function (data) {
                     data.field['id'] = '@Model.data.Id';
                     var areas = cascader.getChooseData();
                     if (areas.length == 0) {
                         common.msg("请选择地区");
                         return;
                     }
                     data.field["province"] = areas[0];
                     data.field["city"] = areas[1];
                     data.field["area"] = areas[2];
                     common.ajax({
                         url: "/api/device/submitform",
                         type: "post",
                         data: data.field
                     });
                 });
        });
    </script>
}