<template>
  <SybProductTradeDivideAccountsTaskList></SybProductTradeDivideAccountsTaskList>
</template>
    <script setup lang="ts">
import {
  API_URL_MCH_APPLYMENT_LIST,
  req,
  reqLoad,
  $exportExcel,
  exportExcelUrl,
  API_URL_STATISTIC,
  API_MCH_PAY_DEFINES,
  API_URL_MCH_STORE_LIST,
  API_URL_IFDEFINES_LIST
} from "@/api/manage";
import { reactive, ref, getCurrentInstance, onMounted } from "vue";
import fileDownload from "js-file-download";
import { message } from "ant-design-vue";
import { useRoute } from "vue-router";
import router from "@/router";

const props = defineProps({
  configMode: { type: String, default: "" } // 模式
});

const {
  $infoBox,
  $access,
  $hasAgentEnt
} = getCurrentInstance()!.appContext.config.globalProperties;
const ledgerGroupAdd = ref();
const showAddTask = ref();
const pageRender = ref();

const tableColumns = reactive([
  { key: "name", title: "商户名称", dataIndex: "name" },
  //   { key: "name", title: "商户号", dataIndex: "name" },
  //   { key: "name", title: "商户名称", dataIndex: "name" },
  { key: "name", title: "门店名称", dataIndex: "name" },
  { key: "name", title: "门店编号", dataIndex: "name" },
  { key: "name", title: "支付通道", dataIndex: "name" },
  { key: "name", title: "所属渠道", dataIndex: "name" },
  { key: "name", title: "分账任务", dataIndex: "name" },
  { key: "name", title: "分账详情  ", dataIndex: "name" }
  //   { key: "createdAt", title: "创建时间", dataIndex: "createdAt" },
  //   { key: "op", title: "操作", fixed: "right", align: "center" }
]);

const infoTable = ref();
const rules = ref();

let data: any = reactive({
  tableColumns: tableColumns,
  searchData: { sysType: "PLATFORM" } as any,
  btnLoading: false
});

let count: any = ref([]); // 数据统计数组
let vdata: any = reactive({
  isshows: false,
  storeList: [] as any,
  mchApplyList: [] as any,
  filteredMchApplyList: [] as any,
  filteredStoreInfoList: [] as any,
  saveObject: {} as any,
  userAddTitle: "",
  visible: false,
  showRechargeModal: false,
  rechargeModalLoading: false, // loading
  rechargeObject: {}, // 预审核结果
  isShow: false, // 发布任务
  showPreRouteModal: false, // 是否显示预审核弹层
  preRouteModalLoading: false, // loading
  preRouteResult: {} // 预审核结果
});

// 请求table接口数据
function reqTableDataFunc(params) {
  reqTableCountFunc(params);
  return req.list(API_URL_MCH_APPLYMENT_LIST, params);
}

function addtask(e) {
  if (e == 1) {
    showAddTask.value.show(true);
  } else if (e == 2) {
    pageRender.value.pageRender();
  } else {
    console.log("未开启");
  }
}

function reqTableCountFunc(params: any) {
  params.method = "transaction";
  req.list(API_URL_STATISTIC + "/total", params).then(res => {
    count.value = [
      {
        title: "总成交金额",
        symbol: "add",
        textColor: "#1A66FF",
        content: (res.totalSuccAmt / 100).toFixed(2)
      },
      { type: "line" },
      { title: "总成交笔数", content: res.totalSuccNum },
      { type: "line" },
      {
        title: "总退款金额",
        symbol: "sub",
        content: (res.totalRefundAmt / 100).toFixed(2)
      },
      { type: "line" },
      { title: "总退款笔数", content: res.totalRefundNum }
    ];
  });
}
function searchFunc() {
  // 点击【查询】按钮点击事件
  vdata.btnLoading = true; // 打开查询按钮上的loading
  infoTable.value.refTable(true);
}

function addFunc() {
  // 业务通用【新增】 函数
  mchApplyList();
  vdata.showPreRouteModal = true;
}

function editFunc(record) {
  // 业务通用【修改】 函数
  vdata.showPreRouteModal = false;
  vdata.userAddTitle = "新建付款账户";
}
function delFunc(recordId) {
  // 业务通用【删除】 函数
  $infoBox.confirmDanger("确认删除？", "", () => {
    return req.delById(API_URL_MCH_APPLYMENT_LIST, recordId).then(res => {
      $infoBox.message.success("删除成功！");
      infoTable.value.refTable(false);
    });
  });
}

function onCopySuccess() {
  $infoBox.message.success("复制成功");
}
// 预审核结果
function rechargeHandleOkFunc() {
  if (!vdata.rechargeResult.state) {
    return $infoBox.message.error("请选择审核结果");
  }

  if (vdata.rechargeResult.state == 8 && !vdata.rechargeResult.applyErrorInfo) {
    return $infoBox.message.error("请填写拒绝原因");
  }

  vdata.rechargeModalLoading = true;
}
function rechargeFunc(record) {
  vdata.showRechargeModal = true;
}
const sybProductTradeRouteStrategyAddOrEdit = ref();
// 分账配置
function fzallocation() {
  ledgerGroupAdd.value.show(true);
  //   vdata.copyInfoSourceApplyId = ""; // 清空副本信息

  //   vdata.showPreRouteModal = true;
  //   vdata.preRouteResult = { applyId: recordId };
}

// 显示弹层
function showPreRouteModalFunc(recordId) {
  vdata.copyInfoSourceApplyId = ""; // 清空副本信息

  vdata.showPreRouteModal = true;
  vdata.preRouteResult = { applyId: recordId };
}
// 预审核结果
function preRouteHandleOkFunc() {
  if (!vdata.preRouteResult.state) {
    return $infoBox.message.error("请选择审核结果");
  }

  if (vdata.preRouteResult.state == 8 && !vdata.preRouteResult.applyErrorInfo) {
    return $infoBox.message.error("请填写拒绝原因");
  }

  vdata.preRouteModalLoading = true;
}

function historyFunc(record) {
  vdata.visible = true;
}
function updateState(recordId, state) {
  // 【更新状态】

  const title = state === 1 ? "确认[启用]？" : "确认[停用]？";
  return new Promise<void>((resolve, reject) => {
    $infoBox.confirmDanger(
      title,
      "",
      () => {
        // return reqLoad.updateById(API_URL_MCH_QR_CODE_LIST, recordId, { qrcState: state }).then(res => {
        //   searchFunc()
        //   resolve()
        // }).catch(err => reject(err))
      },
      () => {
        reject(new Error());
      }
    );
  });
}

function tableExportFunc() {
  return $exportExcel(
    exportExcelUrl.statistic,
    Object.assign({}, vdata.searchData, {})
  )
    .then(res => {
      fileDownload(res.data, "商户统计.xlsx");
    })
    .catch(error => {
      console.log(error);
    });
}

function setAqfRouteInfo(e) {
  vdata.showPreRouteModal = false;
}

//门店
function storeList() {
  vdata.saveObject.storeId = "";
  req
    .list(API_URL_MCH_STORE_LIST, {
      pageSize: 100000,
      mchApplyName: vdata.saveObject.mchApplyId
    })
    .then(res => {
      vdata.filteredStoreInfoList = res.records;
      vdata.storeList = res.records;
    });
}

//商户
function mchApplyList() {
  req
    .list(API_URL_MCH_APPLYMENT_LIST, { pageSize: 100000, state: 99 })
    .then(res => {
      vdata.filteredMchApplyList = res.records;
      vdata.mchApplyList = res.records;
    });
}
function onClose() {
  vdata.showPreRouteModal = false;
}
function handleOkFunc() {}

function handleMchApplyChange(e) {
  vdata.saveObject.mchApplyId = e.value;
  storeList();
}

const handleMchApplySearch = value => {
  vdata.filteredMchApplyList = [];
  req
    .list(API_URL_MCH_APPLYMENT_LIST, {
      mchApplyName: value,
      pageSize: 100000,
      state: 99
    })
    .then(res => {
      vdata.filteredMchApplyList = res.records;
      vdata.mchApplyList = res.records;
    });
};
const handleStoreChange = value => {
  vdata.filteredStoreInfoList = [];
  vdata.storeList = [];
  req
    .list(API_URL_MCH_STORE_LIST, {
      storeName: value,
      pageSize: 100000,
      mchApplyName: vdata.saveObject.mchApplyId
    })
    .then(res => {
      vdata.filteredStoreInfoList = res.records;
      vdata.storeList = res.records;
    });
};
</script>
  
  <style lang="less" >
.bott {
  margin: 50px;
  width: 40%;
  margin-left: 25%;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  .botwarn {
    background-color: #e01515;
    border: #e01515;
  }
}

.card-box-div {
  margin: 10px 0 20px 0;
  .div-top-title-tio1 {
    margin: 18px 0 0 20px;

    .div-top-title-tio-span {
      font-weight: 600;
      font-size: 17px;
    }
  }
  .upload {
    margin-left: 30px;
  }
}

.edit-box {
  padding: 20px 30px;
  margin-bottom: 20px;
}
</style>
    
