/**
 * zzx
 * 2020.09.24
 * 车辆维保查询
    */
    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 master_grid;
var userinfo = {
  p_id: '',
  p_username: '',
  p_workernumber: '',
  p_nickname: '',
  p_phone: null,
  p_email: null,
  p_companyid: '',
  p_enterpriseid: '',
  p_organizationid: '',
  p_companyname: '',
  p_orgname: '',
  p_enterprisename: '',
  p_usertype: '',
  p_lastlogindate: '',
  p_lastmodipassdate: null,
  p_validdate: '',
  p_import_batchno: null,
  p_data_source: null,
  forceModifyPasswdFlag: false,
  vehicle: null,
}

  // 获取默认时间
  var startTime = Prolog.formatDate(new Date(), "%YY-%MM") + "-01 00:00:00";
  var endTime = Prolog.formatDate(new Date()) + " 23:59:59";

  // 获取下拉框数据
  var dataSelect;
  getSelectGJ([
    { objectcode: "REPAIR_TYPE" },
    { objectcode: "AUDIT_STATUS" },
    { objectcode: "REPAIR_UNIT" }], function (data) {
      dataSelect = data.data;
      dataSelect.REPAIR_TYPE.unshift({ "value": "", "text": "" });
    }
  );

  // 配送中心table
  var branchTable = {
    // terrace显示全部，card分页显示
    skin: "card",
    // renderer:"grid",
    columns: [
      { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "companycode", name: "配送中心编号", type: "ro" },
      { id: "companyname", name: "配送中心名称", type: "ro" }
    ],
    multiselect: false,
    toolbar: [
      { type: "settings", position: "label-left", width: 200, labelWidth: 100 },
      {
        type: "block", id: "bk", blockOffset: 0, list: [
          { type: "input", name: "companycode", label: "配送中心编号:", width: 120 },
          { type: "newcolumn" },
          { type: "button", name: "querycarr", value: "查询" },
          { type: "newcolumn" },
          { type: "button", name: "car_Reset", value: "重置" }
        ]
      }
    ],

    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyBranch",
    pagebar: { type: "simple" },
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ companytype: "S" }), // 查询提交参数，分页参数默认为pageNum,pageSize
    contentType: "application/json",
    page: true, // 是否启用分页
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  // 车牌号table
  var vehicleTable = {
    skin: "card",
    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/queryVehicleInfo",
    type: "post",
    pagebar: { type: "simple" },
    contentType: "application/json",
    params: JSON.stringify({ companyId: userinfo.p_companyid }),
    page: true,
    totalCount: "totalCount"
  };
  // 修理厂table
  var gsTable = {
    // terrace显示全部，card分页显示
    skin: "card",
    // renderer:"grid",
    columns: [
      { id: "gsCode", name: "id", type: "ro", width: "130", hidden: true },
      { id: "gsCode", name: "维修厂编号", type: "ro" },
      { id: "gsName", name: "维修厂名称", type: "ro" }
    ],
    multiselect: false,
    toolbar: [
      { type: "settings", position: "label-left", width: 200, labelWidth: 100 },
      {
        type: "block", id: "bk", blockOffset: 0, list: [
          { type: "input", name: "gsCode", label: "维修厂编号:", width: 120 },
          { type: "newcolumn" },
          { type: "button", name: "querycarr", value: "查询" },
          { type: "newcolumn" },
          { type: "button", name: "car_Reset", value: "重置" }
        ]
      }
    ],
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/partnerServingStation/pop",
    pagebar: { type: "simple" },
    type: "post", // 数据提交方式，默认为get
    params: "{}", // 查询提交参数，分页参数默认为pageNum,pageSize
    contentType: "application/json",
    page: true, // 是否启用分页
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };

  // 主页grid
  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "billno", name: "id", type: "ro", width: "130", hidden: true },
      { id: "branchid", name: "机构id", type: "ro", width: 120 },
      { id: "branchname", name: "机构牌号", type: "ro", width: 120 },
      { id: "billno", name: "维修保养单号", type: "ro", width: 120 },
      { id: "vehicleCode", name: "车辆编号", type: "ro", width: 120 },
      {
        id: "repairType", name: "保养维修类型", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.REPAIR_TYPE, v);
        }
      },
      { id: "presentMileage", name: "当前里程", type: "ro", width: 120 },
      { id: "lastRepairTime", name: "上次保养日期", type: "ro", width: 120 },
      { id: "gsCode", name: "维修厂编号", type: "ro", width: 120 },
      { id: "gsName", name: "维修厂", type: "ro", width: 120 },
      { id: "estimateAmount", name: "估损金额", type: "ro", width: 120 },
      { id: "repairTime", name: "维修保养日期", type: "ro", width: 120 },
      { id: "amount", name: "实际金额", type: "ro", width: 120 },
      { id: "audittime", name: "审核时间", type: "ro", width: 120 },
      { id: "auditperson", name: "审核人", type: "ro", width: 120 },
      {
        id: "status", name: "状态", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.AUDIT_STATUS, v);
        }
      },
      { id: "memo", name: "备注", type: "ro", width: 120 }
    ],
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", className: "layui-col-md3", name: "sdate", label: "开始时间:", value: startTime },
          { type: "calendar", className: "layui-col-md3", name: "edate", label: "结束时间:", value: endTime },
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择配送中心",
            readonly: false,
            table: branchTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "branchidList",
            label: "配送中心:",
            listener: function (Grid) {
              setMethod(Grid, branchTable, function () { });
            }
          },
          { type: "input", className: "layui-col-md3", name: "billno", label: "维保单号:" },
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择车牌号",
            readonly: false,
            table: vehicleTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "vehicleNo",
            fieldValue: "vehicleNo",
            name: "vehicleCodeList",
            label: "车牌号:",
            listener: function (Grid) {
              setMethod(Grid, vehicleTable, function () { });
            }
          },
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择维修厂",
            readonly: false,
            table: gsTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "gsName",
            fieldValue: "gsCode",
            name: "gsCodeList",
            label: "维修厂:",
            listener: function (Grid) {
              setMethod(Grid, gsTable, function () { });
            }
          },
          //{ type: "input", className: "layui-col-md3", name: "gsCode", label: "维修厂编号:" },
          { type: "combo", className: "layui-col-md3", readonly: true, name: "repairType", label: "维保类型:", options: dataSelect.REPAIR_TYPE },
          {
            type: "block", className: "layui-col-md3 form_btn_left",
            list: [
              { type: "button", className: "btn-normal", name: "master_search", value: "查 询" },
              { type: "newcolumn" },
              { type: "button", name: "master_reset", value: "重 置" }
            ]
          }
        ]
      }
    ],
    fixedLast: false, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: false, // 是否支持多选

    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/vehicleManage/queryRepairRecord", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ sdate: startTime, edate: endTime, flag: "query" }), // 查询提交参数，分页参数默认为pageNum,pageSize
    page: true, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("vehicleMaintenanceInquiry_master");
  master_grid.loadData();
  master_grid.attachToolBarEvent("onButtonClick", function (name) {
    switch (name) {
      case "master_search":
        var request = master_grid.getToolBarForm().getFormData();
        request.sdate = request.sdate ? Prolog.formatDate(request.sdate) + " 00:00:00" : "";
        request.edate = request.edate ? Prolog.formatDate(request.edate) + " 23:59:59" : "";
        if (request.sdate > request.edate) {
          layer.msg("开始时间不能大于结束时间");
          return false;
        }
        request.branchid = request.branchidList ? JSON.parse(request.branchidList)[0] : "";
        request.vehicleCode = request.vehicleCodeList ? JSON.parse(request.vehicleCodeList)[0] : "";
        request.gsCode = request.gsCodeList ? JSON.parse(request.gsCodeList)[0] : "";
        request.flag = "query";
        dealNullQueryCondition(request);
        delete request.branchidList;
        delete request.vehicleCodeList;
        delete request.gsCodeList;
        master_grid.setParams(JSON.stringify(request));
        master_grid.reload();
        details_grid.clearAll();
        break;
      case "master_reset":
        master_grid.getToolBarForm().clear();
        master_grid.getToolBarForm().setText("branchidList", "");
        master_grid.getToolBarForm().setText("vehicleCodeList", "");
        master_grid.getToolBarForm().setText("gsCodeList", "");
        master_grid.getToolBarForm().setFormData({ sdate: startTime, edate: endTime });
        break;
    }
  });

  // 单击一行显示明细
  master_grid.on("onRowSelect", function (id) {
    let { billno } = master_grid.getRowData(id);
    details_grid.setParams(JSON.stringify({ billno }));
    details_grid.reload();
  });

  // 明细grid
  var details_grid_opts = {
    skin: "card",
    columns: [
      { id: "lineid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "branchid", name: "机构id", type: "ro", width: 120 },
      { id: "branchname", name: "机构牌号", type: "ro", width: 120 },
      { id: "billno", name: "维修保养单号", type: "ro", width: 120 },
      { id: "item", name: "维修保养项目名称", type: "ro", width: 120 },
      { id: "brand", name: "品牌", type: "ro", width: 120 },
      { id: "model", name: "型号", type: "ro", width: 120 },
      { id: "qty", name: "数量", type: "ro", width: 120 },
      {
        id: "unit", name: "单位", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.REPAIR_UNIT, v);
        }
      },
      { id: "price", name: "单价", type: "ro", width: 120 },
      { id: "amount", name: "合计金额", type: "ro", width: 120 },
      { id: "memo", name: "备注", type: "ro", width: 120 }
    ],
    fixedLast: false, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: false, // 是否支持多选

    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/vehicleManage/queryTmsRepairRecordD", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: "{}", // 查询提交参数，分页参数默认为pageNum,pageSize
    page: false, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  details_grid = new PlgGrid(details_grid_opts);
  details_grid.renderTo("vehicleMaintenanceInquiry_details");
};