/**
* 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 () => {
  //获取下拉框
  let dataSelect;
  getSelectGJ([
    { "objectcode": "PRICE_TYPE" },
    { "objectcode": "COMPANY_TYPE", "groupcode2": "1" }
  ], function (data) {
    dataSelect = data.data;
    dataSelect.PRICE_TYPE.unshift({ "text": "", "value": "" });
    dataSelect.COMPANY_TYPE.unshift({ "text": "", "value": "" });
  });
  // 获取默认时间
  var startTime = Prolog.formatDate(new Date(), "%YY-%MM") + "-01 00:00:00";
  var endTime = Prolog.formatDate(new Date()) + " 23:59:59";
  let expenItem;
  //应收方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, render: (v) => {
          return getSelectValue(dataSelect.PRICE_TYPE, v);
        }
      },
      { id: "feeamt", name: "金额", type: "ro", width: 110 },
      { id: "memo", name: "备注", type: "ro", width: 110 },
      {
        id: "op", name: "操作", type: "button", align: "center", fixed: "right", width: 200, render: function () {
          return "删除^expenseEntry_deleteClick,修改^expenseEntry_updataClick,复制^expenseEntry_copy,详情^expenseEntry_details";
        }
      }
    ],
    multiselect: true,
    fixedLast: 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: "<i class='layui-icon layui-icon-add-1'></i>新增" }
            ]
          }
        ]
      }],

    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeBill/queryBmsFeeBill",//数据接口
    type: "post",//数据提交方式，默认为get
    pagebar: {
      buttons: [
        { text: "删除", className: "layui-btn-danger", name: "delete" }
      ]
    },
    contentType: "application/json",
    params: JSON.stringify({ "flag": "inputQuery", sdate: startTime, edate: endTime }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  var grid = new PlgGrid(opts);
  grid.renderTo("expenseEntry_guojzhuans2");
  grid.loadData();
  //按钮添加方法
  grid.attachToolBarEvent("onButtonClick", function (name) {
    if (name == "querywork") {
      var fdata = grid.getToolBarForm().getFormData();
      fdata.feetypeid = fdata.feetypeid == "" || fdata.feetypeid == null ? "" : JSON.parse(fdata.feetypeid)[0];
      fdata.payableId = fdata.payableId == "" || fdata.payableId == null ? "" : JSON.parse(fdata.payableId)[0];
      fdata.receivableId = fdata.receivableId == "" || fdata.receivableId == null ? "" : JSON.parse(fdata.receivableId)[0];
      fdata.flag = "inputQuery";
      fdata.sdate = FormatDateTime(fdata.sdate);
      fdata.edate = Prolog.formatDate(fdata.edate) + " 23:59:59";
      if (fdata.sdate > fdata.edate) {
        layer.msg("开始时间不能大于结束时间");
        return false;
      }
      grid.setParams(JSON.stringify(fdata));
      grid.reload();
    }
    if (name == "addBtn") {
      setViewName("#expenseEntry_div1");
      $("#expenseEntry_div4").html("");
      ddfrom = new PlgForm(expenItem);
      ddfrom.enableItem("feebillNo");
      $("#expenseEntry_title").html("新增");
    }
    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 });
    }
  });
  //按钮添加方法
  grid.attachPageBarEvent(function (name) {
    //删除
    if (name == "delete") {
      let datas = getDeleteAll(grid);
      datas = JSON.stringify(datas);
      utilDelete(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeBill/deleteBmsFeeBill", "post", datas, function (data) {
        grid.reload();
      }, "application/json");
    }
  });
  //删除
  window.expenseEntry_deleteClick = function (rid) {
    grid.selectRowById(rid);
    rid = JSON.stringify([rid]);
    utilDelete(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeBill/deleteBmsFeeBill", "post", rid, function (data) {

      grid.reload();
    }, "application/json");
  };
  //修改
  window.expenseEntry_updataClick = function (rid, data) {
    grid.selectRowById(rid);
    $("#expenseEntry_div4").html("");
    ddfrom = new PlgForm(expenItem);
    ddfrom.disableItem("feebillNo");
    ddfrom.setFormData(data);
    ddfrom.getDForm().setText("buyername2", data.receivableName);
    ddfrom.getDForm().setText("buyername1", data.feetypename);
    ddfrom.getDForm().setText("buyername3", data.payableName);
    setViewName("#expenseEntry_div1");
    $("#expenseEntry_title").html("编辑");
  };
  //复制
  window.expenseEntry_copy = function (rid, data) {
    grid.selectRowById(rid);
    $("#expenseEntry_div4").html("");
    ddfrom = new PlgForm(expenItem);
    ddfrom.enableItem("feebillNo");
    data.feebillId = "";
    ddfrom.getDForm().setText("buyername2", data.receivableName);
    ddfrom.getDForm().setText("buyername1", data.feetypename);
    ddfrom.getDForm().setText("buyername3", data.payableName);
    ddfrom.setFormData(data);
    setViewName("#expenseEntry_div1");
    $("#expenseEntry_title").html("新增");
  };
  //详情
  window.expenseEntry_details = function (rid, rdata) {
    grid.selectRowById(rid);
    setViewName("#expenseEntry_div9");
    new PlgPanel({
      renderer: "expenseEntry_div422",
      skin: 2,
      header: {
        isShow: true,
        title: "基本信息"
      },
      defaultBody: {
        layoutCol: 3,
        valueBj: true,
        cols: [
          [{
            label: "费用单号",
            value: rdata.feebillNo
          },
          {
            label: "费用时间",
            value: rdata.feebillDate
          },
          {
            label: "应收方编号",
            value: rdata.receivableCode
          },
          {
            label: "应收方名称",
            value: rdata.receivableName
          }],
          [
            {
              label: "应付方编号",
              value: rdata.payableCode
            },
            {
              label: "应付方名称",
              value: rdata.payableName
            },
            {
              label: "费用项目编号",
              value: rdata.feetypecode
            },
            {
              label: "费用项目名称",
              value: rdata.feetypename
            }
          ],
          [
            {
              label: "关联业务单号",
              value: rdata.refbillno
            },
            {
              label: "关联业务类型",
              value: getSelectValue(dataSelect.PRICE_TYPE, rdata.refbilltype)
            },
            {
              label: "计费数量",
              value: rdata.qty
            },
            {
              label: "计费价格",
              value: rdata.price
            }
          ],
          [
            {
              label: "计费价格类型",
              value: getSelectValue(dataSelect.PRICE_TYPE, rdata.pricetype)
            },
            {
              label: "金额",
              value: rdata.feeamt
            },
            {
              label: "备注",
              value: rdata.memo
            }
          ]
        ]
      }
    });
  };
  //跳转页面
  var carousel = layui.carousel;
  carousel.render({
    elem: "#expenseEntry_div2",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", //设置容器宽度
    height: "100%"
  });
  let dataView = ["#expenseEntry_div1", "#expenseEntry_div3", "#expenseEntry_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");
      }
    }
  }
  expenItem = {
    renderer: "expenseEntry_div4",
    items: [
      { type: "settings", position: "label-left", width: 300, labelWidth: 90 },
      { type: "hidden", name: "feebillId", label: "id:", width: 230 },
      { type: "input", name: "feebillNo", label: "费用单号:", width: 230, editable: false, validate: "NotEmpty", required: true },
      {
        width: 230,
        type: "selectTable",
        placeholder: "请选择费用项目",
        readonly: false,
        table: objectTable,
        tableWidth: 500,
        tableHeight: 400,
        fieldText: "feetypename",
        fieldValue: "feetypeid",
        name: "buyername1",
        label: "费用项目:",
        listener: function (consignorGrid) {
          setMethod(consignorGrid, objectTable, function (data) {
            var datad = {};
            datad.feetypeid = data.feetypeid;
            datad.feetypecode = data.feetypecode;
            datad.feetypename = data.feetypename;
            ddfrom.setFormData(datad);
          });
        }
      },
      { type: "hidden", name: "feetypeid", label: "费用项目:", width: 230 },
      { type: "hidden", name: "feetypecode", label: "费用项目:", width: 230 },
      { type: "hidden", name: "feetypename", label: "费用项目:", width: 230 },
      { type: "input", name: "qty", label: "计费数量:", width: 230, validate: "Money_11bit" },
      { type: "input", name: "feeamt", label: "金额:", width: 230, validate: "Money_11bit" },
      { type: "newcolumn", offset: 10 },
      {
        width: 230,
        type: "selectTable",
        placeholder: "请选择应收方",
        readonly: false,
        table: userTable,
        tableWidth: 600,
        tableHeight: 500,
        fieldText: "companyname",
        fieldValue: "companyid",
        name: "buyername2",
        label: "应收方:",
        listener: function (consignorGrid) {
          setMethod(consignorGrid, userTable, function (data) {
            var datad = {};
            datad.receivableId = data.companyid;
            datad.receivableCode = data.companycode;
            datad.receivableName = data.companyname;
            ddfrom.setFormData(datad);
          });
        }
      },
      { type: "hidden", name: "receivableId", label: "应收方:", width: 230 },
      { type: "hidden", name: "receivableCode", label: "应收方:", width: 230 },
      { type: "hidden", name: "receivableName", label: "应收方:", width: 230 },
      { type: "input", name: "refbillno", label: "关联业务单号:", width: 230 },
      { type: "input", name: "price", label: "计费价格:", width: 230, validate: "Money_11bit" },
      { type: "input", name: "memo", label: "备注:", width: 230 },
      { type: "newcolumn", offset: 10 },
      {
        width: 230,
        type: "selectTable",
        placeholder: "请选择应付方",
        readonly: false,
        table: userTable,
        tableWidth: 600,
        tableHeight: 500,
        fieldText: "companyname",
        fieldValue: "companyid",
        name: "buyername3",
        label: "应付方:",
        listener: function (consignorGrid) {
          setMethod(consignorGrid, userTable, function (data) {
            var datad = {};
            datad.payableId = data.companyid;
            datad.payableCode = data.companycode;
            datad.payableName = data.companyname;
            ddfrom.setFormData(datad);
          });
        }
      },
      { type: "hidden", name: "payableId", label: "应付方:", width: 230 },
      { type: "hidden", name: "payableCode", label: "应付方:", width: 230 },
      { type: "hidden", name: "payableName", label: "应付方:", width: 230 },
      { type: "input", name: "refbilltype", label: "关联业务类型:", width: 230 },
      { type: "combo", name: "pricetype", label: "计费价格类型:", width: 230, options: dataSelect.PRICE_TYPE, readonly: true }
    ]
  };
  //返回
  $(document).on("click", "#expenseEntry_return", function () {
    setViewName("#expenseEntry_div3");
  });
  //保存
  document.querySelector("#expenseEntry_save").onclick = function () {
    //数据data
    var data = ddfrom.getFormData();
    var feik = ddfrom.validate();
    if (!feik) {
      return;
    }
    //请求地址和类型
    var url = UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeBill/updateBmsFeeBill";
    var type = "post";
    var contentType = "application/json";
    //message新增为0，修改为1
    if (data.feebillId == null || data.feebillId == "") {
      url = UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeBill/addBmsFeeBill";
    }
    //调用ajax方法
    utilAjaxGJ(url, type, JSON.stringify(data), function (data) {

      setViewName("#expenseEntry_div3");
      grid.reload();
    }, contentType);
  };
};
