/*
* zzx
* 2020.05.26
* 订单审核
*    */
    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 carousel = layui.carousel;
  carousel.render({
    elem: "#orderReview",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", // 设置容器宽度
    height: "100%"
  });
  // 切换页面
  function changePanel(name) {
    var slideList = ["#orderReview_main", "#orderReview_view"];
    for (var i = 0; i < slideList.length; i++) {
      if (slideList[i] == name) {
        $(name).css("display", "block");
      }
      else {
        $(slideList[i]).css("display", "none");
      }
    }
  }
  // 返回
  $("#orderReview_view_return").on("click", function () {
    changePanel("#orderReview_main");
  });

  // 获取默认时间
  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": "TMS_SHIP_BILLTYPE" },
    { "objectcode": "URG_FLG" },
    { "objectcode": "ITEM_TYPE" },
    { "objectcode": "TEMP_REQ" },
    { "objectcode": "GOODS_TYPE" },
    { "objectcode": "SEND_CUS_TYPE" }], function (data) {
      dataSelect = data.data;
      dataSelect.TMS_SHIP_BILLTYPE.unshift({ "value": "", "text": "" });
      dataSelect.URG_FLG.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: 110 },
      {
        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 consignorTable = {
    // 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/queryCompanyConsignor",
    pagebar: { type: "simple" },
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ companytype: "H" }), // 查询提交参数，分页参数默认为pageNum,pageSize
    contentType: "application/json",
    page: true, // 是否启用分页
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };

  var master_grid, details_grid, orderReview_view_grid;

  // 主页grid
  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "billid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "branchname", name: "配送中心/站点名称", type: "ro", width: 120 },
      { id: "consignorname", name: "货主", type: "ro", width: 120 },
      { id: "billno", name: "运单编号", type: "ro", width: 120 },
      { id: "billdate", name: "单据日期", type: "ro", width: 120 },
      {
        id: "billtype", name: "单据类型", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.TMS_SHIP_BILLTYPE, v);
        }
      },
      {
        id: "urgentIdentify", name: "急单标识", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.URG_FLG, v);
        }
      },
      { id: "shipperName", name: "发货客户", type: "ro", width: 120 },
      { id: "shipoperator", name: "发货联系人", type: "ro", width: 120 },
      { id: "shipmentAddress", name: "发货地址", type: "ro", width: 120 },
      { id: "arriverName", name: "收货客户", type: "ro", width: 120 },
      { id: "consignee", name: "收货联系人", type: "ro", width: 120 },
      { id: "arriveAddress", name: "收货地址", type: "ro", width: 120 },
      { id: "sumcases", name: "总件数", type: "ro", width: 120 },
      { id: "sumgoodsweight", name: "总重量", type: "ro", width: 120 },
      { id: "sumgoodsvol", name: "总体积", type: "ro", width: 120 },
      { id: "memo", name: "备注", type: "ro", width: 120 },
      {
        id: "op", name: "操作", type: "button", align: "left", width: 80, fixed: "right", render: function () {
          return "详情^orderReview_view";
        }
      }
    ],
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择配送中心/站点",
            readonly: false,
            table: branchTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "branchList",
            label: "配送中心/站点:",
            listener: function (Grid) {
              setMethod(Grid, branchTable, function () { });
            }
          },
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择货主",
            readonly: false,
            table: consignorTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "consignorList",
            label: "货主:",
            listener: function (Grid) {
              setMethod(Grid, consignorTable, function () { });
            }
          },
          { type: "input", className: "layui-col-md3", name: "shipperName", label: "发货方:" },
          { type: "input", className: "layui-col-md3", name: "arriverName", label: "收货方:" },
          { type: "input", className: "layui-col-md3", name: "billno", label: "运单编号:" },
          { type: "combo", className: "layui-col-md3", readonly: true, name: "billtype", label: "单据类型:", options: dataSelect.TMS_SHIP_BILLTYPE },
          { type: "combo", className: "layui-col-md3", readonly: true, name: "urgentIdentify", label: "急单标识:", options: dataSelect.URG_FLG },
          { type: "calendar", className: "layui-col-md3", name: "sdate", label: "开始时间:", value: startTime },
          { type: "calendar", className: "layui-col-md3", name: "edate", label: "结束时间:", value: endTime },
          {
            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: "重 置" }
            ]
          },
          {
            type: "block", className: "layui-col-md6 right",
            list: [
              { type: "button", className: "btn-normal", name: "master_review", value: "审 核" },
              { type: "newcolumn" },
              { type: "button", name: "master_void", value: "作 废" }
            ]
          }
        ]
      }
    ],
    fixedLast: true, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: false, // 是否支持多选

    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/tmsOrderManage/queryTmsOrderBillHAudit", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ flag: "auditQuery", sdate: startTime, edate: endTime }), // 查询提交参数，分页参数默认为pageNum,pageSize
    page: true, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("orderReview_master");
  master_grid.loadData();
  master_grid.attachToolBarEvent("onButtonClick", function (name) {
    var id = master_grid.getSelectedRowId();
    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.branchList ? JSON.parse(request.branchList)[0] : "";
        request.consignor = request.consignorList ? JSON.parse(request.consignorList)[0] : "";
        request.flag = "auditQuery";
        master_grid.setParams(JSON.stringify(request));
        master_grid.reload();
        details_grid.clearAll();
        break;
      case "master_reset":
        master_grid.getToolBarForm().clear();
        master_grid.getToolBarForm().setText("branchList", "");
        master_grid.getToolBarForm().setText("consignorList", "");
        master_grid.getToolBarForm().setFormData({ sdate: startTime, edate: endTime });
        break;
      case "master_review":
        if (!id) {
          layer.msg("未选择需要审核的数据");
          return;
        }
        var { branchid } = master_grid.getRowData(id);
        orderAudit(id, branchid);
        break;
      case "master_void":
        if (!id) {
          layer.msg("未选择需要作废的数据");
          return;
        }
        masterVoid(id);
        break;
    }
  });
  // 审核、驳回事件
  function orderAudit(id, branchid) {
    var audit_form;
    PlgDialog.open({
      title: "审核",
      shadeClose: false,
      type: 1, // 设置为1，手动控制dialog的开关
      area: ["450px", "250px"],
      btn: ["确定", "取消"],
      content: "<div id =\"orderReview_auditForm\"></div>", //iframe的url
      yes: function () {
        let { radioStatus, auditdescrible } = audit_form.getFormData();
        if (radioStatus === "0" && !auditdescrible) {
          layer.msg("驳回时审核意见不能为空");
          return false;
        }
        let request = {
          volist: [{ billid: id, branchid }],
          auditdescrible
        };
        let url = radioStatus === "1" ? UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/tmsOrderManage/orderAudit" : UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/tmsOrderManage/orderReject";
        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();
              details_grid.clearAll();
              return true;
            }
                        return false;
          },
          error: function () { PlgDialog.close(pload); return false; }
        });
      }
    });
    audit_form = new PlgForm({
      renderer: "orderReview_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 }
          ]
        }
      ]
    });
  }
  // 作废事件
  function masterVoid(id) {
    var void_form;
    PlgDialog.open({
      title: "作废",
      shadeClose: true,
      area: ["400px", "250px"],
      btn: ["确定", "取消"],
      content: "<div id =\"void_form\"></div>", // iframe的url
      yes: function () {
        var feik = void_form.validate();
        if (!feik) {
          return;
        }
        let { auditdescrible } = void_form.getFormData();
        if (!auditdescrible) {
          layer.msg("作废原因不能为空");
          return false;
        }
        let request = {
          volist: [{ billid: id }],
          auditdescrible
        };
        let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
        Prolog.ajax({
          url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/tmsOrderManage/orderCancel", 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();
              details_grid.clearAll();
              return true;
            }
                        return false;
          },
          error: function () { PlgDialog.close(pload); return false; }
        });
      }
    });
    void_form = new PlgForm({
      renderer: "void_form",
      items: [
        { type: "input", name: "auditdescrible", label: "作废原因:", width: 280, rows: 3, required: true }
      ]
    });
  }
  // 单击一行显示明细
  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: "itemtype", name: "货品类别", type: "ro", render: function (v) {
          return getSelectValue(dataSelect.ITEM_TYPE, v);
        }
      },
      { id: "itemno", name: "货品编号", type: "ro" },
      { id: "itemname", name: "货品名称", type: "ro" },
      { id: "pcs", name: "货品件数", type: "ro" },
      { id: "weight", name: "货品重量", type: "ro" },
      { id: "vol", name: "货品体积", type: "ro" }
    ],
    fixedLast: false, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: false, // 是否支持多选

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

  // 详情页
  window.orderReview_view = function (rid, rdata) {
    new PlgPanel({
      renderer: "orderReview_view_form",
      skin: 2,
      header: {
        isShow: true,
        title: "运单详情"
      },
      defaultBody: {
        layoutCol: 4,
        valueBj: true,
        cols: [
          [
            { value: rdata.branchname, label: "配送中心" },
            { value: rdata.consignorname, label: "货主" },
            { value: getSelectValue(dataSelect.TMS_SHIP_BILLTYPE, rdata.billtype), label: "单据类型" },
            { value: getSelectValue(dataSelect.TEMP_REQ, rdata.temperatureRequire), label: "温度要求" },
            { value: getSelectValue(dataSelect.GOODS_TYPE, rdata.shipgoodsgroup), label: "货物类型" },
            { value: getSelectValue(dataSelect.URG_FLG, rdata.urgentIdentify), label: "紧急标识" },
            { value: rdata.reqshipmentdate, label: "预计发货时间" },
            { value: rdata.reqarrivvaldate, label: "要求到货时间" },
            { value: rdata.billdate, label: "单据日期" },
            { value: rdata.memo, label: "备注", layoutCol: 12 }
          ]
        ]
      }
    });
    new PlgPanel({
      renderer: "orderReview_view_form",
      skin: 2,
      empyt: false,
      header: {
        isShow: true,
        title: "发货人信息"
      },
      defaultBody: {
        layoutCol: 4,
        valueBj: true,
        cols: [
          [
            { value: getSelectValue(dataSelect.SEND_CUS_TYPE, rdata.shipperType), label: "客户类型" },
            { value: rdata.shipperName, label: "发运客户", layoutCol: 8 },
            { value: rdata.shipoperator, label: "发运联系人" },
            { value: rdata.shipoperatortel, label: "发运人电话" },
            { value: rdata.shipoperatormobile, label: "发运人手机" },
            { value: rdata.shipmentProvinceName, label: "省" },
            { value: rdata.shipmentCityName, label: "市" },
            { value: rdata.shipmentDistrictName, label: "区/县" },
            { value: rdata.shipmentTownName, label: "街道/乡镇" },
            { value: rdata.shipmentCommunityName, label: "村/社区", layoutCol: 8 },
            { value: rdata.shipmentAddress, label: "详细地址", layoutCol: 12 }
          ]
        ]
      }
    });
    new PlgPanel({
      renderer: "orderReview_view_form",
      skin: 2,
      empyt: false,
      header: {
        isShow: true,
        title: "收货人信息"
      },
      defaultBody: {
        layoutCol: 4,
        valueBj: true,
        cols: [
          [
            { value: getSelectValue(dataSelect.SEND_CUS_TYPE, rdata.arriverType), label: "客户类型" },
            { value: rdata.arriverName, label: "收货客户", layoutCol: 8 },
            { value: rdata.consignee, label: "收货联系人" },
            { value: rdata.consigneetel, label: "收货人电话" },
            { value: rdata.consigneemobile, label: "收货人手机" },
            { value: rdata.arriveProvinceName, label: "省" },
            { value: rdata.arriveCityName, label: "市" },
            { value: rdata.arriveDistrictName, label: "区/县" },
            { value: rdata.arriveTownName, label: "街道/乡镇" },
            { value: rdata.arriveCommunityName, label: "村/社区", layoutCol: 8 },
            { value: rdata.arriveAddress, label: "详细地址", layoutCol: 12 }
          ]
        ]
      }
    });
    changePanel("#orderReview_view");
    orderReview_view_grid.setParams(JSON.stringify({ billno: rdata.billno }));
    orderReview_view_grid.reload();
  };
  // 详情页grid
  orderReview_view_grid = new PlgGrid(details_grid_opts);
  orderReview_view_grid.renderTo("orderReview_view_grid");
};