/***
 * jiw
 * 2020-08-05
 * 承运商司机管理
 ***/
 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 userinfo = window.localStorage.getItem("userinfo");
  if (userinfo != null) {
    userinfo = JSON.parse(userinfo);
  }
  let fromDataJson;
  var grid;
  //绑定车牌table
  var userTable3 = {
    columns: [
      { id: "vehicleId", name: "id", type: "ro", width: "130", hidden: true },
      { id: "vehicleNo", name: "车牌号", type: "ro" },
      { id: "vehicleName", name: "车辆名称", type: "ro" }
    ],
    multiselect: false,
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseVehicle/queryTBaseVehicle",
    type: "post",
    pagebar: { type: "simple" },
    contentType: "application/json",
    params: JSON.stringify({ companyId: userinfo.p_companyid }),
    page: true,
    totalCount: "totalCount"
  };
  var ddfrom = null;
  var opts = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "id", name: "id", type: "ro", width: "100", hidden: true },
      { id: "userId", name: "手机号", type: "ro" },
      { id: "userName", name: "司机姓名", type: "ro" },
      { id: "companyName", name: "所属单位", type: "ro" },
      { id: "vehicleBindId", name: "绑定车牌", type: "ro" },
      { id: "memo", name: "备注", type: "ro" },
      {
        id: "op", name: "操作", type: "button", align: "center", fixed: "right", width: 200, render: function () {
          return "删除^driverManagementByY_deleteClick,修改^driverManagementByY_updataClick,复制^driverManagementByY_copy,详情^driverManagementByY_details";
        }
      }
    ],
    multiselect: true,
    fixedLast: true,
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "input", className: "layui-col-md3", name: "userId", label: "手机号:" },
          { type: "input", className: "layui-col-md3", name: "userName", label: "司机姓名:" },
          {
            type: "block", className: "layui-col-md2 form_btn_left",
            list: [
              { type: "button", className: "btn-normal", name: "querywork", value: "查 询" },
              { type: "newcolumn" },
              { type: "button", name: "work_Reset", className: "normal", value: "重 置" }
            ]
          },
          {
            type: "block", className: "layui-col-md4 right",
            list: [
              { type: "button", className: "btn-addBtn btn-normal", name: "addBtn", value: "<i class='layui-icon layui-icon-add-1'></i>新 增" }
            ]
          }
        ]
      }],
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sysUserDuty/queryCarrierDriver",
    type: "post",
    pagebar: {
      buttons: [
        { text: "删除", className: "layui-btn-danger", name: "delete" }
      ]
    },
    contentType: "application/json",
    params: JSON.stringify({ companyId: userinfo.p_companyid }),
    page: true,
    totalCount: "totalCount"
  };
  //分页参数 pageSize pageNum
  grid = new PlgGrid(opts);
  grid.renderTo("driverManagementByY_guojzhuans2");
  grid.loadData();

  //按钮添加方法
  grid.attachToolBarEvent("onButtonClick", function (name) {
    if (name == "querywork") {
      var fdata = grid.getToolBarForm().getFormData();
      fdata.companyId = userinfo.p_companyid;
      grid.setParams(JSON.stringify(fdata));
      grid.reload();
    }
    if (name == "addBtn") {
      setView("#driverManagementByY_div1");
      $("#driverManagementByY_div4").html("");
      ddfrom = new PlgForm(fromDataJson);
      $("#driverManagementByY_title").html("新增");
    }
    if (name == "work_Reset") {
      grid.getToolBarForm().clear();
    }
  });
  //按钮添加方法
  grid.attachPageBarEvent(function (name) {
    //删除
    if (name == "delete") {
      let data = [];
      let datas = getDeleteAll(grid);
      datas.forEach(v => {
        data.push({ id: v });
      });
      data = JSON.stringify(data);
      utilDelete(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sysUserDuty/deleteSysUserDuty", "post", data, function (data) {
        grid.reload();
      }, "application/json");
    }
  });
  //删除
  window.driverManagementByY_deleteClick = function (rid) {
    grid.selectRowById(rid);
    let data = [];
    let datas = getDeleteAll(grid);
    datas.forEach(v => {
      data.push({ id: v });
    });
    data = JSON.stringify(data);
    utilDelete(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sysUserDuty/deleteSysUserDuty", "post", data, function (data) {

      grid.reload();
    }, "application/json");
  };
  //修改
  window.driverManagementByY_updataClick = function (rid, data) {
    $("#driverManagementByY_div4").html("");
    ddfrom = new PlgForm(fromDataJson);
    grid.selectRowById(rid);
    data.usercode = data.userId;
    ddfrom.setFormData(data);
    ddfrom.getDForm().setText("buyername5", data.vehicleBindId == null ? "" : data.vehicleBindId);
    setView("#driverManagementByY_div1");
    $("#driverManagementByY_title").html("编辑");
  };
  //复制
  window.driverManagementByY_copy = function (rid, data) {
    $("#driverManagementByY_div4").html("");
    ddfrom = new PlgForm(fromDataJson);
    grid.selectRowById(rid);
    data.userId = "";
    ddfrom.getDForm().setText("buyername5", data.vehicleBindId == null ? "" : data.vehicleBindId);
    ddfrom.setFormData(data);
    setView("#driverManagementByY_div1");
    $("#driverManagementByY_title").html("新增");
  };
  //详情
  window.driverManagementByY_details = function (rid, rdata) {
    grid.selectRowById(rid);
    setView("#driverManagementByY_div9");
    new PlgPanel({
      renderer: "driverManagementByY_div422",
      skin: 2,
      header: {
        isShow: true,
        title: "基本信息"
      },
      defaultBody: {
        layoutCol: 3,
        valueBj: true,
        cols: [
          [{
            label: "手机号",
            value: rdata.userId
          },
          {
            label: "司机姓名",
            value: rdata.userName
          }],
          [
            {
              label: "绑定车牌",
              value: rdata.vehicleBindId
            },
            {
              label: "备注",
              value: rdata.memo
            }
          ]
        ]
      }
    });
  };
  //跳转页面
  var carousel = layui.carousel;
  carousel.render({
    elem: "#driverManagementByY_div2",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", //设置容器宽度
    height: "100%"
  });
  let viewList = ["#driverManagementByY_div1", "#driverManagementByY_div3", "#driverManagementByY_div9"];
  function setView(name) {
    for (let i = 0; i < viewList.length; i++) {
      if (name == viewList[i]) {
        $(name).css("display", "block");
      }
      else {
        $(viewList[i]).css("display", "none");
      }
    }
  }
  fromDataJson = {
    renderer: "driverManagementByY_div4",
    items: [
      { type: "settings", position: "label-left", width: 300, labelWidth: 90, labelAlign: "right" },
      { type: "hidden", name: "userId", label: "id:", width: 230 },
      { type: "input", name: "usercode", label: "手机号:", width: 230, editable: false, validate: "NotEmpty", required: true },
      {
        width: 230,
        type: "selectTable",
        placeholder: "请选择绑定车牌",
        readonly: false,
        table: userTable3,
        tableWidth: 500,
        tableHeight: 400,
        fieldText: "vehicleName",
        fieldValue: "vehicleName",
        name: "buyername5",
        label: "绑定车牌:",
        listener: function (consignorGrid) {
          setMethod(consignorGrid, userTable3, function (data) {
            var datad = {};
            datad.vehicleBindId = data.vehicleId;
            ddfrom.setFormData(datad);
          });
        }
      },
      { type: "hidden", name: "vehicleBindId", label: "绑定车牌:", width: 230 },
      { type: "newcolumn", offset: 10 },
      { type: "input", name: "userName", label: "司机姓名:", width: 230, editable: false, validate: "NotEmpty", required: true },
      { type: "input", name: "memo", label: "备注:", width: 230 }

    ]
  };
  //返回
  $(document).on("click", "#driverManagementByY_return", function () {
    setView("#driverManagementByY_div3");
  });
  //保存
  document.querySelector("#driverManagementByY_save").onclick = function () {
    //数据data
    var data = ddfrom.getFormData();
    data.branchid = userGJ.p_companyid;
    data.companyId = userinfo.p_companyid;
    data.companyName = userinfo.p_companyname;

    var feik = ddfrom.validate();
    if (!feik) {
      return;
    }
    if (!Prolog.isMobileNo(data.usercode)) {
      PlgDialog.msg("请输入正确的手机号");
      return;
    }
    //请求地址和类型
    var url = UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sysUserDuty/updateSysUserDuty";
    var type = "post";
    var contentType = "application/json";
    //message新增为0，修改为1
    if (data.userId == null || data.userId == "") {
      data.userId = data.usercode;
      url = UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sysUserDuty/addCarrierDriver";
    }
    var pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    //调用ajax方法
    utilAjaxGJ(url, type, JSON.stringify(data), function (data) {
      PlgDialog.close(pload);
      if (data.success) {
        setView("#driverManagementByY_div3");
        grid.reload();
      }
    }, contentType);
  };
};
