/**
 * guoj
 * 2020.05.25
 * 司机管理
    */
    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 () => {
  let fromDataJson;
  var grid;
  //供应商table
  var company_Table = {
    columns: [
      { id: "carrierId", name: "id", type: "ro", width: "130", hidden: true },
      { id: "carrierCode", name: "单位编号", type: "ro" },
      { id: "carrierName", name: "单位名称", type: "ro" }
    ],
    multiselect: false,

    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tmsBaseCarrier/queryTmsBaseCarrier", //数据接口
    type: "post", //数据提交方式，默认为get
    pagebar: { type: "simple" },
    contentType: "application/json",
    params: JSON.stringify({ pageNum: 1, pageSize: 500 }), //查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  var ddfrom = null;
  let dataSelete;
  //获取下拉框
  getSelectGJ([{ "objectcode": "USER_STATUS" },
  { "objectcode": "DUTY_TYPE" }], function (data) {
    dataSelete = data.data;
  });
  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: "dutyName", name: "职务名称", type: "ro" },
      {
        id: "status", name: "资料状态", type: "ro", render: (v) => {
          return getSelectValue(dataSelete.USER_STATUS, v);
        }
      },
      { id: "memo", name: "备注", type: "ro" },
      {
        id: "op", name: "操作", type: "button", align: "center", fixed: "right", width: 200, render: function () {
          return "删除^driverManagement_deleteClick,修改^driverManagement_updataClick,复制^driverManagement_copy,详情^driverManagement_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: "用户名称:" },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择单位",
            readonly: false,
            table: company_Table,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "carrierName",
            fieldValue: "carrierCode",
            name: "vendorcode1",
            label: "单位:",
            listener: function (consignorGrid) {
              setMethod(consignorGrid, company_Table, function (data) {
                var fromdata = grid.getToolBarForm().getFormData();
                data = fromdata.vendorcode1.length == 2 ? "" : data;
                var datad = {};
                datad.companyId = data.carrierId || "";
                grid.getToolBarForm().setFormData(datad);
              });
            }
          },
          { type: "hidden", name: "companyId", 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-md1 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/queryGetSysUserDuty",//数据接口
    type: "post",//数据提交方式，默认为get
    pagebar: {
      buttons: [
        { text: "删除", className: "layui-btn-danger", name: "delete" }
      ]
    },
    contentType: "application/json",
    params: "{}",//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  grid = new PlgGrid(opts);
  grid.renderTo("driverManagement_guojzhuans2");
  $("#driverManagement_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();
      delete fdata.vendorcode1;
      grid.setParams(JSON.stringify(fdata));
      grid.reload();
    }
    if (name == "addBtn") {
      setView("#driverManagement_div1");
      $("#driverManagement_div4").html("");
      ddfrom = new PlgForm(fromDataJson);
      ddfrom.enableItem("usercode");
      ddfrom.enableItem("userName");
      ddfrom.disableItem("buyername5");
      ddfrom.enableItem("vendorcode1");
      $("#driverManagement_title").html("新增");
    }
    if (name == "work_Reset") {
      grid.getToolBarForm().getDForm().setText("vendorcode1", "");
      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.driverManagement_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.driverManagement_updataClick = function (rid, data) {
    $("#driverManagement_div4").html("");
    ddfrom = new PlgForm(fromDataJson);
    ddfrom.disableItem("usercode");
    ddfrom.disableItem("userName");
    ddfrom.disableItem("buyername5");
    ddfrom.disableItem("vendorcode1");
    grid.selectRowById(rid);
    data.usercode = data.userId;
    ddfrom.setFormData(data);
    ddfrom.getDForm().setText("vendorcode1", data.companyName == null ? "" : data.companyName);
    setView("#driverManagement_div1");
    $("#driverManagement_title").html("编辑");
  };
  //复制
  window.driverManagement_copy = function (rid, data) {
    $("#driverManagement_div4").html("");
    ddfrom = new PlgForm(fromDataJson);
    ddfrom.enableItem("usercode");
    ddfrom.enableItem("userName");
    ddfrom.disableItem("buyername5");
    ddfrom.enableItem("vendorcode1");
    grid.selectRowById(rid);
    data.userId = "";
    ddfrom.getDForm().setText("vendorcode1", data.companyName == null ? "" : data.companyName);
    ddfrom.setFormData(data);
    setView("#driverManagement_div1");
    $("#driverManagement_title").html("新增");
  };
  //详情
  window.driverManagement_details = function (rid, rdata) {
    grid.selectRowById(rid);
    setView("#driverManagement_div9");
    new PlgPanel({
      renderer: "driverManagement_div422",
      skin: 2,
      header: {
        isShow: true,
        title: "基本信息"
      },
      defaultBody: {
        layoutCol: 3,
        valueBj: true,
        cols: [
          [{
            label: "用户编号",
            value: rdata.userId
          },
          {
            label: "用户名称",
            value: rdata.userName
          },
          {
            label: "所属单位",
            value: rdata.companyName
          },
          {
            label: "职务名称",
            value: rdata.dutyName
          }],
          [
            {
              label: "资料状态",
              value: getSelectValue(dataSelete.USER_STATUS, rdata.status)
            },
            {
              label: "备注",
              value: rdata.memo
            }
          ]
        ]
      }
    });
  };
  //跳转页面
  var carousel = layui.carousel;
  carousel.render({
    elem: "#driverManagement_div2",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", //设置容器宽度
    height: "100%"
  });
  let viewList = ["#driverManagement_div1", "#driverManagement_div3", "#driverManagement_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: "driverManagement_div4",
    items: [
      { type: "settings", position: "label-left", width: 300, labelWidth: 90 },
      { type: "hidden", name: "userId", label: "id:", width: 230 },
      { type: "input", name: "usercode", label: "用户编号:", width: 230, editable: false, validate: "NotEmpty", required: true },
      {
        type: "combo", name: "dutyType", label: "职务:", width: 230, options: dataSelete.DUTY_TYPE
      },
      {
        type: "input", name: "tel", label: "手机号:", width: 230, required: true
      },
      { type: "newcolumn", offset: 10 },
      {
        type: "input", name: "userName", label: "用户名称:", width: 230, editable: false, validate: "NotEmpty", required: true
      },
      {
        type: "input", name: "card", label: "身份证号:", width: 230
      },
      {
        type: "input", name: "memo", label: "备注:", width: 230
      },
      { type: "newcolumn", offset: 10 },
      {
        width: 230,
        type: "selectTable",
        placeholder: "请选择所属单位",
        readonly: false,
        table: company_Table,
        tableWidth: 500,
        tableHeight: 400,
        fieldText: "carrierName",
        fieldValue: "carrierCode",
        name: "vendorcode1",
        label: "所属单位:",
        listener: function (consignorGrid) {
          setMethod(consignorGrid, company_Table, function (data) {
            var fromdata = ddfrom.getFormData();
            data = fromdata.vendorcode1.length == 2 ? "" : data;
            var datad = {};
            datad.companyId = data.carrierId;
            datad.companyName = data.carrierName;
            ddfrom.setFormData(datad);
            ddfrom.getDForm().setText("buyername5", "");
            ddfrom.enableItem("buyername5");
          });
        }
      },
      {
        type: "hidden", name: "companyId", label: "所属单位:", width: 230
      },
      {
        type: "hidden", name: "companyName", label: "所属单位:", width: 230
      },
      {
        type: "combo", name: "status", label: "资料状态:", width: 230, options: dataSelete.USER_STATUS
      }]
  };
  //返回
  $(document).on("click", "#driverManagement_return", function () {
    setView("#driverManagement_div3");
  });
  //保存
  document.querySelector("#driverManagement_save").onclick = function () {
    //数据data
    var data = ddfrom.getFormData();
    data.branchid = userGJ.p_companyid;
    var feik = ddfrom.validate();
    if (!feik) {
      return;
    }
    if (data.tel && !/^1\d{10}$/.test(data.tel)) {
      layer.msg("请输入正确的手机号");
      return false;
    }
    if (data.card && !/(^\d{15}$)|(^\d{17}(x|X|\d)$)/.test(data.card)) {
      layer.msg("请输入正确的身份证号");
      return false;
    }
    data.dutyName = ddfrom.getCombo("dutyType").getComboText();
    //请求地址和类型
    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/addSysUserDuty";
    }
    //调用ajax方法
    utilAjaxGJ(url, type, JSON.stringify(data), function (data) {
      if (data.success) {
        setView("#driverManagement_div3");
        grid.reload();
      }
    }, contentType);
  };
};
