/**
 * zzx
 * 2020.07.28
 * 评价查看
 */
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 details_form;
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 carousel = layui.carousel;
  carousel.render({
    elem: "#evaluation",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", // 设置容器宽度
    height: "100%"
  });
  // 切换页面
  function changePanel(name) {
    var slideList = ["#evaluation_master", "#evaluation_details"];
    for (var i = 0; i < slideList.length; i++) {
      if (slideList[i] == name) {
        $(name).css("display", "block");
      }
      else {
        $(slideList[i]).css("display", "none");
      }
    }
  }
  // 返回
  $(document).on("click", "#evaluation_details_return", function () {
    changePanel("#evaluation_master");
  });

  // 获取默认时间
  var startTime = Prolog.formatDate(new Date(), "%YY-%MM") + "-01 00:00:00";
  var endTime = Prolog.formatDate(new Date()) + " 23:59:59";
  // 获取下拉框
  var dataSelect = {
    TMS_SHIP_BILLTYPE: []
  };
  getSelectGJ([
    { "objectcode": "TMS_SHIP_BILLTYPE" }], function (data) {
      dataSelect = data.data;
    });

  // 配送中心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: 80 },
      {
        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 carrierTable = {
    // 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: 80 },
      {
        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/queryPubCompanyPopup",
    pagebar: { type: "simple" },
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ companytype: "3" }), // 查询提交参数，分页参数默认为pageNum,pageSize
    contentType: "application/json",
    page: true, // 是否启用分页
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  // 客户table
  var arriverTable = {
    // 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: 80 },
      {
        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/queryPubCompanyPopup",
    pagebar: { type: "simple" },
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ companytype: "4" }), // 查询提交参数，分页参数默认为pageNum,pageSize
    contentType: "application/json",
    page: true, // 是否启用分页
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };

  // 主页grid
  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "seqno", name: "id", type: "ro", width: "130", hidden: true },
      { id: "branchname", name: "配送中心", type: "ro", width: 120 },
      { id: "shipbillno", name: "运单号", type: "ro", width: 120 },
      {
        id: "billtype", name: "运单类型", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.TMS_SHIP_BILLTYPE, v);
        }
      },
      { id: "billdate", name: "单据日期", type: "ro", width: 120 },
      //{ id: "refbillno", name: "业务单号", type: "ro", width: 120 },
      { id: "carriername", name: "承运商", type: "ro", width: 120 },
      //{ id: "voucherno", name: "车辆", type: "ro", width: 120 },
      //{ id: "driver1stname", name: "司机", type: "ro", width: 120 },
      { id: "arriverName", name: "客户名称", type: "ro", width: 120 },
      { id: "arriveAddress", name: "客户地址", type: "ro", width: 180 },
      { id: "rsumcases", name: "件数", type: "ro", width: 120 },
      { id: "rsumgoodsweight", name: "重量", type: "ro", width: 120 },
      { id: "rsumgoodsvol", name: "体积", type: "ro", width: 120 },
      { id: "sumRating", name: "综合得分", type: "ro", width: 120 },
      { id: "memo", name: "备注", type: "ro", width: 120 },
      {
        id: "op", name: "操作", type: "button", align: "left", fixed: "right", width: "100", render: function () {
          return "查看详情^evaluation_detailsView";
        }
      }
    ],
    toolbar: [
      { type: "settings", labelWidth: 70, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", className: "layui-col-md3", name: "startTime", label: "开始时间:", value: startTime },
          { type: "calendar", className: "layui-col-md3", name: "endTime", 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: "shipbillno", label: "运单号:" },
          //{ type: "input", className: "layui-col-md3", name: "refbillno", label: "业务单号:" },
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择承运商",
            readonly: false,
            table: carrierTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "carriernoList",
            label: "承运商:",
            listener: function (Grid) {
              setMethod(Grid, carrierTable, function () { });
            }
          },
          //{ type: "input", className: "layui-col-md3", name: "driver1stname", label: "司机:" },
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择客户",
            readonly: false,
            table: arriverTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "arriverCodeList",
            label: "客户:",
            listener: function (Grid) {
              setMethod(Grid, arriverTable, function () { });
            }
          },
          {
            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: true, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: false, // 是否支持多选
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/evaluation/customerEvaluationQuery", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ startTime, endTime }),
    page: true, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("evaluation_master");
  master_grid.loadData();
  // master_grid.loadData({ data: { list: [{ seqno: 1, url: "/images/0.jpg" }] } });
  master_grid.attachToolBarEvent("onButtonClick", function (name) {
    switch (name) {
      case "master_search":
        var request = master_grid.getToolBarForm().getFormData();
        request.startTime = request.startTime ? Prolog.formatDate(request.startTime) + " 00:00:00" : "";
        request.endTime = request.endTime ? Prolog.formatDate(request.endTime) + " 23:59:59" : "";
        if (request.startTime > request.endTime) {
          layer.msg("开始时间不能大于结束时间");
          return false;
        }
        request.branchid = request.branchidList ? JSON.parse(request.branchidList)[0] : "";
        request.carrierno = request.carriernoList ? JSON.parse(request.carriernoList)[0] : "";
        request.arriverCode = request.arriverCodeList ? JSON.parse(request.arriverCodeList)[0] : "";
        request.driver1stname = request.driver1stname ? request.driver1stname : "";
        dealNullQueryCondition(request);
        delete request.branchidList;
        master_grid.setParams(JSON.stringify(request));
        master_grid.reload();
        break;
      case "master_reset":
        master_grid.getToolBarForm().clear();
        master_grid.getToolBarForm().setFormData({ startTime, endTime });
        master_grid.getToolBarForm().setText("branchidList", "");
        master_grid.getToolBarForm().setText("carriernoList", "");
        master_grid.getToolBarForm().setText("arriverCodeList", "");
        break;
    }
  });

  // 查看详情
  window.evaluation_detailsView = function (rid, rdata) {
    changePanel("#evaluation_details");
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    Prolog.ajax({
      url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/evaluation/customerEvaluationDtlQuery", type: "post", contentType: "application/json", data: JSON.stringify({ shipbillno: rid }),
      success: function (res) {
        PlgDialog.close(pload);
        if (res.success) {
          let itemRating = res.data.length ? res.data[0].itemRating : 0;
          let packageRating = res.data.length ? res.data[0].packageRating : 0;
          let speedRating = res.data.length ? res.data[0].speedRating : 0;
          let serviceRating = res.data.length ? res.data[0].serviceRating : 0;
          let evaluationDescription = res.data.length ? res.data[0].evaluationDescription : "";
          // 更新评分
          layui.use(["rate"], function () {
            var rate = layui.rate;
            rate.render({
              elem: "#evaluation_goodsReview",
              value: itemRating,
              readonly: true,
              half: true
            });
            rate.render({
              elem: "#evaluation_packageExpress",
              value: packageRating,
              readonly: true,
              half: true
            });
            rate.render({
              elem: "#evaluation_deliverySpeed",
              value: speedRating,
              readonly: true,
              half: true
            });
            rate.render({
              elem: "#evaluation_deliveryService",
              value: serviceRating,
              readonly: true,
              half: true
            });
          });
          $("#evaluation_goodsDetails").html(evaluationDescription);

          // 更新数据
          details_form.setFormData(rdata);

          // 更新图片
          var param = {
            bizId: rid,
            bizType: 0,
            enterpriseid: userinfo.p_enterpriseid,
            indexCol1: "03"
          };
          $("#evaluation_imageList").html("");
          querySysPicture(param).then(res => {
            res.forEach(item => {
              let html = "";
              html += "<div style='display: inline-block;margin-right: 20px;'>";
              html += "   <img src='" + item.fileUrl + "' class='evaluation_upload_image' style='height: 100px;'>";
              html += "</div>";
              let htmljs = $(html);
              // 点击展示
              htmljs.find(".evaluation_upload_image").on("click", function () {
                getImgDataRecon(res, item.fileUrl);
              });
              $("#evaluation_imageList").append(htmljs);
            });
          });
          return true;
        }
                return false;

      }, error: function () { PlgDialog.close(pload); return false; }
    });
  };

  // 图片放大
  function getImgDataRecon(res, src) {
    for (let i = 0; i < res.length; i++) {
      let rd = res[i];
      if (rd.fileUrl == src) {
        imageView(res, i);
        return;
      }
    }
  }

  // 详情页左侧表单
  var details_form_opts = {
    renderer: "evaluation_details_form",
    items: [
      { type: "settings", labelAlign: "left", labelWidth: 90 },
      {
        type: "row",
        list: [
          { type: "input", className: "layui-col-md6 no-border", name: "branchname", label: "配送中心:", readonly: true },
          { type: "input", className: "layui-col-md6 no-border", name: "shipbillno", label: "运单号:", readonly: true },
          { type: "input", className: "layui-col-md6 no-border", name: "billdate", label: "单据日期:", readonly: true },
          { type: "input", className: "layui-col-md6 no-border", name: "carriername", label: "承运商:", readonly: true },
          //{ type: "input", className: "layui-col-md6 no-border", name: "driver1stname", label: "司机:", readonly: true },
          //{ type: "input", className: "layui-col-md6 no-border", name: "voucherno", label: "车牌号:", readonly: true },
          { type: "input", className: "layui-col-md12 no-border", name: "arriverName", label: "客户:", readonly: true },
          { type: "input", className: "layui-col-md6 no-border", name: "rsumcases", label: "总件数:", readonly: true },
          { type: "input", className: "layui-col-md6 no-border", name: "rsumgoodsweight", label: "总重量:", readonly: true },
          { type: "input", className: "layui-col-md12 no-border", name: "rsumgoodsvol", label: "总体积:", readonly: true },
          { type: "input", className: "layui-col-md12 no-border", name: "arriveAddress", label: "客户地址:", readonly: true },
          { type: "input", className: "layui-col-md12 no-border", name: "memo", label: "备注:", readonly: true }
        ]
      }
    ]
  };
  details_form = new PlgForm(details_form_opts);
};