/**
 * guoj
 * 2019.09.30
 * 打印机配置
 */
 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;
  //获取下拉框
  getSelectGJ([{ "objectcode": "sys_print_mst_status" }, { "objectcode": "sys_print_mst_printertype" }], function (data) {
    $("#printDispose_select").attr("consignor", JSON.stringify(data.data));
  });
  var opts = {
    //terrace显示全部，card分页显示
    skin: "card",
    columns: [
      { id: "printerid", name: "id", type: "ro", width: "100", hidden: true },
      { id: "printercode", name: "打印机编号", type: "ro", width: 150 },
      { id: "printername", name: "打印机名称", type: "ro", width: 150 },
      {
        id: "branchid", name: "仓库名称", type: "ro", width: 100
      },
      { id: "tcp", name: "TCP/IP地址", type: "ro", width: 100 },
      {
        id: "status", name: "打印机状态", type: "ro", width: 100, render: function (v) {
          var dataSelete = JSON.parse($("#printDispose_select").attr("consignor"));
          return getSelectValue(dataSelete.sys_print_mst_status, v);
        }
      },
      {
        id: "printertype", name: "打印机类型", type: "ro", width: 100, render: function (v) {
          var dataSelete = JSON.parse($("#printDispose_select").attr("consignor"));
          return getSelectValue(dataSelete.sys_print_mst_printertype, v);
        }
      },
      { id: "createby", name: "创建人", type: "ro", width: 100 },
      { id: "createdate", name: "创建时间", type: "ro", width: 130 },
      {
        id: "op", name: "操作", type: "button", align: "center", fixed: "right", width: 200, render: function () {
          return "删除^printDispose_deleteClick,修改^printDispose_updataClick,详情^printDispose_details";
        }
      }
    ],
    multiselect: true,
    fixedLast: true, //开启固定列，固定最后一行，倒数第二列不能隐藏
    toolbar: [
      { type: "newcolumn", offset: 10 },
      { type: "input", name: "condition", 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" }],

    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sys-print-mst/lists",//数据接口
    type: "post",//数据提交方式，默认为get
    pagebar: {
      buttons: [
        { text: "删除", className: "layui-btn-danger", name: "delete" }
      ]
    },
    contentType: "application/json",
    params: JSON.stringify({ "pageNum": 1, "pageSize": 10 }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  var grid = new PlgGrid(opts);
  grid.renderTo("printDispose_guojzhuans2");
  $("#printDispose_guojzhuans2").addClass("layui-form");
  grid.attachEvent("onDataReady", function () {
    setTimeout(function () {
      layui.form.render();
    }, 200);
  });
  grid.loadData();
  //单击事件
  grid.on("onRowSelect", function (id) {
    guodids = id;
  });
  //按钮添加方法
  grid.attachToolBarEvent("onButtonClick", function (name) {
    if (name == "querywork") {
      var fdata = grid.getToolBarForm().getFormData();

      grid.setParams(JSON.stringify(fdata));
      //grid.setParams(JSON.stringify(fdata));
      grid.reload();
    }
    if (name == "addBtn") {
      $("#printDispose_div1").css("display", "block");
      $("#printDispose_div3").css("display", "none");
      $("#printDispose_div9").css("display", "none");
      ddfrom.clear();
      ddfrom.enableAllItem();
      $("#printDispose_title").html("新增");
    }
    if (name == "work_Reset") {
      grid.getToolBarForm().clear();
    }
  });


  //按钮添加方法
  grid.attachPageBarEvent(function (name) {
    //删除
    if (name == "delete") {

      let ids = grid.getCheckedIds();
      if (ids.length < 1) {
        return;
      }
      utilDelete(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sys-print-mst/delete", "post", { ids: ids }, function (data) {
        setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
        grid.reload();
      }, "application/x-www-form-urlencoded");
    }
  });
  //删除
  window.printDispose_deleteClick = function (rid) {
    utilDelete(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sys-print-mst/delete", "post", { ids: rid }, function (data) {
      setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
      grid.reload();
    }, "application/x-www-form-urlencoded");
  };
  //修改
  window.printDispose_updataClick = function (rid, data) {
    grid.selectRowById(rid);
    ddfrom.clear();
    ddfrom.disableItem("printId");
    ddfrom.setFormData(data);
    $("#printDispose_div1").css("display", "block");
    $("#printDispose_div3").css("display", "none");
    $("#printDispose_div9").css("display", "none");
    $("#printDispose_title").html("编辑");
  };
  //详情
  window.printDispose_details = function (rid, rdata) {
    var dataSelete = JSON.parse($("#printDispose_select").attr("consignor"));
    grid.selectRowById(rid);
    $("#printDispose_div1").css("display", "none");
    $("#printDispose_div3").css("display", "none");
    $("#printDispose_div9").css("display", "block");
    new PlgPanel({
      renderer: "printDispose_div422",
      skin: 2,
      header: {
        isShow: true,
        title: "基本信息"
      },
      defaultBody: {
        layoutCol: 3,
        valueBj: true,
        cols: [
          [{
            label: "打印机编号",
            value: rdata.printercode
          },
          {
            label: "打印机名称",
            value: rdata.printername
          },
          {
            label: "仓库名称",
            value: rdata.branchid
          },
          {
            label: "TCP/IP地址",
            value: rdata.tcp
          }],
          [
            {
              label: "打印机状态",
              value: getSelectValue(dataSelete.sys_print_mst_status, rdata.status)
            },
            {
              label: "打印机类型",
              value: getSelectValue(dataSelete.sys_print_mst_printertype, rdata.printertype)
            },
            {
              label: "创建时间",
              value: rdata.createdate
            },
            {
              label: "创建人",
              value: rdata.createby
            }
          ]
        ]
      }
    });
  };
  //跳转页面
  var carousel = layui.carousel;
  var car = carousel.render({
    elem: "#printDispose_div2",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", //设置容器宽度
    height: "100%"
  });
  //跳转方法
  var tt = "printDispose_div3";
  function changePanel() {
    car.slide(tt);
    tt = tt == "printDispose_div1" ? "printDispose_div3" : "printDispose_div1";
  }
  ddfrom = $("#printDispose_div4").initPlgForm({
    items: [
      { type: "settings", position: "label-left", width: 300, labelWidth: 90 },
      { type: "hidden", name: "printerid", label: "id:", width: 230 },
      { type: "input", name: "printercode", label: "打印机编号:", width: 230, editable: false, validate: "NotEmpty", required: true },
      { type: "input", name: "tcp", label: "TCP/IP地址:", width: 230 },
      { type: "newcolumn", offset: 10 },
      { type: "input", name: "printername", label: "打印机名称:", width: 230, validate: "NotEmpty", required: true },
      {
        type: "select", name: "printertype", label: "打印机类型:", width: 230, options: JSON.parse($("#printDispose_select").attr("consignor")).sys_print_mst_printertype
      },
      { type: "newcolumn", offset: 10 },
      {
        type: "select", name: "status", label: "打印机状态:", width: 230, options: JSON.parse($("#printDispose_select").attr("consignor")).sys_print_mst_status
      }
    ]
  });
  //返回
  $(document).on("click", "#printDispose_return", function () {
    $("#printDispose_div1").css("display", "none");
    $("#printDispose_div3").css("display", "block");
    $("#printDispose_div9").css("display", "none");
  });
  //保存
  document.querySelector("#printDispose_save").onclick = function () {
    //数据data
    var data = ddfrom.getFormData();
    var feik = ddfrom.validate();
    if (!feik) {
      return;
    }
    //请求地址和类型
    var url = UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sys-print-mst/update";
    var type = "post";
    var contentType = "application/json";
    //message新增为0，修改为1
    if (data.printerid == null || data.printerid == "") {
      url = UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sys-print-mst/create";
    }
    //调用ajax方法
    utilAjaxGJ(url, type, JSON.stringify(data), function (data) {
      setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
      $("#printDispose_div1").css("display", "none");
      $("#printDispose_div3").css("display", "block");
      $("#printDispose_div9").css("display", "none");
      grid.reload();
    }, contentType);
  };
}
