/**
* guoj
* 2019.04.17
* 收货定义
*/
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 () => {
    var grid = null;
    var ddfrom = null;
    var subsegtwoSelect;
    //获取下拉框
    getSelectGJ([{ "objectcode": "subsegtwo" }], function (data) {
        subsegtwoSelect = data.data.subsegtwo;
    });
    var opts = {
        //terrace显示全部，card分页显示
        skin: "card",
        //renderer:"grid",
        columns: [
            { id: "id", name: "id", type: "ro", width: 100, hidden: true },
            { id: "datacode", name: "仓名称", type: "ro", width: 100 },
            { id: "subsegone", name: "收货时间段", type: "ro", width: 120 },
            { id: "subsegtwo", name: "分组", type: "ro", width: 100 },
            { id: "valueparmone", name: "计算顺序", type: "ro", align: "right", width: 100 },
            { id: "storevalue", name: "上限量", type: "ro", align: "right", width: 100 },
            { id: "valueparmthree", name: "上限超额比例", type: "ro", align: "right", width: 100 },
            { id: "remarkinfoname", name: "创建人", type: "ro", width: 100 },
            { id: "createdate", name: "创建时间", type: "ro", width: 130 },
            {
                id: "op", name: "操作", type: "button", align: "center", fixed: "right", width: 170, render: function () {
                    return "删除^definition_deleteClick,修改^definition_updataClick,复制^definition_copy,详情^definition_details";
                }
            }
        ],
        multiselect: true,
        fixedLast: true, //开启固定列，固定最后一行，倒数第二列不能隐藏
        toolbar: [
            { type: "newcolumn", offset: 10 },
            { type: "input", name: "storevalue", label: "上限量:", width: 300 },
            { type: "newcolumn", offset: 10 },
            { type: "button", name: "querywork", value: "查询" }, { type: "newcolumn" },
            { type: "button", name: "work_Reset", className: "normal", value: "重置" }, { type: "newcolumn" },
            { type: "button", name: "addBtn", offsetLeft: 10, className: "plg-toolbar-right", value: "<i class='layui-icon layui-icon-add-1'></i>新增" }, { type: "newcolumn" }],
        imagePath: "/prologui/dist/images/PlgGrid",
        url: "/booking/goodsdefined/getGoodsdefined/v1.0",//数据接口
        type: "get",//数据提交方式，默认为get
        pagebar: {
            buttons: [
                { text: "删除", className: "layui-btn-danger", name: "delete" }
            ]
            // type: 'simple' //full simple
        },
        params: {},//查询提交参数，分页参数默认为pageNum,pageSize
        page: true, //是否启用分页
        totalCount: "totalCount" //总记录数字段，默认totalCount
    };
    //分页参数 pageSize pageNum
    grid = new PlgGrid(opts);
    grid.renderTo("definition_table");
    $("#definition_table").addClass("layui-form");
    grid.attachEvent("onDataReady", function () {
        setTimeout(function () {
            layui.form.render();
        }, 200);
    });
    grid.loadData();
    //按钮添加方法
    grid.attachToolBarEvent("onButtonClick", function (name) {
        if (name == "querywork") {
            var fdata = grid.getToolBarForm().getFormData();
            grid.setParams(fdata);
            grid.reload();
        }
        if (name == "addBtn") {
            $("#definition_div1").css("display", "block");
            $("#definition_div3").css("display", "none");
            $("#definition_div9").css("display", "none");
            $("#definition_Template").css("display", "none");
            ddfrom.enableAllItem();
            ddfrom.getDForm().setText("consignorcode1", "");
            ddfrom.clear();
            $("#definition_title").html("新增");
            definitionTimeInit();
        }
        if (name == "work_Reset") {
            grid.getToolBarForm().getDForm().setText("consignorcode1", "");
            grid.getToolBarForm().clear();
        }
    });


    utilImportGJ2("definition_Template1", "definitionImportId", "definitionExportId", "url", function () {
        $("#definition_div1").css("display", "none");
        $("#definition_div3").css("display", "block");
        $("#definition_div9").css("display", "none");
        $("#definition_Template").css("display", "none");
    });
    //按钮添加方法
    grid.attachPageBarEvent(function (name) {
        //删除
        if (name == "delete") {
            var datas = getDeleteAll(grid);
            utilDelete("/booking/goodsdefined/deleteGoodsdefined/v1.0", "post", JSON.stringify(datas), function (data) {
                setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
                grid.reload();
            }, "application/json");
        }
    });
    //删除
    window.definition_deleteClick = function (rid) {
        grid.selectRowById(rid);
        var datas = getDeleteAll(grid);
        utilDelete("/booking/goodsdefined/deleteGoodsdefined/v1.0", "post", JSON.stringify(datas), function (data) {
            setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
            grid.reload();
        }, "application/json");
    };
    //修改
    window.definition_updataClick = function (rid, data) {
        ddfrom.enableAllItem();
        grid.selectRowById(rid);
        ddfrom.getDForm().setText("consignorcode1", "");
        var strconsole = data.subsegone.split("-");
        ddfrom.clear();
        data.End = strconsole[1];
        data.start = strconsole[0];
        ddfrom.setFormData(data);
        ddfrom.getDForm().setText("consignorcode1", data.bankaccountidname);
        definitionTimeInit();
        $("#definition_div1").css("display", "block");
        $("#definition_div3").css("display", "none");
        $("#definition_div9").css("display", "none");
        $("#definition_Template").css("display", "none");
        $("#definition_title").html("编辑");
    };
    //复制
    window.definition_copy = function (rid, data) {
        ddfrom.enableAllItem();
        grid.selectRowById(rid);
        ddfrom.getDForm().setText("consignorcode1", "");
        var strconsole = data.subsegone.split("-");
        ddfrom.clear();
        data.id = "";
        data.End = strconsole[1];
        data.start = strconsole[0];
        ddfrom.setFormData(data);
        $("#definition_div1").css("display", "block");
        $("#definition_div3").css("display", "none");
        $("#definition_div9").css("display", "none");
        $("#definition_Template").css("display", "none");
        $("#definition_title").html("新增");
        definitionTimeInit();
    };
    //详情
    window.definition_details = function (rid, rdata) {
        grid.selectRowById(rid);
        $("#definition_div1").css("display", "none");
        $("#definition_div3").css("display", "none");
        $("#definition_div9").css("display", "block");
        $("#definition_Template").css("display", "none");
        new PlgPanel({
            renderer: "definition_div422",
            skin: 2,
            header: {
                isShow: true,
                title: "基本信息"
            },
            defaultBody: {
                layoutCol: 3,
                valueBj: true,
                cols: [
                    [{
                        label: "仓名称",
                        value: rdata.datacode
                    },
                    {
                        label: "收货时间段",
                        value: rdata.subsegone
                    },
                    {
                        label: "计算顺序",
                        value: rdata.valueparmone
                    },
                    {
                        label: "分组",
                        value: rdata.subsegtwo
                    }],
                    [
                        {
                            label: "上限量",
                            value: rdata.storevalue
                        },
                        {
                            label: "超额比例",
                            value: rdata.valueparmthree
                        },
                        {
                            label: "创建时间",
                            value: rdata.createdate
                        },
                        {
                            label: "创建人",
                            value: rdata.remarkinfoname
                        }
                    ]
                ]
            }
        });
    };
    //跳转页面
    var carousel = layui.carousel;
    carousel.render({
        elem: "#definition_div2",
        autoplay: false,
        index: 0,
        arrow: "none",
        indicator: "none",
        anim: "fade",
        width: "100%", //设置容器宽度
        height: "100%"
    });
    ddfrom = $("#definition_div4").initPlgForm({
        items: [
            { type: "settings", position: "label-left", width: 300, labelWidth: 80 },
            { type: "hidden", name: "id", label: "id:", width: 230 },
            { type: "combo", readonly: true, name: "subsegtwo", label: "分组:", width: 230, options: subsegtwoSelect },
            { type: "input", name: "valueparmone", label: "计算顺序:", width: 230 },
            { type: "newcolumn", offset: 10 },
            { type: "input", name: "start", label: "收货开始时间:", width: 230, dateFormat: "%H:%i", enableTime: true, readonly: true },
            { type: "input", name: "storevalue", label: "上限量:", width: 230 },
            { type: "newcolumn", offset: 10 },
            { type: "input", name: "End", label: "收货结束时间:", width: 230, dateFormat: "%H:%i", enableTime: true, readonly: true },
            { type: "input", name: "valueparmthree", label: "上限超额比例:", width: 230 }
        ]
    });

    var laydate = layui.laydate;
    function definitionTimeInit() {
        let ipt1 = $("#definition_div4 input[name=\"start\"]");
        laydate.render({
            elem: "#" + ipt1[0].id,
            type: "time",
            format: "HH:mm"
        });
        let ipt2 = $("#definition_div4 input[name=\"End\"]");
        laydate.render({
            elem: "#" + ipt2[0].id,
            type: "time",
            format: "HH:mm"
        });
    }
    //返回
    $(document).on("click", "#definition_return", function () {
        $("#definition_div1").css("display", "none");
        $("#definition_div3").css("display", "block");
        $("#definition_div9").css("display", "none");
        $("#definition_Template").css("display", "none");
    });
    //保存
    document.querySelector("#definition_save").onclick = function () {
        //数据data
        var data = ddfrom.getFormData();
        var feik = ddfrom.validate();
        if (!feik) {
            return;
        }
        data.start = $("#definition_div4 input[name=\"start\"]").val();
        data.End = $("#definition_div4 input[name=\"End\"]").val();
        if (data.start > data.End) {
            PlgDialog.msg("开始时间不能大于结束时间");
            return;
        }
        data.subsegone = data.start + "-" + data.End;
        data.datacode = userGJ.p_companyid;
        data.objectcode = "IN_Book_Ability";
        //请求地址和类型
        var url = "/booking/goodsdefined/modifyGoodsdefined/v1.0";
        var type = "post";
        //message新增为0，修改为1
        if (data.id == null || data.id == "") {
            url = "/booking/goodsdefined/newGoodsdefined/v1.0";
        }
        //调用ajax方法
        utilAjaxGJ(url, type, data, function (data) {
            setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
            $("#definition_div1").css("display", "none");
            $("#definition_div3").css("display", "block");
            $("#definition_div9").css("display", "none");
            $("#definition_Template").css("display", "none");
            grid.reload();
        });
    };
};
