/**
 * jiw
 * 2018.12.24
 * 行政区域
 */
 import { UPCLOUD_RDC_TMS_MAINDATA_SERVICE,UPCLOUD_RDC_TMS_BUSINESS_SERVICE,UPCLOUD_RDC_TMS_INTERFACE_SERVICE} from '@/config/serviceSettings.js';
 import { getSelectGJ, querySysPicture } from '@/utils/prolog.ajax.js';


export default () => {
    $("#updateBtn").hide();
    $("#saveBtn").hide();
    var areaForm_opt = {
        renderer: "areaForm",
        items: [
            { type: "settings", position: "label-left", labelWidth: 80, inputWidth: 270 },
            { type: "input", name: "pId", label: "上级编号:", disabled: true },
            { type: "input", name: "fullname", label: "上级名称:", disabled: true },
            { type: "input", name: "zlevel", label: "层级:", disabled: true },
            { type: "input", name: "sortcode", label: "排序优先级:" },
            { type: "newcolumn", offset: 20 },
            { type: "input", name: "id", label: "当前编号:", required: true },
            { type: "input", name: "name", label: "名称:", required: true },
            { type: "input", name: "keyvalue", label: "简称:", required: true }
        ]
    };
    var areaForm = new PlgForm(areaForm_opt);
    var setting = {
        view: {
            selectedMulti: false
        },
        async: {
            enable: true,
            url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/zone/get-by-pid",
            contentType: "application/json",
            type: "post",
            autoParam: ["id=fatherid"],
            headers: {
                "Authorization": Prolog.reloadToken()
            },
            dataFilter: filter
        },
        callback: {
            beforeClick,
            beforeAsync,
            onAsyncError,
            onAsyncSuccess,
            beforeExpand
        }
    };

    function filter(treeId, parentNode, childNodes) {
        if (!childNodes.success) return null;
        childNodes.data.forEach(v => {
            v.name = v.zname;
            v.id = v.zoneid;
            v.pId = v.fatherid;
            v.isParent = true;
        });
        return childNodes.data;
    }

    function beforeClick(treeId, treeNode) {
        areaForm.clear();
        areaForm.setFormData(treeNode);

        if (treeNode.id === "newid") {
            $("#updateBtn").hide();
            $("#saveBtn").show();
            areaForm.enableItem("id");
        }
        else {
            $("#updateBtn").show();
            $("#saveBtn").hide();
            areaForm.disableItem("id");
        }
    }

    function beforeExpand() {

    }

    function beforeAsync() {
    }
    function onAsyncError() {
    }
    function onAsyncSuccess() {
    }

    function addNode(e) {
        areaForm.clear();
        $("#updateBtn").hide();
        $("#saveBtn").show();
        var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            silent = e.data.silent,
            nodes = zTree.getSelectedNodes();
        var treeNode = nodes[0];
        if (treeNode) {
            treeNode = zTree.addNodes(treeNode, -1, { id: "newid", pId: treeNode.id, fullname: treeNode.name, zlevel: treeNode.zlevel + 1, isParent: true, name: "new node" });
        }
        else {
            treeNode = zTree.addNodes(null, -1, { id: "newid", pId: null, isParent: true, name: "new node" }, silent);
        }
    }

    function removeNode() {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            nodes = zTree.getSelectedNodes(),
            treeNode = nodes[0];
        if (!treeNode) {
            PlgDialog.msg("请先选择一个节点！");
            return;
        }
        var znode = {
            zoneid: treeNode.zoneid
        };
        if (treeNode.id == "newid"){
            zTree.removeNode(treeNode);
            return;
        }
        PlgDialog.confirm("确认删除吗？", function (index) {
            PlgDialog.close(index);
            //加载等待
            var pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
            areaForm.clear();
            var paramData = [];
            paramData.push(znode);
            Prolog.ajax({
                url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/zone/delete-zone",
                type: "put",
                contentType: "application/json",
                data: JSON.stringify(paramData),
                success: function (data) {
                    PlgDialog.close(pload);
                    if (data.success) {
                        layer.msg(data.message);
                        zTree.removeNode(treeNode);
                    }
                },
                error: function () { PlgDialog.close(pload); PlgDialog.msg("接口调用失败"); }
            });
        });
    }

    function updateData() {
        var formData = areaForm.getFormData();
        if (formData.id == "newid") {
            PlgDialog.msg("newid 不能做为编号使用");
            return;
        }
        if (!areaForm.validate()) return;
        //加载等待
        var pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
        var newData = {
            zoneid: formData.id,
            zname: formData.name,
            fatherid: formData.pId,
            zlevel: formData.zlevel,
            keyvalue: formData.keyvalue,
            fullname: formData.fullname,
            sortcode: formData.sortcode
        };
        Prolog.ajax({
            url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/zone/updateZone",
            type: "post",
            contentType: "application/json",
            data: JSON.stringify(newData),
            success: function (data) {
                PlgDialog.close(pload);
                if (data.success) {
                    $.fn.zTree.init($("#treeDemo"), setting);
                    areaForm.clear();
                }
                else {
                    setTimeout(() => { PlgDialog.alert(data.message == null ? "接口调用失败" : data.message); }, 300);
                }
            },
            error: function () { PlgDialog.close(pload); PlgDialog.msg("接口调用失败"); }
        });
    }

    function saveData() {
        var formData = areaForm.getFormData();
        if (formData.id == "newid") {
            PlgDialog.msg("newid 不能做为编号使用");
            return;
        }
        if (!areaForm.validate()) return;
        //加载等待
        var pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
        var newData = {
            zoneid: formData.id,
            zname: formData.name,
            fatherid: formData.pId,
            zlevel: formData.zlevel || 1,
            keyvalue: formData.keyvalue,
            fullname: formData.fullname
        };
        Prolog.ajax({
            url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/zone/addZone",
            type: "post",
            contentType: "application/json",
            data: JSON.stringify(newData),
            success: function (data) {
                PlgDialog.close(pload);
                if (data.success) {
                    $.fn.zTree.init($("#treeDemo"), setting);
                    areaForm.clear();
                }
                else {
                    setTimeout(() => { PlgDialog.alert(data.message == null ? "接口调用失败" : data.message); }, 300);
                }
            },
            error: function () { PlgDialog.close(pload); PlgDialog.msg("接口调用失败"); }
        });
    }

    $(document).ready(function () {
        $.fn.zTree.init($("#treeDemo"), setting);
    });

    // 增加节点
    $("#addBtn").bind("click", { type: "add", silent: false }, addNode);
    // 删除节点
    $("#delBtn").bind("click", removeNode);
    // 修改/更新
    $("#updateBtn").bind("click", updateData);
    // 保存
    $("#saveBtn").bind("click", saveData);

};