/**
 * guoj
 * 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 () => {
  let fromDataJson;
  //省
  var province;
  //查询省
  utilSyncAjaxGJ(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/zone/get-by-pid", "post", JSON.stringify({ "fatherid": "01" }), function (data) {
    province = data.data;
    for (let x in province) {
      province[x].text = province[x].keyvalue;
      province[x].value = province[x].zoneid;
    }
    province.unshift({ "text": "", "value": "" });
  }, "application/json");
  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,
    pagebar: { type: "simple" },

    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyBranch", //数据接口
    type: "post", //数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ "companytype": "S" }), //查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //货主table
  var consignor_Table = {
    columns: [
      { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "companycode", name: "货主编号", type: "ro" },
      { id: "companyname", name: "货主名称", type: "ro" }
    ],
    multiselect: false,
    pagebar: { type: "simple" },

    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyConsignor", //数据接口
    type: "post", //数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ "companytype": "H" }), //查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  var ddfrom = null;
  var opts = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "customerId", name: "id", type: "ro", width: "100", hidden: true },
      { id: "branchName", name: "配送中心", type: "ro", width: 150 },
      { id: "consignorName", name: "货主", type: "ro", width: 150 },
      {
        id: "customerCode", name: "个体编号", type: "ro", width: 100
      },
      { id: "customerName", name: "个体名称", type: "ro", width: 100 },
      { id: "address", name: "详细地址", type: "ro", width: 100 },
      {
        id: "provinceName", name: "省", type: "ro", width: 100
      },
      {
        id: "cityName", name: "市", type: "ro", width: 100
      },
      {
        id: "districtName", name: "区/县", type: "ro", width: 100
      },
      {
        id: "streetName", name: "乡镇/街道", type: "ro", width: 100
      },
      {
        id: "contactman", name: "联系人", type: "ro", width: 100
      },
      {
        id: "contacttel1", name: "联系方式1", type: "ro", width: 100
      },
      {
        id: "contacttel2", name: "联系方式2", type: "ro", width: 100
      },
      {
        id: "op", name: "操作", type: "button", align: "center", fixed: "right", width: 200, render: function () {
          return "删除^individualCustomer_deleteClick,修改^individualCustomer_updataClick,复制^individualCustomer_copy,详情^individualCustomer_details";
        }
      }
    ],
    multiselect: true,
    fixedLast: true, //开启固定列，固定最后一行，倒数第二列不能隐藏
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          {
            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 fromdata = grid.getToolBarForm().getFormData();
                data = fromdata.buyername1.length == 2 ? "" : data;
                var datad = {};
                datad.branchId = data.companyid ? data.companyid : "";
                grid.getToolBarForm().setFormData(datad);
              });
            }
          },
          { type: "hidden", name: "branchId", label: "配送中心:", className: "layui-col-md3" },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择货主",
            readonly: false,
            table: consignor_Table,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "companyname",
            fieldValue: "companycode",
            name: "consignorcode1",
            label: "货主:",
            listener: function (consignorGrid) {
              setMethod(consignorGrid, consignor_Table, function (data) {
                var fromdata = grid.getToolBarForm().getFormData();
                data = fromdata.consignorcode1.length == 2 ? "" : data;
                var datad = {};
                datad.consignor = data.companyid ? data.companyid : "";
                grid.getToolBarForm().setFormData(datad);
              });
            }
          },
          { type: "hidden", name: "consignor", label: "货主:", className: "layui-col-md3" },
          { type: "input", name: "contactman", label: "联系人:", className: "layui-col-md3" },
          { type: "input", name: "contacttel1", label: "联系方式:", className: "layui-col-md3" },
          {
            type: "block", className: "layui-col-md6 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-normal", name: "addBtn", offsetLeft: 10, value: "<i class='layui-icon layui-icon-add-1'></i>新增" }
            ]
          }
        ]
      }],

    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseIndividualCustomer/queryTBaseIndividualCustomer",//数据接口
    type: "post",//数据提交方式，默认为get
    pagebar: {
      buttons: [
        { text: "删除", className: "layui-btn-danger", name: "delete" }
      ]
    },
    contentType: "application/json",
    params: "{}",//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  grid = new PlgGrid(opts);
  grid.renderTo("individualCustomer_guojzhuans2");
  $("#individualCustomer_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 fdata = grid.getToolBarForm().getFormData();
      grid.setParams(JSON.stringify(fdata));
      grid.reload();
    }
    if (name == "addBtn") {
      setView("#individualCustomer_div1");
      $("#individualCustomer_div4").html("");
      ddfrom = new PlgForm(fromDataJson);
      ddfrom.enableItem("customerCode");
      $("#individualCustomer_title").html("新增");
      linkage(0, "");
    }
    if (name == "work_Reset") {
      grid.getToolBarForm().getDForm().setText("buyername1", "");
      grid.getToolBarForm().getDForm().setText("consignorcode1", "");
      grid.getToolBarForm().clear();
    }
  });
  //按钮添加方法
  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_MAINDATA_SERVICE+"/tBaseIndividualCustomer/deleteTBaseIndividualCustomer", "post", data, function (data) {
        grid.reload();
      }, "application/json");
    }
  });
  //删除
  window.individualCustomer_deleteClick = function (rid, rdata) {
    grid.selectRowById(rid);
    rdata = JSON.stringify([rdata]);
    utilDelete(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseIndividualCustomer/deleteTBaseIndividualCustomer", "post", rdata, function (data) {

      grid.reload();
    }, "application/json");
  };
  //修改
  window.individualCustomer_updataClick = function (rid, data) {
    grid.selectRowById(rid);
    $("#individualCustomer_div4").html("");
    setView("#individualCustomer_div1");
    ddfrom = new PlgForm(fromDataJson);
    ddfrom.disableItem("customerCode");
    ddfrom.setFormData(data);
    ddfrom.getDForm().setText("buyername1", data.branchName);
    ddfrom.getDForm().setText("consignorcode1", data.consignorName);
    linkage(1, data);
    $("#individualCustomer_title").html("编辑");
  };
  //复制
  window.individualCustomer_copy = function (rid, data) {
    grid.selectRowById(rid);
    $("#individualCustomer_div4").html("");
    ddfrom = new PlgForm(fromDataJson);
    ddfrom.enableItem("customerCode");
    data.customerId = "";
    ddfrom.setFormData(data);
    linkage(1, data);
    ddfrom.getDForm().setText("buyername1", data.branchName);
    ddfrom.getDForm().setText("consignorcode1", data.consignorName);
    setView("#individualCustomer_div1");
    $("#individualCustomer_title").html("新增");
  };
  //详情
  window.individualCustomer_details = function (rid, rdata) {
    grid.selectRowById(rid);
    setView("#individualCustomer_div9");
    new PlgPanel({
      renderer: "individualCustomer_div422",
      skin: 2,
      header: {
        isShow: true,
        title: "基本信息"
      },
      defaultBody: {
        layoutCol: 3,
        valueBj: true,
        cols: [
          [{
            label: "配送中心",
            value: rdata.branchName
          },
          {
            label: "货主",
            value: rdata.consignorName
          },
          {
            label: "个体编号",
            value: rdata.customerCode
          },
          {
            label: "个体名称",
            value: rdata.customerName
          }],
          [
            {
              label: "详细地址",
              value: rdata.address
            },
            {
              label: "省",
              value: rdata.provinceName
            },
            {
              label: "市",
              value: rdata.cityName
            },
            {
              label: "区/县",
              value: rdata.districtName
            }
          ],
          [
            {
              label: "乡镇/街道",
              value: rdata.streetName
            },
            {
              label: "联系人",
              value: rdata.contactman
            },
            {
              label: "联系方式1",
              value: rdata.contacttel1
            },
            {
              label: "联系方式2",
              value: rdata.contacttel2
            }
          ]
        ]
      }
    });
  };
  //跳转页面
  var carousel = layui.carousel;
  carousel.render({
    elem: "#individualCustomer_div2",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", //设置容器宽度
    height: "100%"
  });
  let viewList = ["#individualCustomer_div1", "#individualCustomer_div3", "#individualCustomer_div9"];
  function setView(name) {
    for (let i = 0; i < viewList.length; i++) {
      if (name == viewList[i]) {
        $(name).css("display", "block");
      }
      else {
        $(viewList[i]).css("display", "none");
      }
    }
  }
  fromDataJson = {
    renderer: "individualCustomer_div4",
    items: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "hidden", name: "customerId", 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 fromdata = ddfrom.getFormData();
                data = fromdata.buyername1.length == 2 ? "" : data;
                var datad = {};
                datad.branchId = data.companyid ? data.companyid : "";
                datad.branchName = data.companyname ? data.companyname : "";
                ddfrom.setFormData(datad);
              });
            }
          },
          { type: "hidden", name: "branchId", label: "配送中心:", className: "layui-col-md3" },
          { type: "hidden", name: "branchName", label: "配送中心:", className: "layui-col-md3" },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择货主",
            readonly: false,
            table: consignor_Table,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "companycode",
            fieldValue: "companycode",
            name: "consignorcode1",
            label: "货主:<span class='dhxform_item_required'>*<span>",
            listener: function (consignorGrid) {
              setMethod(consignorGrid, consignor_Table, function (data) {
                var fromdata = ddfrom.getFormData();
                data = fromdata.consignorcode1.length == 2 ? "" : data;
                var datad = {};
                datad.consignor = data.companyid ? data.companyid : "";
                datad.consignorName = data.companyname ? data.companyname : "";
                ddfrom.setFormData(datad);
              });
            }
          },
          { type: "hidden", name: "consignor", label: "货主:", className: "layui-col-md3" },
          { type: "hidden", name: "consignorName", label: "货主:", className: "layui-col-md3" },
          { type: "input", name: "customerCode", label: "个体编号:", className: "layui-col-md3", required: true },
          { type: "input", name: "customerName", label: "个体名称:", className: "layui-col-md3", required: true }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", name: "contactman", label: "联系人:", className: "layui-col-md3", required: true },
          { type: "input", name: "contacttel1", label: "联系方式1:", className: "layui-col-md3", required: true },
          { type: "input", name: "contacttel2", label: "联系方式2:", className: "layui-col-md3" },
          { type: "combo", name: "province", label: "省:", className: "layui-col-md3", options: province, required: true },
          { type: "hidden", name: "provinceName", label: "id:", className: "layui-col-md3" }
        ]
      },
      {
        type: "row",
        list: [
          { type: "combo", name: "city", label: "市:", className: "layui-col-md3", required: true },
          { type: "hidden", name: "cityName", label: "id:", className: "layui-col-md3" },
          { type: "combo", name: "district", label: "区:", className: "layui-col-md3", required: true },
          { type: "hidden", name: "districtName", label: "id:", className: "layui-col-md3" },
          { type: "combo", name: "street", label: "乡镇/街道:", className: "layui-col-md3", required: true },
          { type: "hidden", name: "streetName", label: "id:", className: "layui-col-md3" },
          { type: "input", name: "memo", label: "备注:", className: "layui-col-md3" }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", name: "address", label: "详细地址:", className: "layui-col-md6", required: true }

        ]
      }
    ]
  };
  //返回
  $(document).on("click", "#individualCustomer_return", function () {
    setView("#individualCustomer_div3");
  });
  //保存
  document.querySelector("#individualCustomer_save").onclick = function () {
    //数据data
    var data = ddfrom.getFormData();
    var feik = ddfrom.validate();
    if (!feik) {
      return;
    }
    data.provinceName = ddfrom.getCombo("province").getComboText();
    data.cityName = ddfrom.getCombo("city").getComboText();
    data.districtName = ddfrom.getCombo("district").getComboText();
    data.streetName = ddfrom.getCombo("street").getComboText();
    //请求地址和类型
    var url = UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseIndividualCustomer/updateTBaseIndividualCustomer";
    var type = "post";
    var contentType = "application/json";
    //message新增为0，修改为1
    if (data.customerId == null || data.customerId == "") {
      url = UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseIndividualCustomer/addTBaseIndividualCustomer";
    }
    //调用ajax方法
    utilAjaxGJ(url, type, JSON.stringify(data), function (data) {

      setView("#individualCustomer_div3");
      grid.reload();
    }, contentType);
  };
  //三级联动
  function linkage(type, data) {
    var f = ddfrom;
    f.attachComboEvent("province", "onChange", function (id) {
      f.getCombo("city").setComboText("");
      f.loadComboData("city", { url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/zone/get-by-pid", type: "post", contentType: "application/json", data: JSON.stringify({ "fatherid": id }), valueField: "zoneid", textField: "keyvalue" });
    });
    f.attachComboEvent("city", "onChange", function (id) {
      f.getCombo("district").setComboText("");
      f.loadComboData("district", { url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/zone/get-by-pid", type: "post", contentType: "application/json", data: JSON.stringify({ "fatherid": id }), valueField: "zoneid", textField: "keyvalue" });
    });
    f.attachComboEvent("district", "onChange", function (id) {
      f.getCombo("street").setComboText("");
      f.loadComboData("street", { url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/zone/get-by-pid", type: "post", contentType: "application/json", data: JSON.stringify({ "fatherid": id }), valueField: "zoneid", textField: "keyvalue" });
    });
    if (type == 1) {
      f.loadComboData("city", { url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/zone/get-by-pid", type: "post", contentType: "application/json", data: JSON.stringify({ "fatherid": data.province }), valueField: "zoneid", textField: "keyvalue" });
      f.getCombo("city").setComboText(data.cityName == "" ? data.city : data.cityName);
      f.loadComboData("district", { url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/zone/get-by-pid", type: "post", contentType: "application/json", data: JSON.stringify({ "fatherid": data.city }), valueField: "zoneid", textField: "keyvalue" });
      f.getCombo("district").setComboText(data.districtName == "" ? data.district : data.districtName);
      f.loadComboData("street", { url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/zone/get-by-pid", type: "post", contentType: "application/json", data: JSON.stringify({ "fatherid": data.district }), valueField: "zoneid", textField: "keyvalue" });
      f.getCombo("street").setComboText(data.streetName == "" ? data.street : data.streetName);
    }
  }
};
