/**
* guoj
* 2020.09.21
* 对账生成管理
*/
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 startTime = Prolog.formatDate(new Date(), "%YY-%MM") + "-01 00:00:00";
  var endTime = Prolog.formatDate(new Date()) + " 23:59:59";

  //获取下拉框
  let dataSelect;
  getSelectGJ([
    { "objectcode": "COMPANY_TYPE", "groupcode2": "1" }
  ], function (data) {
    dataSelect = data.data;
    dataSelect.COMPANY_TYPE.unshift({ "text": "", "value": "" });
  });
  //应收方table
  var userTable = {
    columns: [
      { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "companycode", name: "编号", type: "ro" },
      { id: "companyname", name: "名称", type: "ro" },
      { id: "companytypeName", name: "单位类型", type: "ro" }
    ],
    toolbar: [
      { type: "settings", labelWidth: 60, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "input", name: "companycode", label: "单位编码:", className: "layui-col-md6" },
          { type: "input", name: "companyname", label: "单位名称:", className: "layui-col-md6" },
          { type: "combo", name: "companytype", label: "单位类型:", className: "layui-col-md6", options: dataSelect.COMPANY_TYPE, readonly: true },
          {
            type: "block", className: "layui-col-md6 form_btn_left",
            list: [
              { type: "button", name: "querycarr", className: "btn-normal", value: "查询" },
              { type: "newcolumn" },
              { type: "button", name: "car_Reset", value: "重置" }
            ]
          }
        ]
      }
    ],
    multiselect: false,
    pagebar: { type: "simple" },

    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompany", //数据接口
    type: "post", //数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({}), //查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //费用项目table
  var objectTable = {
    columns: [
      { id: "feetypeid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "feetypecode", name: "费用项目编号", type: "ro" },
      { id: "feetypename", name: "费用项目名称", type: "ro" }
    ],
    multiselect: false,

    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeType/queryBmsFeeType",//数据接口
    type: "post", //数据提交方式，默认为get
    pagebar: { type: "simple" },
    params: "{}", //查询提交参数，分页参数默认为pageNum,pageSize
    contentType: "application/json",
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  var ddfrom = null;
  var opts = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "feebillId", name: "id", type: "ro", width: "100", hidden: true },
      { id: "feebillNo", name: "费用单号", type: "ro", width: 100 },
      { id: "feebillDate", name: "费用时间", type: "ro", width: 130 },
      { id: "receivableCode", name: "应收方编号", type: "ro", width: 100 },
      { id: "receivableName", name: "应收方名称", type: "ro", width: 100 },
      { id: "payableCode", name: "应付方编号", type: "ro", width: 100 },
      { id: "payableName", name: "应付方名称", type: "ro", width: 100 },
      { id: "feetypecode", name: "费用项目编号", type: "ro", width: 110 },
      { id: "feetypename", name: "费用项目名称", type: "ro", width: 110 },
      { id: "refbillno", name: "关联业务单号", type: "ro", width: 110 },
      { id: "refbilltype", name: "关联业务类型", type: "ro", width: 110 },
      { id: "qty", name: "计费数量", type: "ro", width: 110 },
      { id: "price", name: "计费价格", type: "ro", width: 110 },
      { id: "pricetype", name: "计费价格类型", type: "ro", width: 110, hidden: true },
      { id: "pricetypeName", name: "计费价格类型", type: "ro", width: 110 },
      { id: "feeamt", name: "金额", type: "ro", width: 110 },
      { id: "memo", name: "备注", type: "ro", width: 110 }
    ],
    multiselect: true,
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", name: "sdate", label: "开始时间:", className: "layui-col-md3", value: startTime },
          { type: "calendar", name: "edate", label: "结束时间:", className: "layui-col-md3", value: endTime },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择应收方",
            readonly: false,
            table: userTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "receivableId",
            label: "应收方:",
            listener: function (Grid) {
              setMethod(Grid, userTable, function () {
              });
            }
          },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择应付方",
            readonly: false,
            table: userTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "payableId",
            label: "应付方:",
            listener: function (Grid) {
              setMethod(Grid, userTable, function () {
              });
            }
          },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择费用项目",
            readonly: false,
            table: objectTable,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "feetypename",
            fieldValue: "feetypeid",
            name: "feetypeid",
            label: "费用项目:",
            listener: function () {
            }
          },
          { type: "input", name: "refbillno", 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-md3 right",
            list: [
              { type: "button", className: "btn-normal", name: "addBtn", offsetLeft: 10, value: "对账生成" }
            ]
          }
        ]
      }],

    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeBill/queryBmsFeeBill",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ "flag": "accountQuery", sdate: startTime, edate: endTime }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  var grid = new PlgGrid(opts);
  grid.renderTo("reconciliationManagement_guojzhuans2");
  grid.loadData();
  //按钮添加方法
  grid.attachToolBarEvent("onButtonClick", function (name) {
    //查询
    if (name == "querywork") {
      var fdata = grid.getToolBarForm().getFormData();
      fdata.sdate = FormatDateTime(fdata.sdate);
      fdata.edate = Prolog.formatDate(fdata.edate) + " 23:59:59";
      if (fdata.sdate > fdata.edate) {
        layer.msg("开始时间不能大于结束时间");
        return false;
      }
      fdata.flag = "accountQuery";
      fdata.receivableId = fdata.receivableId == null || fdata.receivableId == "" ? "" : JSON.parse(fdata.receivableId)[0];
      fdata.payableId = fdata.payableId == null || fdata.payableId == "" ? "" : JSON.parse(fdata.payableId)[0];
      fdata.feetypeid = fdata.feetypeid == null || fdata.feetypeid == "" ? "" : JSON.parse(fdata.feetypeid)[0];
      grid.setParams(JSON.stringify(fdata));
      grid.reload();
    }
    //对账生成
    if (name == "addBtn") {
      let ids = grid.getCheckedIds();
      if (!ids) {
        layer.msg("请选择数据");
        return false;
      }
      let dataList = ids.split(",").map(id => grid.getRowData(id));
      let feeamt = 0;
      for (var i = 0; i < dataList.length; i++) {
        if (i != 0 && dataList[i].payableId != dataList[0].payableId) {
          layer.msg("所有明细应付方必须相同才可以对账");
          return;
        }
        if (i != 0 && dataList[i].receivableId != dataList[0].receivableId) {
          layer.msg("所有明细应收方必须相同才可以对账");
          return;
        }
        feeamt += dataList[i].feeamt * 1;
      }
      let formData = Object.assign({}, dataList[0], { feeamt, billnum: dataList.length });
      ddfrom.clear();
      ddfrom.setFormData(formData);
      $("#reconciliationManagement_title").html("对账生成");
      setViewName("#reconciliationManagement_div1");
    }
    //重置
    if (name == "work_Reset") {
      grid.getToolBarForm().getDForm().setText("feetypeid", "");
      grid.getToolBarForm().getDForm().setText("receivableId", "");
      grid.getToolBarForm().getDForm().setText("payableId", "");
      grid.getToolBarForm().clear();
      grid.getToolBarForm().setFormData({ sdate: startTime, edate: endTime });
    }
  });
  //跳转页面
  var carousel = layui.carousel;
  carousel.render({
    elem: "#reconciliationManagement_div2",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", //设置容器宽度
    height: "100%"
  });
  let dataView = ["#reconciliationManagement_div1", "#reconciliationManagement_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");
      }
    }
  }
  ddfrom = new PlgForm({
    renderer: "reconciliationManagement_div4",
    items: [
      { type: "settings", position: "label-left", width: 300, labelWidth: 90 },
      { type: "hidden", name: "feebillId", label: "id:", width: 230 },
      { type: "hidden", name: "receivableId", label: "应收方:", width: 230 },
      { type: "hidden", name: "receivableCode", label: "应收方:", width: 230 },
      { type: "input", name: "receivableName", label: "应收方:", readonly: true, width: 230 },
      { type: "input", name: "billnum", label: "总单数:", readonly: true, width: 230, editable: false },
      { type: "input", name: "memo", label: "备注:", width: 230 },
      { type: "newcolumn", offset: 10 },
      { type: "hidden", name: "payableCode", label: "应付方:", width: 230 },
      { type: "hidden", name: "payableId", label: "应付方:", width: 230 },
      { type: "input", name: "payableName", readonly: true, label: "应付方:", width: 230 },
      { type: "input", name: "feeamt", readonly: true, label: "总金额:", width: 230 }]
  });
  //返回
  $("#reconciliationManagement_return").on("click", () => {
    setViewName("#reconciliationManagement_div3");
  });
  //保存
  $("#reconciliationManagement_save").on("click", () => {
    layer.open({
      title: "保存操作",
      content: " 是否确定保存",
      btn: ["确认", "取消"],
      yes: function () {
        let dataList = getDeleteAll(grid);
        for (var i = 0; i < dataList.length; i++) {
          dataList[i] = grid.getRowData(dataList[i]);
        }
        //数据data
        var data = ddfrom.getFormData();
        data.list = dataList;
        //调用ajax方法
        utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeAccountBill/bmsFeeAccountSave", "post", JSON.stringify(data), function (data) {
          layer.closeAll();
            setViewName("#reconciliationManagement_div3");
          grid.reload();
        }, "application/json");
      },
      btn2: function () {
        layer.closeAll();
      }
    });

  });
};
