//config的设置是全局的
layui.config({
    base: '/static/js/extend/' //假设这是你存放拓展模块的根目录
}).extend({ //设定模块别名
    common: 'common' //如果 mymod.js 是在根目录，也可以不用设定别名
});


layui.use(['form','table','upload','common'], function() {
    var form = layui.form;
    var common = layui.common;
    var table = layui.table;
    var $ = layui.jquery,upload = layui.upload;
    var tableInfo=table.render({
        elem: '#businessRepair'
        ,url:'/businessCategory/getList'
        ,id: 'id'
        ,where:{
            parentId:0
        }
        ,method:"post"
        ,contentType:"application/json;charset=UTF-8"
        ,loading:true
        ,cols: [[
            {field:'id', title: 'ID', sort: true,checkbox:true ,fixed: 'left'}
            ,{field:'id', title: '编号'}
            ,{field:'name', title: '类目名称'}
            ,{field:'state', title: '状态', sort: true,templet:function (d) {
                    return d.state==1?'启用':'禁用';
            }}
            ,{field:'createTime', title: '创建时间'}
            ,{fixed: 'right', title:'操作', toolbar: '#businessRepairButton', width:150}
        ]]
        ,page: true
    });

    //指定允许上传的文件类型
    upload.render({
        elem: '#importExcel'
        ,url: '/businessCategory/uploadExcelFile'
        ,accept: 'file' //普通文件,
        ,data:""
        ,done: function(res,index,upload){
            if (res.code=="01"){
                table.reload("id");
                layer.alert('导入成功', {
                    icon: 1,
                    skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
                });
            }else{
                layer.alert('导入失败，请联系管理员', {
                    icon: 2,
                    skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
                });
            }
        }
    });

    //监听行工具事件
    var parentIdData;
    table.on('tool(businessRepair)', function(obj){
        parentIdData=obj.data;
        if (obj.event=='open'){
            common.layuiOpen("businessRepairNodeDiv","70%","80%")
            table.render({
                elem: '#businessRepairNode'
                , url: '/businessCategory/getList'
                , id: 'nodeId'
                ,toolbar:"#toolNodeDiv"
                , where: {
                    master:1,
                    parentId:obj.data.id
                }
                ,method:"post"
                ,contentType:"application/json;charset=UTF-8"
                , loading: true
                , cols: [[
                    {field: 'id', title: 'ID', sort: true, checkbox: true, fixed: 'left'}
                    , {field: 'name', title: '名称'}
                    , {field: 'cityLevel', title: '城市等级', templet: function (d) {
                        return RenderingCityLevel(d.cityLevel);
                      }}
                    , {field: 'level', title: '区域', templet: function (d) {
                        return d.level="district"?"城区":"乡镇";
                      }}
                    , {field: 'installationPrice', title: '安装价格'}
                    , {field: 'modifyPrice', title: '修孔价格'}
                    , {field: 'openingPrice', title: '开孔价格'}
                    , {field: 'newOpeningPrice', title: '全新开孔'}
                    , {
                        field: 'state', title: '状态', sort: true, width: 100, templet: function (d) {
                            if (d.state==1){
                                return '<input type="checkbox" value="'+d.id+'" name="state" checked="checked" lay-skin="switch"  lay-filter="switchTest" lay-text="启用|禁用">';
                            }else{
                                return '<input type="checkbox" value="'+d.id+'" name="state" lay-skin="switch"  lay-filter="switchTest" lay-text="启用|禁用">';
                            }
                        }
                    }
                    , {field: 'createTime', title: '创建时间'}
                ]]
                , page: true
            });
        }//监听单元格编辑
        table.on('edit(businessRepairNode)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            common.ajaxPostRequest("/businessCategory/saveOrUpdate",data);
        });
        //监听锁定操作
        form.on('switch(switchTest)', function(obj){
            common.ajaxGetRequest("/businessCategory/updateByState",{id:this.value,state:obj.elem.checked==false?0:1})
        });
    });

    form.on('submit(formDemo)', function(data){
        var result= common.ajaxPostRequest("/businessCategory/saveOrUpdate",data.field);
        if (result!='false'){
            table.reload("id");
            layer.alert('操作成功', {
                icon: 1,
                skin: 'layer-ext-moon'
            });
        }
    });



    //打开 新增or 修改窗口
    $(document).on('click', '#openSaveOrUpdate', function() {
        common.layuiOpen("addDiv","40%","60%")
    });

    //子页面新增按钮
    $(document).on('click', '#nodeAdd', function() {
        common.layuiOpen("add-node-main","50%","60%");
        //表单初始赋值
        form.val('initData', {
            "parentId": parentIdData.id
        })
    });
    //子页面监听提交
    form.on('submit(nodeSubmit)', function(data){
        var result= common.ajaxPostRequest("/businessCategory/saveOrUpdate",data.field,"nodeId");
        if (result!='false'){
            layer.close(layer.index);
            layer.alert('操作成功', {
                icon: 1,
                skin: 'layer-ext-moon'
            });
        }
        return false;
    });

    table.on('tool(businessRepairNode)', function(obj){
        if(obj.event === 'del'){
            layer.confirm('真的删除行么', function(index){
                common.ajaxGetRequest("/businessCategory/deleteById",{id:obj.data.id})
                table.reload("id",{
                    where:{
                        parentId:obj.data.parentId
                    }
                });
                layer.close(index);
            });
        }
    });

});


function calculatePrice(price) {
  var price= $("input[name='"+price+"']").val();
  $("input[name='"+price+"']").parent().parent().find("div").eq(1).text("师傅到手价格为:"+parseFloat(price)*parseFloat("0.85"))
}

function RenderingCityLevel(level) {
    if (level=="1"){
        return "一线城市";
    }else if (level=="2"){
        return "二线城市";
    }else if (level=="3"){
        return "三线城市";
    }else if (level=="4"){
        return "四线城市";
    }
}