/**
* guoj
* 2020.07.30
* 回访审核
*/
export default () => {
  // 获取默认时间
  var startTime = Prolog.formatDate(new Date(), "%YY-%MM") + "-01 00:00:00";
  var endTime = Prolog.formatDate(new Date()) + " 23:59:59";
  var dataSelete = {};
  //获取下拉框
  getSelectGJ([{ "objectcode": "TMS_RETURN_VISIT_TYPE" }], function (data) {
    data.data.TMS_RETURN_VISIT_TYPE.unshift({ "text": "", "value": "" });
    dataSelete = data.data;
  });
  let dataItem;
  var grid;
  //配送中心table
  var userTable = {
    columns: [
      { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "companycode", name: "配送中心编号", type: "ro" },
      { id: "companyname", name: "配送中心名称", type: "ro" }
    ],
    multiselect: false,
    contentType: "application/json",
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyBranch", //数据接口
    type: "post", //数据提交方式，默认为get
    pagebar: { type: "simple" },
    params: JSON.stringify({ "companytype": "S" }), //查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //回访对象table
  var huifTable = {
    columns: [
      { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "companycode", name: "对象编号", type: "ro" },
      { id: "companyname", name: "对象名称", type: "ro" }
    ],
    multiselect: false,
    contentType: "application/json",
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyCustomer", //数据接口
    type: "post", //数据提交方式，默认为get
    pagebar: { type: "simple" },
    params: JSON.stringify({ "companytype": "C" }), //查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  var ddfrom = null;
  var opts = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "seqno", name: "id", type: "ro", width: "100", hidden: true },
      { id: "branchname", name: "配送中心", type: "ro", width: 150 },
      { id: "returnVisitNo", name: "回访编号", type: "ro", width: 150 },
      { id: "returnVisitDate", name: "回访时间", type: "ro", width: 130 },
      {
        id: "returnVisitType", name: "回访类型", type: "ro", width: 100, render: (v) => {
          return getSelectValue(dataSelete.TMS_RETURN_VISIT_TYPE, v);
        }
      },
      { id: "returnVisitObjectName", name: "回访对象", type: "ro", width: 100 },
      { id: "returnVisitObjectPerson", name: "客户参与人", type: "ro", width: 100 },
      { id: "returnVisitPerson", name: "己方参与人", type: "ro", width: 100 },
      { id: "returnVisitContent", name: "回访内容", type: "ro", width: 100 },
      { id: "returnVisitResult", name: "回访结果", type: "ro", width: 120 },
      { id: "createperson", name: "回访登记人", type: "ro", width: 100 },
      { id: "createtime", name: "登记时间", type: "ro", width: 130 },
      { id: "memo", name: "备注", type: "ro", width: 100 },
      {
        id: "op", name: "操作", type: "button", align: "center", fixed: "right", width: 50, render: function () {
          return "审核^returnVisit_details";
        }
      }
    ],
    multiselect: false,
    fixedLast: true, //开启固定列，固定最后一行，倒数第二列不能隐藏
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", name: "startTime", className: "layui-col-md3", label: "开始时间:", value: startTime },
          { type: "calendar", name: "endTime", className: "layui-col-md3", label: "结束时间:", value: endTime },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择配送中心",
            readonly: false,
            table: userTable,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "companyname",
            fieldValue: "companycode",
            name: "buyername1",
            label: "配送中心:",
            listener: function (consignorGrid) {
              setMethod(consignorGrid, userTable, function (data) {
                var datad = {};
                datad.branchid = data.companyid;
                grid.getToolBarForm().setFormData(datad);
              });
            }
          },
          { type: "hidden", name: "branchid", label: "配送中心:" },
          { type: "combo", name: "returnVisitType", label: "回访类型:", className: "layui-col-md3", options: dataSelete.TMS_RETURN_VISIT_TYPE, readonly: true },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择回访对象",
            readonly: false,
            table: huifTable,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "companyname",
            fieldValue: "companycode",
            name: "buyername2",
            label: "回访对象:",
            listener: function (consignorGrid) {
              setMethod(consignorGrid, huifTable, function (data) {
                var datad = {};
                datad.returnVisitObjectId = data.companyid;
                grid.getToolBarForm().setFormData(datad);
              });
            }
          },
          { type: "hidden", name: "returnVisitObjectId", label: "回访对象:", className: "layui-col-md3" },
          {
            type: "block", className: "layui-col-md3 form_btn_left",
            list: [
              { type: "button", className: "btn-normal", name: "querywork", value: "查 询" },
              { type: "newcolumn" },
              { type: "button", name: "work_Reset", className: "normal", value: "重 置" }
            ]
          }
        ]
      }],
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/evaluation/returnVisitRecordQuery",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ startTime: startTime, endTime: endTime }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  grid = new PlgGrid(opts);
  grid.renderTo("returnVisit_guojzhuans2");
  $("#returnVisit_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 jsonData = grid.getToolBarForm().getFormData();
      jsonData.startTime = jsonData.startTime ? Prolog.formatDate(jsonData.startTime) + " 00:00:00" : "";
      jsonData.endTime = jsonData.endTime ? Prolog.formatDate(jsonData.endTime) + " 23:59:59" : "";
      if (jsonData.startTime > jsonData.endTime) {
        layer.msg("开始时间不能大于结束时间");
        return false;
      }
      grid.setParams(JSON.stringify(jsonData));
      grid.reload();
    }
    //重置
    if (name == "work_Reset") {
      grid.getToolBarForm().getDForm().setText("buyername1", "");
      grid.getToolBarForm().getDForm().setText("buyername2", "");
      grid.getToolBarForm().clear();
      grid.getToolBarForm().setFormData({ startTime: startTime, endTime: endTime });
    }
  });
  //审核
  window.returnVisit_details = function (rid, data) {
    grid.selectRowById(rid);
    setViewName("#returnVisit_div1");
    data.auditOpinion = data.auditOpinion == null ? "1" : data.auditOpinion;
    $("#returnVisit_div4").html("");
    ddfrom = new PlgForm(dataItem);
    ddfrom.setFormData(data);
    ddfrom.getDForm().setText("buyername2", data.returnVisitObjectName == null ? "" : data.returnVisitObjectName);
    ddfrom.getDForm().setText("buyername1", data.branchname == null ? "" : data.branchname);
    ddfrom.disableItem("buyername1");
    ddfrom.disableItem("buyername2");
  };
  //跳转页面
  var carousel = layui.carousel;
  carousel.render({
    elem: "#returnVisit_div2",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", //设置容器宽度
    height: "100%"
  });
  let dataView = ["#returnVisit_div1", "#returnVisit_div3"];
  function setViewName(name) {
    for (var i = 0; i < dataView.length; i++) {
      if (name == dataView[i]) {
        $(name).css("display", "block");
      }
      else {
        $(dataView[i]).css("display", "none");
      }
    }
  }
  dataItem = {
    renderer: "returnVisit_div4",
    items: [
      { type: "settings", position: "label-left", width: 300, labelWidth: 90 },
      { type: "hidden", name: "seqno", label: "id:", width: 260 },
      {
        width: 260,
        type: "selectTable",
        placeholder: "请选择配送中心",
        readonly: false,
        table: userTable,
        tableWidth: 500,
        tableHeight: 400,
        fieldText: "companyname",
        fieldValue: "companycode",
        name: "buyername1",
        label: "配送中心:",
        listener: function (consignorGrid) {
          setMethod(consignorGrid, userTable, function (data) {
            var datad = {};
            datad.branchid = data.companyid;
            ddfrom.setFormData(datad);
          });
        }
      },
      { type: "hidden", name: "branchid", label: "配送中心:" },
      { type: "newcolumn" },
      { type: "calendar", name: "returnVisitDate", label: "回访时间:", width: 260, disabled: true },
      { type: "newcolumn" },
      { type: "combo", name: "returnVisitType", label: "回访类型:", width: 260, options: dataSelete.TMS_RETURN_VISIT_TYPE, readonly: true, disabled: true },
      { type: "newcolumn" },
      { type: "input", name: "returnVisitObjectPerson", label: "客户参与人:", width: 260, editable: false, validate: "NotEmpty", required: true, disabled: true },
      { type: "newcolumn" },
      { type: "input", name: "returnVisitPerson", label: "己方参与人:", width: 260, disabled: true },
      { type: "newcolumn" },
      {
        width: 260,
        type: "selectTable",
        placeholder: "请选择回访对象",
        readonly: false,
        table: huifTable,
        tableWidth: 500,
        tableHeight: 400,
        fieldText: "companyname",
        fieldValue: "companycode",
        name: "buyername2",
        label: "回访对象:",
        listener: function (consignorGrid) {
          setMethod(consignorGrid, huifTable, function (data) {
            var datad = {};
            datad.returnVisitObjectId = data.companyid;
            datad.returnVisitObjectName = data.companyname;
            datad.returnVisitObjectCode = data.companycode;
            ddfrom.setFormData(datad);
          });
        }
      },
      { type: "hidden", name: "returnVisitObjectCode", label: "回访对象:" },
      { type: "hidden", name: "returnVisitObjectId", label: "回访对象:" },
      { type: "hidden", name: "returnVisitObjectName", label: "回访对象:" },
      { type: "newcolumn" },
      { type: "input", name: "returnVisitContent", label: "回访内容:", width: 978, disabled: true },
      { type: "newcolumn" },
      { type: "input", name: "returnVisitResult", label: "回访结果:", width: 978, disabled: true },
      { type: "newcolumn" },
      { type: "input", name: "auditDescription", label: "审核描述:", width: 978, editable: false, validate: "NotEmpty", required: true },
      { type: "newcolumn" },
      { type: "combo", name: "auditOpinion", label: "审核意见:", width: 260, options: [{ text: "通过", value: "1" }, { text: "驳回", value: "0" }], readonly: true }
    ]
  };
  //返回
  $(document).on("click", "#returnVisit_return", function () {
    setViewName("#returnVisit_div3");
  });
  //确认
  document.querySelector("#returnVisit_save").onclick = function () {
    //数据data
    var data = ddfrom.getFormData();
    var feik = ddfrom.validate();
    if (!feik) {
      return;
    }
    data.returnVisitDate = FormatDateTime(data.returnVisitDate);
    //调用ajax方法
    utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/evaluation/returnVisitAuditConfirm", "post", JSON.stringify(data), function (data) {
      if (data.success) {
        setViewName("#returnVisit_div3");
        grid.reload();
      }
    }, "application/json");
  };
};
