/**
 * 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: "ILLEGAL_TYPE" },
    { objectcode: "AUDIT_STATUS" },
    { objectcode: "YES_OR_NO" }], function (data) {
      dataSelect = data.data;
      dataSelect.ILLEGAL_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"
  };

  // 主页grid
  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "illegalNo", name: "id", type: "ro", width: "130", hidden: true },
      { id: "branchid", name: "机构id", type: "ro", width: 120 },
      { id: "branchname", name: "机构牌号", type: "ro", width: 120 },
      { id: "illegalNo", name: "违章单号", type: "ro", width: 120 },
      { id: "illegalTime", name: "违章发生时间", type: "ro", width: 120 },
      {
        id: "illegalType", name: "违章类别", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.ILLEGAL_TYPE, v);
        }
      },
      { id: "illegalAdd", name: "违章地点", type: "ro", width: 120 },
      { id: "illegalPerson", name: "违章人员", type: "ro", width: 120 },
      { id: "vehicleCode", name: "车辆编号", type: "ro", width: 120 },
      { id: "forfeit", name: "罚款", type: "ro", width: 120 },
      { id: "minusPoints", name: "扣分", type: "ro", width: 120 },
      { id: "discoverTime", name: "发现时间", type: "ro", width: 120 },
      { id: "discoverPerson", name: "发现人", type: "ro", width: 120 },
      { id: "disposeTime", name: "违章处理时间", type: "ro", width: 120 },
      {
        id: "punishPostpone", name: "是否延期处罚", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.YES_OR_NO, v);
        }
      },
      { id: "postponeDay", name: "延期处罚天数", type: "ro", width: 120 },
      { id: "undertaker", name: "承担对象", type: "ro", width: 120 },
      { id: "amountLossG", name: "货物损失金额", type: "ro", width: 120 },
      { id: "amountLossV", name: "车辆损失", type: "ro", width: 120 },
      { id: "compensate", name: "理赔收入", type: "ro", width: 120 },
      { id: "punishment", 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: "illegalNo", 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: "input", className: "layui-col-md3", name: "illegalPerson", label: "违章人员:" },
          { type: "combo", className: "layui-col-md3", readonly: true, name: "illegalType", label: "违章类别:", options: dataSelect.ILLEGAL_TYPE },
          {
            type: "block", className: "layui-col-md2 form_btn_left",
            list: [
              { type: "button", className: "btn-normal", name: "master_search", value: "查 询" },
              { type: "newcolumn" },
              { type: "button", name: "master_reset", value: "重 置" }
            ]
          },
          {
            type: "block", className: "layui-col-md1 right",
            list: [
              { type: "button", name: "master_audit", className: "btn-normal", value: "审 核" }
            ]
          }
        ]
      }
    ],
    fixedLast: false, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: false, // 是否支持多选

    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/vehicleManage/queryTVmIllegalRecordAudit", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ sdate: startTime, edate: endTime, flag: "auditQuery" }), // 查询提交参数，分页参数默认为pageNum,pageSize
    page: true, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("vehicleViolateAudit");
  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.flag = "auditQuery";
        dealNullQueryCondition(request);
        delete request.branchidList;
        delete request.vehicleCodeList;
        master_grid.setParams(JSON.stringify(request));
        master_grid.reload();
        break;
      case "master_reset":
        master_grid.getToolBarForm().clear();
        master_grid.getToolBarForm().setText("branchidList", "");
        master_grid.getToolBarForm().setText("vehicleCodeList", "");
        master_grid.getToolBarForm().setFormData({ sdate: startTime, edate: endTime });
        break;
      case "master_audit":
        var id = master_grid.getSelectedRowId();
        if (!id) {
          layer.msg("未选择需要审核的数据");
          return false;
        }
        tVmIllegalRecordAudit(id);
        break;
    }
  });

  // 审核、驳回事件
  function tVmIllegalRecordAudit(id) {
    var audit_form;
    PlgDialog.open({
      title: "审核",
      shadeClose: false,
      type: 1, // 设置为1，手动控制dialog的开关
      area: ["450px", "250px"],
      btn: ["确定", "取消"],
      content: "<div id =\"vehicleViolateAudit_auditForm\"></div>", //iframe的url
      yes: function () {
        let { radioStatus, auditdescrible } = audit_form.getFormData();
        if (radioStatus === "0" && !auditdescrible) {
          layer.msg("驳回时审核意见不能为空");
          return false;
        }
        let request = {
          illegalNo: id,
          auditdescrible
        };
        let url = radioStatus === "1" ? UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/vehicleManage/tVmIllegalRecordAudit" : UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/vehicleManage/tVmIllegalRecordReject";
        let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
        Prolog.ajax({
          url, type: "post", contentType: "application/json", data: JSON.stringify(request),
          success: function (res) {
            PlgDialog.close(pload);
            if (res.success) {
              layer.closeAll();
              PlgDialog.msg(res.message);
              master_grid.reload();
              return true;
            }
                        return false;
          },
          error: function () { PlgDialog.close(pload); return false; }
        });
      }
    });
    audit_form = new PlgForm({
      renderer: "vehicleViolateAudit_auditForm",
      items: [
        { type: "settings", position: "label-left", labelWidth: 60 },
        {
          type: "block", width: "*",
          list: [
            { type: "radio", name: "radioStatus", value: "1", label: "通过:", checked: true },
            { type: "newcolumn", offset: 50 },
            { type: "radio", name: "radioStatus", value: "0", label: "驳回:" }
          ]
        },
        {
          type: "block", width: "*",
          list: [
            { type: "input", name: "auditdescrible", label: "审核意见:", width: 280, rows: 3 }
          ]
        }
      ]
    });
  }
};