/**
* 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;
  });
  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, ddfrom_opts;
  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: 200, render: function () {
          return "删除^clientReview_deleteClick,修改^clientReview_updataClick,复制^clientReview_copy,详情^clientReview_details";
        }
      }
    ],
    multiselect: true,
    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: "companyid",
            name: "branchidList",
            label: "配送中心:",
            listener: function (grid) {
              setMethod(grid, userTable, function () {
              });
            }
          },
          { 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: "companyid",
            name: "returnVisitObjectIdList",
            label: "回访对象:",
            listener: function (grid) {
              setMethod(grid, huifTable, function () { });
            }
          },
          { 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: "重 置" }
            ]
          },
          {
            type: "block", className: "layui-col-md6 right",
            list: [
              { type: "button", className: "btn-addBtn btn-normal", name: "addBtn", value: "新 增" }
            ]
          }
        ]
      }],
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/evaluation/returnVisitRecordQuery",//数据接口
    type: "post",//数据提交方式，默认为get
    pagebar: {
      buttons: [
        { text: "删除", className: "layui-btn-danger", name: "delete" }
      ]
    },
    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("clientReview_guojzhuans2");
  $("#clientReview_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;
      }
      jsonData.branchid = jsonData.branchidList ? JSON.parse(jsonData.branchidList)[0] : "";
      jsonData.returnVisitObjectId = jsonData.returnVisitObjectIdList ? JSON.parse(jsonData.returnVisitObjectIdList)[0] : "";
      delete jsonData.branchidList;
      delete jsonData.returnVisitObjectIdList;
      grid.setParams(JSON.stringify(jsonData));
      grid.reload();
    }
    //新增
    if (name == "addBtn") {
      setViewName("#clientReview_div1");
      $("#clientReview_div4").html("");
      ddfrom = new PlgForm(ddfrom_opts);
      $("#clientReview_title").html("新增");
    }
    //重置
    if (name == "work_Reset") {
      grid.getToolBarForm().getDForm().setText("branchidList", "");
      grid.getToolBarForm().getDForm().setText("returnVisitObjectIdList", "");
      grid.getToolBarForm().clear();
      grid.getToolBarForm().setFormData({ startTime: startTime, endTime: endTime });
    }
  });
  //按钮添加方法
  grid.attachPageBarEvent(function (name) {
    //删除
    if (name == "delete") {
      let data = [];
      let datas = getDeleteAll(grid);
      datas.forEach(v => {
        data.push(grid.getRowData(v));
      });
      data = JSON.stringify(data);
      utilDelete(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/evaluation/returnVisitRecordRemove", "post", data, function (data) {
        grid.reload();
      }, "application/json");
    }
  });
  //删除
  window.clientReview_deleteClick = function (rid, rdata) {
    grid.selectRowById(rid);
    let data = JSON.stringify([rdata]);
    utilDelete(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/evaluation/returnVisitRecordRemove", "post", data, function (data) {
      setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
      grid.reload();
    }, "application/json");
  };
  //修改
  window.clientReview_updataClick = function (rid, data) {
    grid.selectRowById(rid);
    data.buyername2 = JSON.stringify([data.returnVisitObjectCode]);
    data.buyername1 = JSON.stringify([data.branchid]);
    $("#clientReview_div4").html("");
    ddfrom = new PlgForm(ddfrom_opts);
    ddfrom.getDForm().setText("buyername2", data.returnVisitObjectName == null ? "" : data.returnVisitObjectName);
    ddfrom.getDForm().setText("buyername1", data.branchname == null ? "" : data.branchname);
    ddfrom.clear();
    ddfrom.setFormData(data);
    setViewName("#clientReview_div1");
    $("#clientReview_title").html("编辑");
  };
  //复制
  window.clientReview_copy = function (rid, data) {
    grid.selectRowById(rid);
    data.seqno = "";
    data.buyername2 = JSON.stringify([data.returnVisitObjectCode]);
    data.buyername1 = JSON.stringify([data.branchid]);
    $("#clientReview_div4").html("");
    ddfrom = new PlgForm(ddfrom_opts);
    ddfrom.getDForm().setText("buyername2", data.returnVisitObjectName == null ? "" : data.returnVisitObjectName);
    ddfrom.getDForm().setText("buyername1", data.branchname == null ? "" : data.branchname);
    ddfrom.setFormData(data);
    setViewName("#clientReview_div1");
    $("#clientReview_title").html("新增");
  };
  //详情
  window.clientReview_details = function (rid, rdata) {
    grid.selectRowById(rid);
    setViewName("#clientReview_div9");
    new PlgPanel({
      renderer: "clientReview_div422",
      skin: 2,
      header: {
        isShow: true,
        title: "基本信息"
      },
      defaultBody: {
        layoutCol: 3,
        valueBj: true,
        cols: [
          [
            {
              label: "配送中心",
              value: rdata.branchname
            },
            {
              label: "回访编号",
              value: rdata.returnVisitNo
            },
            {
              label: "回访时间",
              value: rdata.returnVisitDate
            },
            {
              label: "回访类型",
              value: getSelectValue(dataSelete.TMS_RETURN_VISIT_TYPE, rdata.returnVisitType)
            }
          ],
          [
            {
              label: "回访对象",
              value: rdata.returnVisitObjectName
            },
            {
              label: "客户参与人",
              value: rdata.returnVisitObjectPerson
            },
            {
              label: "己方参与人",
              value: rdata.returnVisitPerson
            },
            {
              label: "回访内容",
              value: rdata.returnVisitContent
            }
          ],
          [
            {
              label: "回访结果",
              value: rdata.returnVisitResult
            },
            {
              label: "回访登记人",
              value: rdata.createperson
            },
            {
              label: "登记时间",
              value: rdata.createtime
            },
            {
              label: "备注",
              value: rdata.memo
            }
          ]
        ]
      }
    });
  };
  //跳转页面
  var carousel = layui.carousel;
  carousel.render({
    elem: "#clientReview_div2",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", //设置容器宽度
    height: "100%"
  });
  let dataView = ["#clientReview_div1", "#clientReview_div3", "#clientReview_div9"];
  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");
      }
    }
  }
  ddfrom_opts = {
    renderer: "clientReview_div4",
    items: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "hidden", name: "seqno", label: "id:" },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择配送中心",
            readonly: false,
            table: userTable,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "companyname",
            fieldValue: "companycode",
            name: "buyername1",
            label: "配送中心:<span class='dhxform_item_required'>*</span>",
            listener: function (consignorGrid) {
              setMethod(consignorGrid, userTable, function (data) {
                var datad = {};
                datad.branchid = data.companyid;
                datad.branchname = data.companyname;
                ddfrom.setFormData(datad);
              });
            }
          },
          { type: "hidden", name: "branchid", label: "配送中心:" },
          { type: "hidden", name: "branchname", label: "配送中心:" },
          { type: "calendar", name: "returnVisitDate", label: "回访时间:", className: "layui-col-md3", required: true },
          { type: "combo", name: "returnVisitType", label: "回访类型:", className: "layui-col-md3", options: dataSelete.TMS_RETURN_VISIT_TYPE, readonly: true, required: true },
          { type: "input", name: "returnVisitObjectPerson", label: "客户参与人:", className: "layui-col-md3", editable: false, validate: "NotEmpty", required: true }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", name: "returnVisitPerson", label: "己方参与人:", className: "layui-col-md3", required: true },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择回访对象",
            readonly: false,
            table: huifTable,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "companyname",
            fieldValue: "companycode",
            name: "buyername2",
            label: "回访对象:<span class='dhxform_item_required'>*</span>",
            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: "row",
        list: [
          { type: "input", name: "returnVisitContent", label: "回访内容:", className: "layui-col-md9", required: true }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", name: "returnVisitResult", label: "回访结果:", className: "layui-col-md9", required: true }
        ]
      }
    ]
  };
  //返回
  $(document).on("click", "#clientReview_return", function () {
    setViewName("#clientReview_div3");
  });
  //保存
  document.querySelector("#clientReview_save").onclick = function () {
    //数据data
    var data = ddfrom.getFormData();
    var feik = ddfrom.validate();
    if (!feik) {
      return;
    }
    data.returnVisitDate = FormatDateTime(data.returnVisitDate);
    //请求地址和类型
    var url = UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/evaluation/returnVisitRecordUpdate";
    var type = "post";
    var contentType = "application/json";
    //message新增为0，修改为1
    if (data.seqno == null || data.seqno == "") {
      // data.seqno = Math.random();
      url = UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/evaluation/returnVisitRecordAdd";
    }
    //调用ajax方法
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    Prolog.ajax({
      url, type, contentType, data: JSON.stringify(data),
      success: function (data) {
        PlgDialog.close(pload);
        if (data.success) {
          PlgDialog.msg(data.message);
          setViewName("#clientReview_div3");
          grid.reload();
          return true;
        }
        setTimeout(() => { PlgDialog.alert(data.message || "接口调用失败"); }, 300);
        return false;

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