/**
 * guoj
 * 2018.10.15
 * 工作台
 */
 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 ddfrom = null;

    //获取下拉框
    getSelectGJ([{ "objectcode": "ws_status" }, { "objectcode": "stationattr1" }, { "objectcode": "stationattr2" },
    { "objectcode": "stationtype" }], function (data) {
        data.data.stationattr2.unshift({ "text": "", "value": "" });
        $("#workbench_select").attr("consignor", JSON.stringify(data.data));
    });
    var bench = { "pageSize": 10, "pageNum": 1 };
    var opts = {
        //terrace显示全部，card分页显示
        skin: "card",
        //renderer:"grid",
        columns: [
            { id: "stationid", name: "id", type: "ro", width: "100", hidden: true },
            { id: "stationcode", name: "工作站编号", type: "ro", width: 150 },
            { id: "stationname", name: "工作站名称", type: "ro", width: 150 },
            {
                id: "status", name: "状态", type: "ro", width: 100, render: function (v, rd) {
                    if (rd["status"] == 0) {
                        return Prolog.getCircle("red") + "作废";
                    }
                    if (rd["status"] == 10) {
                        return Prolog.getCircle("green") + "可使用";
                    }
                    if (rd["status"] == 20) {
                        return Prolog.getCircle("yellow") + "锁定";
                    }
                }
            },
            { id: "groupcode", name: "分组号", type: "ro", width: 100 },
            { id: "ceilingtasknum", name: "任务上限", type: "ro", width: 100 },
            {
                id: "stationattr1", name: "复核台类型", type: "ro", width: 100, render: function (v, rd) {
                    if (rd["stationattr1"] == 0) {
                        return "普通复核台";
                    }
                    if (rd["stationattr1"] == 1) {
                        return "异常剔除口和追加拣货剔除口";
                    }
                    if (rd["stationattr1"] == 2) {
                        return "追加拣货剔除口";
                    }
                    if (rd["stationattr1"] == 3) {
                        return "普通收货台";
                    }
                }
            },
            {
                id: "stationtype", name: "工作站类型", type: "ro", width: 100, render: function (v, rd) {
                    if (rd["stationtype"] == 1) {
                        return "复核打包";
                    }
                    if (rd["stationtype"] == 2) {
                        return "称重复核";
                    }
                }
            },
            {
                id: "stationattr2", name: "复核台类别", type: "ro", width: 100, render: function (v, rd) {
                    if (rd["stationattr2"] == 0) {
                        return "西药";
                    }
                    if (rd["stationattr2"] == 1) {
                        return "中药";
                    }
                    if (rd["stationattr2"] == 2) {
                        return "计生";
                    }
                    if (rd["stationattr2"] == 3) {
                        return "器械";
                    }
                }
            },
            {
                id: "abnormalflag", name: "异常处理工作站", type: "ro", width: 120, render: function (v) {
                    return "<input disabled=\"disabled\" type=\"checkbox\" " + (v == 1 ? "checked=\"\"" : "") + " name=\"chk_ismodify\" lay-skin=\"switch\" lay-filter=\"chk_ismodify\" lay-text=\"ON|OFF\">";
                }
            },
            { id: "createby", name: "创建人", type: "ro", width: 100 },
            { id: "createat", name: "创建时间", type: "ro", width: 130 },
            { id: "branchid", name: "所属仓库", type: "ro", width: 100 },
            {
                id: "op", name: "操作", type: "button", align: "center", fixed: "right", width: 200, render: function () {
                    return "删除^workbench_deleteClick,修改^workbench_updataClick,复制^workbench_copy,详情^workbench_details";
                }
            }
        ],
        multiselect: true,
        fixedLast: true, //开启固定列，固定最后一行，倒数第二列不能隐藏
        toolbar: [
            { type: "newcolumn", offset: 10 },
            { type: "input", name: "stationcode", 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: "exportBtn", className: "plg-toolbar-right", value: "<i class='layui-icon layui-icon-download-circle'></i>导出" }, { type: "newcolumn" },
            { type: "button", name: "addBtn", offsetLeft: 10, className: "plg-toolbar-right", value: "<i class='layui-icon layui-icon-add-1'></i>新增" }],
        imagePath: "/prologui/dist/images/PlgGrid",
        url: "/basic/workbench/list",//数据接口
        type: "post",//数据提交方式，默认为get
        pagebar: {
            buttons: [
                { text: "删除", className: "layui-btn-danger", name: "delete" },
                { text: "锁定", className: "layui-btn-normal", name: "lock" },
                { text: "启用", className: "layui-btn-normal", name: "unlock" }
            ]
            // type: 'simple' //full simple
        },
        contentType: "application/json",
        params: JSON.stringify(bench),//查询提交参数，分页参数默认为pageNum,pageSize
        page: true, //是否启用分页
        totalCount: "totalCount" //总记录数字段，默认totalCount
    };
    //分页参数 pageSize pageNum
    var grid = new PlgGrid(opts);
    grid.renderTo("workbench_guojzhuans2");
    $("#workbench_guojzhuans2").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(JSON.stringify(fdata));
            grid.reload();
        }
        if (name == "addBtn") {
            setViewName("#workbench_div1");
            ddfrom.enableAllItem();
            ddfrom.clear();
            ddfrom.setFormData({ "status": "10" });
            $("#workbench_title").html("新增");
        }
        if (name == "impandexpBtn") {
            setViewName("#workbench_Template");
        }
        if (name == "download") {
            window.open("/download/工作台模板.xlsx");
        }
        if (name == "work_Reset") {
            grid.getToolBarForm().clear();
        }
        if (name == "exportBtn") {
            unfilledWorktime_export();
        }
    });

    //主界面导出事件
    function unfilledWorktime_export() {
        let fdata = grid.getToolBarForm().getFormData();
        let obj = JSON.parse(JSON.stringify(fdata));
        let url = "/basic/workbench/export?stationcode=" + encodeURI(obj.stationcode);
        let token = localStorage.getItem("authorization");
        if (token == null || token == "") {
            PlgDialog.msg("登录状态异常，请重新登录！");
            window.location.href = "/menu?path=login";
            return;
        }
        let xhh = new XMLHttpRequest();
        xhh.open("get", url);
        xhh.setRequestHeader("Authorization", token);
        xhh.responseType = "blob";
        xhh.onreadystatechange = function () {
            if (xhh.readyState === 4 && xhh.status === 200) {
                let filename = "工作台" + new Date().getTime() + ".xlsx";
                let blob = new Blob([xhh.response], { type: "text/xlsx" });
                let csvUrl = URL.createObjectURL(blob);
                let link = document.createElement("a");
                link.href = csvUrl;
                link.download = filename;
                link.click();
            }
        };
        xhh.send();

    }

    utilImportGJ2("workbench_Template1", "workbenchImportId", "workbenchExportId", "url", function () {
        setViewName("#workbench_div3");
    });
    //按钮添加方法
    grid.attachPageBarEvent(function (name) {
        //删除
        if (name == "delete") {
            let data = [];
            let datas = getDeleteAll(grid);
            datas.forEach(v => {
                data.push({ stationid: v });
            });
            data = JSON.stringify(data);
            utilDelete("/basic/workbench/batch-delete", "put", data, function (data) {
                setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
                grid.reload();
            }, "application/json");
        } //锁定
        if (name == "lock") {
            let data = [];
            let datas = getDeleteAll(grid);
            datas.forEach(v => {
                data.push({ stationid: v, status: "20" });
            });
            data = JSON.stringify(data);
            utilAjaxGJ("/basic/workbench/update-status", "put", data, function (data) {
                setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
                grid.reload();
            }, "application/json");
        }
        //启用
        if (name == "unlock") {
            let data = [];
            let datas = getDeleteAll(grid);
            datas.forEach(v => {
                data.push({ stationid: v, status: "10" });
            });
            data = JSON.stringify(data);
            utilAjaxGJ("/basic/workbench/update-status", "put", data, function (data) {
                setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
                grid.reload();
            }, "application/json");
        }
    });
    //删除
    window.workbench_deleteClick = function (rid) {
        grid.selectRowById(rid);
        let data = [];
        let datas = getDeleteAll(grid);
        datas.forEach(v => {
            data.push({ stationid: v });
        });
        data = JSON.stringify(data);
        utilDelete("/basic/workbench/batch-delete", "put", data, function (data) {
            setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
            grid.reload();
        }, "application/json");
    };
    //修改
    window.workbench_updataClick = function (rid, data) {
        ddfrom.enableAllItem();
        grid.selectRowById(rid);
        ddfrom.clear();
        ddfrom.setFormData(data);
        setViewName("#workbench_div1");
        $("#workbench_title").html("编辑");
        ddfrom.disableItem("stationcode");
        ddfrom.disableItem("stationname");
    };
    //复制
    window.workbench_copy = function (rid, data) {
        ddfrom.enableAllItem();
        grid.selectRowById(rid);
        ddfrom.clear();
        data.stationcode = "";
        data.stationname = "";
        data.stationid = "";
        ddfrom.setFormData(data);
        setViewName("#workbench_div1");
        ddfrom.setFormData({ "status": "10" });
        $("#workbench_title").html("新增");
    };
    //详情
    window.workbench_details = function (rid, rdata) {
        var dataSelete = JSON.parse($("#workbench_select").attr("consignor"));
        grid.selectRowById(rid);
        setViewName("#workbench_div9");
        new PlgPanel({
            renderer: "workbench_div422",
            skin: 2,
            header: {
                isShow: true,
                title: "基本信息"
            },
            defaultBody: {
                layoutCol: 3,
                valueBj: true,
                cols: [
                    [{
                        label: "工作站编号",
                        value: rdata.stationcode
                    },
                    {
                        label: "工作站名称",
                        value: rdata.stationname
                    },
                    {
                        label: "状态",
                        value: getSelectValue(dataSelete.ws_status, rdata.status)
                    },
                    {
                        label: "分组号",
                        value: rdata.groupcode
                    }],
                    [
                        {
                            label: "任务上限",
                            value: rdata.ceilingtasknum
                        },
                        {
                            label: "复核台类型",
                            value: getSelectValue(dataSelete.stationattr1, rdata.stationattr1)
                        },
                        {
                            label: "工作站类型",
                            value: getSelectValue(dataSelete.stationtype, rdata.stationtype)
                        },
                        {
                            label: "复核台类别",
                            value: getSelectValue(dataSelete.stationattr2, rdata.stationattr2)
                        }
                    ],
                    [
                        {
                            label: "异常工作站",
                            value: rdata.abnormalflag == 0 ? "否" : "是"
                        },
                        {
                            label: "创建人",
                            value: rdata.createby
                        },
                        {
                            label: "创建时间",
                            value: rdata.createat
                        },
                        {
                            label: "所属仓库",
                            value: rdata.branchid
                        }
                    ]
                ]
            }
        });
    };
    //跳转页面
    var carousel = layui.carousel;
    carousel.render({
        elem: "#workbench_div2",
        autoplay: false,
        index: 0,
        arrow: "none",
        indicator: "none",
        anim: "fade",
        width: "100%", //设置容器宽度
        height: "100%"
    });
    let dataView = ["#workbench_div1","#workbench_div3","#workbench_div9","#workbench_Template"];
    function setViewName(name){
        for(var i = 0;i < dataView.length;i++){
            if(name == dataView[i]){
                $(name).css("display", "block");
            }
            else{
                $(dataView[i]).css("display", "none");
            }
        }
    }
    ddfrom = $("#workbench_div4").initPlgForm({
        items: [
            { type: "settings", position: "label-left", width: 300, labelWidth: 90 },
            { type: "hidden", name: "stationid", label: "id:", width: 230 },
            { type: "input", name: "stationcode", label: "工作站编号:", width: 230, editable: false, validate: "NotEmpty", required: true },
            { type: "input", name: "stationname", label: "工作站名称:", width: 230, validate: "NotEmpty", required: true },
            {
                type: "select", name: "status", label: "状态 :", width: 230, options: JSON.parse($("#workbench_select").attr("consignor")).ws_status
            },
            { type: "newcolumn", offset: 10 },
            { type: "input", name: "groupcode", label: "分组号:", width: 230 },
            { type: "input", name: "ceilingtasknum", label: "任务上限:", width: 230 },
            {
                type: "select", name: "stationattr1", label: "复核台类型:", width: 230, options: JSON.parse($("#workbench_select").attr("consignor")).stationattr1
            },
            { type: "newcolumn", offset: 10 },
            {
                type: "select", name: "stationtype", label: "工作站类型:", width: 230, options: JSON.parse($("#workbench_select").attr("consignor")).stationtype
            },
            {
                type: "select", name: "stationattr2", label: "复核台类别:", width: 230, options: JSON.parse($("#workbench_select").attr("consignor")).stationattr2
            },
            { type: "checkbox", name: "abnormalflag", label: "异常处理工作站:", width: 230 }]
    });
    //返回
    $(document).on("click", "#workbench_return", function () {
        setViewName("#workbench_div3");
    });
    //保存
    document.querySelector("#workbench_save").onclick = function () {
        //数据data
        var data = ddfrom.getFormData();
        data.branchid = userGJ.p_companyid;
        var feik = ddfrom.validate();
        if (!feik) {
            return;
        }
        //请求地址和类型
        var url = "/basic/workbench/update";
        var type = "put";
        var contentType = "application/json";
        //message新增为0，修改为1
        if (data.stationid == null || data.stationid == "") {
            data.stationid = data.stationcode;
            url = "/basic/workbench/create";
            type = "post";
        }
        //调用ajax方法
        utilAjaxGJ(url, type, JSON.stringify(data), function (data) {
            setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
            setViewName("#workbench_div3");
            grid.reload();
        }, contentType);
    };
};
