/**
* 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": "PRICE_TYPE" },
    { "objectcode": "COMPANY_TYPE", "groupcode2": "1" }
  ], function (data) {
    dataSelect = data.data;
    dataSelect.PRICE_TYPE.unshift({ "text": "", "value": "" });
    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 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 }
    ],
    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: "审核" },
              { type: "newcolumn" },
              { type: "button", className: "btn-normal", name: "boHui", value: "驳回" }
            ]
          }
        ]
      }],

    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeBill/queryBmsFeeBill",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ "flag": "auditQuery", sdate: startTime, edate: endTime }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  var grid = new PlgGrid(opts);
  grid.renderTo("costAudit_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 = "auditQuery";
      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") {
      let ids = grid.getCheckedIds();
      if (!ids) {
        layer.msg("请选择数据");
        return false;
      }
      PlgDialog.open({
        title: "审核弹出框",
        shadeClose: true, //点击遮罩关闭
        content: "<div style='padding:23px'><text style='font-size:23px'>审核意见：</text><input type='text' style='font-size:23px' id='aostInput'/></div>",
        area: ["450px", "170px"],
        type: "2",
        btn: ["确认", "关闭"], //只是为了演示
        yes: function () {
          let auditdescrible = $("#aostInput").val();
          let dataList = ids.split(",").map(item => {
            const element = grid.getRowData(item);
            element.auditdescrible = auditdescrible;
            return element;
          });
          utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeBill/audit", "post", JSON.stringify(dataList), () => {
            layer.closeAll();
            grid.reload();
            layer.msg("成功");
          }, "application/json");
        },
        btn2: function () {
          layer.closeAll();
        }
      });
    }
    //重置
    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 });
    }
    //驳回
    if (name == "boHui") {
      let ids = grid.getCheckedIds();
      if (!ids) {
        layer.msg("请选择数据");
        return false;
      }
      PlgDialog.open({
        title: "驳回弹出框",
        shadeClose: true, //点击遮罩关闭
        content: "<div style='padding:23px'><text style='font-size:23px'>审核意见：</text><input type='text' style='font-size:23px' id='aostInput'/></div>",
        area: ["450px", "170px"],
        type: "2",
        btn: ["确认", "关闭"], //只是为了演示
        yes: function () {
          let auditdescrible = $("#aostInput").val();
          if (!auditdescrible) {
            layer.msg("审核结论必填");
            return;
          }
          let dataList = ids.split(",").map(item => {
            const element = grid.getRowData(item);
            element.auditdescrible = auditdescrible;
            return element;
          });
          utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeBill/reject", "post", JSON.stringify(dataList), () => {
            layer.closeAll();
            grid.reload();
            layer.msg("成功");
          }, "application/json");
        },
        btn2: function () {
          layer.closeAll();
        }
      });
    }
  });
};
