/**
 * guoj
 * 2018.10.10
 * 承运商
 */
 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;
  //省
  var province;
  //查询省
  utilSyncAjaxGJ(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/zone/get-by-pid", "post", JSON.stringify({ "fatherid": "01" }), function (data) {
      province = data.data;
      for (x in province) {
          province[x].text = province[x].keyvalue;
          province[x].value = province[x].zoneid;
      }
      province.unshift({ "text": "", "value": "" });
  }, "application/json");
  //分页参数
  var carr = { "pageSize": 10, "pageNum": 1, "carrierid": "%" };
  var opts = {
      //terrace显示全部，card分页显示
      skin: "card",
      //renderer:"grid",
      columns: [
          { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
          { id: "companycode", name: "承运商编号", type: "ro", width: "100" },
          { id: "companyname", name: "承运商名称", type: "ro", width: "100" },
          { id: "groupno1", name: "所在区域", type: "ro", width: "80" },
          {
              id: "province", name: "省", type: "ro", width: "80", render: function (v, rd) {
                  return rd["provincename"];
              }
          },
          {
              id: "city", name: "市", type: "ro", width: "80", render: function (v, rd) {
                  return rd["cityname"];
              }
          },
          {
              id: "area", name: "区", type: "ro", width: "80", render: function (v, rd) {
                  return rd["areaname"];
              }
          },
          { id: "address", name: "详细地址", type: "ro", width: "80" },
          { id: "contactman", name: "联系人", type: "ro", width: "80" },
          { id: "createdate", name: "创建时间", type: "ro", width: "130", hidden: true },
          { id: "createby", name: "创建人", type: "ro", width: "60", hidden: true },
          { id: "lastmodifydate", name: "最后修改时间", type: "ro", width: "130" },
          {
              id: "op", name: "操作", type: "button", align: "left", fixed: "right", width: "170", render: function () {
                  return "复制^carrier_copyClick,删除^carrier_deleteClick,修改^carrier_updataClick,详情^carrier_details";
              }
          }
      ],
      multiselect: true,
      fixedLast: true,
      toolbar: [
          { type: "newcolumn", offset: 10 },
          { type: "input", name: "carriername", label: "承运商名称/编号:", width: 260 },
          { type: "newcolumn", offset: 10 },
          { type: "button", name: "querycarr", value: "查询" }, { type: "newcolumn" },
          { type: "button", name: "car_Reset", className: " normal", value: "重置" }, { type: "newcolumn" },
          { type: "button", name: "carr_imp", offsetLeft: 10, className: "plg-toolbar-right normal", 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>新增" }, { type: "newcolumn" }],
      imagePath: "/prologui/dist/images/PlgGrid",
      url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/company/carries",//数据接口
      type: "post",//数据提交方式，默认为get
      pagebar: {
          buttons: [
              { text: "删除", className: "layui-btn-danger layui-btn", name: "delete" }
          ]
          // type: 'min' //full simple
      },
      contentType: "application/json",
      params: JSON.stringify(carr),//查询提交参数，分页参数默认为pageNum,pageSize
      page: true, //是否启用分页
      totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  var grid = new PlgGrid(opts);
  grid.renderTo("guojzhuans");
  grid.loadData();

  //按钮添加方法
  grid.attachToolBarEvent("onButtonClick", function (name) {
      if (name == "querycarr") {
          var fdata = grid.getToolBarForm().getFormData();
          grid.setParams(JSON.stringify(fdata));
          grid.reload();
      }
      if (name == "addBtn") {
          ddfrom.clear();
          $("#carrier_title").html("新增");
          $("#carrier_div3").css("display", "none");
          $("#carrier_div1").css("display", "block");
          $("#carrier_div9").css("display", "none");
          $("#carrier_Template").css("display", "none");
          ddfrom.enableItem("companycode");
          ddfrom.enableItem("companyname");
      }
      if (name == "carr_imp") {
          exportExcel();
      }
      if (name == "download") {
          window.open("/download/单位资料导入模板.xlsx");
      }
      if (name == "car_Reset") {
          grid.getToolBarForm().clear();
      }
  });

  utilImportGJ2("carrier_Template1", "carrierImportId", "carrierExportId", "url", function () {
      $("#carrier_div1").css("display", "none");
      $("#carrier_div3").css("display", "block");
      $("#carrier_div9").css("display", "none");
      $("#carrier_Template").css("display", "none");
  });


  function exportExcel() {

      var fdata = grid.getToolBarForm().getFormData();
      var obj = JSON.parse(JSON.stringify(fdata));

      var url = UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/company/export-carriers?carriername=" + encodeURI(obj.carriername);

      var token = localStorage.getItem("authorization");

      if (token == null || token == "") {
          PlgDialog.msg("登录状态异常，请重新登录！");
          window.location.href = "/menu?path=login";
          return;
      }

      var xhh = new XMLHttpRequest();

      xhh.open("get", url);
      xhh.setRequestHeader("Authorization", token);
      xhh.responseType = "blob";
      xhh.onreadystatechange = function () {
          if (xhh.readyState === 4 && xhh.status === 200) {
              var filename = "承运商资料" + new Date().getTime() + ".xls";
              var blob = new Blob([xhh.response], { type: "text/xls" });
              var csvUrl = URL.createObjectURL(blob);
              var link = document.createElement("a");
              link.href = csvUrl;
              link.download = filename;
              link.click();
          }
      };
      xhh.send();

  }

  //按钮添加方法
  grid.attachPageBarEvent(function (name) {
      //删除
      if (name == "delete") {
          var data = [];
          var datas = getDeleteAll(grid);
          datas.forEach(v => {
              data.push({ companyid: v });
          });
          data = JSON.stringify(data);
          utilDelete(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/company/batch-delete", "post", data, function (data) {
              layer.closeAll();
              setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
              grid.reload();
          }, "application/json");
      }

  });
  //复制
  window.carrier_copyClick = function (rid, data) {
      data.companyid = null;
      data.companycode = null;
      data.companyname = null;
      ddfrom.clear();
      $("#carrier_title").html("新增");
      $("#carrier_div3").css("display", "none");
      $("#carrier_div1").css("display", "block");
      $("#carrier_div9").css("display", "none");
      $("#carrier_Template").css("display", "none");
      ddfrom.setFormData(data);
      ddfrom.enableItem("companycode");
      ddfrom.enableItem("companyname");
      linkage(1, data);
  };
  //删除
  window.carrier_deleteClick = function (rid) {
      grid.selectRowById(rid);
      var data = [];
      var datas = getDeleteAll(grid);
      datas.forEach(v => {
          data.push({ companyid: v });
      });
      data = JSON.stringify(data);
      utilDelete(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/company/batch-delete", "post", data, function (data) {
          layer.closeAll();
          setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
          grid.reload();
      }, "application/json");
  };
  //修改
  window.carrier_updataClick = function (rid, data) {
      ddfrom.clear();
      $("#carrier_title").html("编辑");
      $("#carrier_div3").css("display", "none");
      $("#carrier_div1").css("display", "block");
      $("#carrier_div9").css("display", "none");
      $("#carrier_Template").css("display", "none");
      ddfrom.setFormData(data);
      ddfrom.disableItem("companycode");
      ddfrom.disableItem("companyname");
      linkage(1, data);
  };
  //详情
  window.carrier_details = function (rid, rdata) {
      $("#carrier_div1").css("display", "none");
      $("#carrier_div3").css("display", "none");
      $("#carrier_div9").css("display", "block");
      $("#carrier_Template").css("display", "none");
      new PlgPanel({
          renderer: "carrier_div422",
          skin: 2,
          header: {
              isShow: true,
              title: "基本信息"
          },
          defaultBody: {
              layoutCol: 3,
              valueBj: true,
              cols: [
                  [{
                      label: "承运商编号",
                      value: rdata.companycode
                  },
                  {
                      label: "承运商名称",
                      value: rdata.companyname
                  },
                  {
                      label: "所在区域",
                      value: rdata.groupno1
                  },
                  {
                      label: "联系人",
                      value: rdata.contactman
                  }],
                  [
                      {
                          label: "省",
                          value: rdata.provincename
                      },
                      {
                          label: "市",
                          value: rdata.cityname
                      },
                      {
                          label: "区",
                          value: rdata.areaname
                      },
                      {
                          label: "详细地址",
                          value: rdata.address
                      }
                  ],
                  [
                      {
                          label: "创建时间",
                          value: rdata.createdate
                      },
                      {
                          label: "创建人",
                          value: rdata.createname
                      },
                      {
                          label: "修改时间",
                          value: rdata.lastmodifydate
                      }
                  ]
              ]
          }
      });
  };
  //三级联动
  function linkage(type, data) {
      var f = ddfrom;
      if (type == 0) {

          f.attachComboEvent("province", "onChange", function (id) {
              f.getCombo("city").setComboText("");
              f.getInput("provincename").value = f.getCombo("province").getComboText();
              f.loadComboData("city", { url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/zone/get-by-pid", type: "post", contentType: "application/json", data: JSON.stringify({ "fatherid": id }), valueField: "zoneid", textField: "keyvalue" });
          });
          f.attachComboEvent("city", "onChange", function (id) {
              f.getCombo("area").setComboText("");
              f.getInput("cityname").value = f.getCombo("city").getComboText();
              f.loadComboData("area", { url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/zone/get-by-pid", type: "post", contentType: "application/json", data: JSON.stringify({ "fatherid": id }), valueField: "zoneid", textField: "keyvalue" });
          });
          f.attachComboEvent("area", "onChange", function () {
              f.getInput("areaname").value = f.getCombo("area").getComboText();
          });
      }
      if (type == 1) {
          f.loadComboData("city", { url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/zone/get-by-pid", type: "post", contentType: "application/json", data: JSON.stringify({ "fatherid": data.province }), valueField: "zoneid", textField: "keyvalue" });
          f.getCombo("city").setComboText(data.cityname == "" ? data.city : data.cityname);
          f.loadComboData("area", { url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/zone/get-by-pid", type: "post", contentType: "application/json", data: JSON.stringify({ "fatherid": data.city }), valueField: "zoneid", textField: "keyvalue" });
          f.getCombo("area").setComboText(data.areaname == "" ? data.area : data.areaname);
      }
  }

  //滑屏
  var carousel = layui.carousel;
  carousel.render({
      elem: "#carrier_div2",
      autoplay: false,
      index: 0,
      arrow: "none",
      indicator: "none",
      anim: "fade",
      width: "100%", //设置容器宽度
      height: "100%"
  });

  //返回
  $(document).on("click", "#carrier_return", function () {
      $("#carrier_div3").css("display", "block");
      $("#carrier_div1").css("display", "none");
      $("#carrier_div9").css("display", "none");
      $("#carrier_Template").css("display", "none");
  });
  //新增或修改from页面
  ddfrom = $("#carrier_div4").initPlgForm({
      items: [
          { type: "settings", position: "label-left", labelWidth: 80 },
          { type: "hidden", name: "companyid", label: "id:", width: 230 },
          { type: "newcolumn", offset: 30 },
          { type: "input", name: "companycode", label: "承运商编号:", width: 260, editable: false, validate: "NotEmpty", required: true },
          { type: "input", name: "companyname", label: "承运商名称:", width: 260, validate: "NotEmpty", required: true },
          { type: "input", name: "address", label: "地址:", width: 260 },
          { type: "newcolumn", offset: 30 },
          {
              type: "combo", readonly: true, name: "province", label: "省:", width: 260, options: province
          },
          {
              type: "combo", readonly: true, name: "city", label: "市:", width: 260
          },
          {
              type: "combo", readonly: true, name: "area", label: "区:", width: 260
          },
          { type: "hidden", name: "provincename", label: "省:", width: 260 },
          { type: "hidden", name: "cityname", label: "市:", width: 260 },
          { type: "hidden", name: "areaname", label: "区:", width: 260 },
          { type: "newcolumn", offset: 30 },
          { type: "input", name: "groupno1", label: "所在区域:", width: 260 },
          { type: "hidden", name: "companytype", label: "类型:", width: 260, value: "Y" },
          { type: "input", name: "contactman", label: "联系人:", width: 260 }]
  });
  //保存
  document.querySelector("#carrier_save").onclick = function () {
      //数据data
      var data = ddfrom.getFormData();
      data.companytype = "Y";
      //请求地址和类型
      var url = UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/company/update-comany";
      var type = "post";
      var contentType = "application/json";
      //companyid新增为null
      if (data.companyid == null || data.companyid == "") {
          data.companyid = data.companycode;
          url = UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/company/batch-create";
          data = {list: [data]};
      }
      //调用ajax方法
      utilAjaxGJ(url, type, JSON.stringify(data), function (data) {
          setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
          grid.reload();
          $("#carrier_div3").css("display", "block");
          $("#carrier_div1").css("display", "none");
          $("#carrier_div9").css("display", "none");
          $("#carrier_Template").css("display", "none");
      }, contentType);
  };
  linkage(0, "");
};