<template>
  <div class="page-container list-container">
    <el-row :gutter="24" style="width: 100%">
      <el-col :span="12">
        <div class="dashboard-total-item">
          <div class="dashboard-total-item-label">
            <span>
              <span>预存款余额</span>
            </span>
          </div>
          <div class="number-label">
            ￥ {{ rowNum(totalRemain ? totalRemain : 0) }}
          </div>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="dashboard-total-item">
          <div class="dashboard-total-item-label">
            <span>
              <span>赠送余额</span>
            </span>
          </div>
          <div class="number-label">
            ￥ {{ rowNum(donateRemain ? donateRemain : 0) }}
          </div>
        </div>
      </el-col>
    </el-row>
    <!-- 功能按钮区 -->
    <div class="list-btns-container">
      <el-button type="success" size="small" icon="Refresh" @click="getList">刷新</el-button>
      <el-dropdown trigger="click">
        <el-button type="primary" size="small">
          其它<el-icon class="el-icon--right"><arrow-down /></el-icon>
        </el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item icon="Download" @click="handleExport" v-hasPermi="['business:deduct:export']">
              导出</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
    <!-- 列表区 -->
    <el-table v-loading="loading" :data="orderList" size="small" border height="100%"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="委托单号" align="center" prop="formNo" min-width="140" />
      <el-table-column label="扣款单位" align="center" prop="contactCompany" min-width="150" />
      <el-table-column label="扣款信息" align="center">
        <template #default="scope">
          {{ scope.row.year }}年{{ scope.row.month }}月扣款
        </template>
      </el-table-column>
      <el-table-column label="扣款时间" align="center" prop="deductDate" min-width="120" />
      <el-table-column label="扣款金额" header-align="center" align="right" prop="amount" min-width="80">
        <template #default="scope">
          <span> {{ rowNum(scope.row.amount) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="扣款明细" align="center" prop="amount" min-width="80">
        <template #default="scope">
          <!-- <el-button link size="small" type="primary" @click="handleDetail(scope.row)">查看明细</el-button> -->
          <el-button link size="small" type="primary" v-hasPermi="['business:deduct:info']"
            @click="handleStatementAccount(scope.row)">跳转对账单</el-button>
        </template>
      </el-table-column>
    </el-table>
    <statementAccountForm ref="statementAccountFormRef" :get-list="getList" />
    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script setup name="Deduct">
  // import contractForm from "./form";
  import {
    listDeduct,
    delDeduct,
    exportDeduct,
  } from "@/api/business/entrust/deduct";
  import { rowNum } from "@/utils/index";
  import statementAccountForm from "./form.vue";
  import { getRemain } from "@/api/business/entrust/deposit";
  // import companyForm from "./form"
  const { proxy } = getCurrentInstance();
  /** 字典数组区 */
  /** 查询 对象 */

  const orderList = ref([]);
  const loading = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const prev = ref([]);
  const statementAccountFormRef = ref(null);

  const totalRemain = ref(0);
  const donateRemain = ref(0);

  const { contract_verify_status } = proxy.useDict("contract_verify_status");
  const { contract_status } = proxy.useDict("contract_status");
  /** 查询对象 */
  const queryParams = ref({
    pageNum: 1,
    pageSize: 20,
    orderByColumn: "create_time",
    companyName: "",
    noContract: 0,
  });

  const editStatus = {
    startMonth: false,
  };

  /***********************  方法区  ****************************/
  onActivated(() => {
    // 你的逻辑
    getList();
  });
  /** 查询company列表 */
  function getList() {
    loading.value = true;
    Promise.all([listDeduct(queryParams.value), getRemain()]).then((res) => {
      orderList.value = res[0].rows.map((l) => ({ ...l }));
      prev.value = proxy.deepClone(res[0].rows);
      total.value = res[0].total;
      totalRemain.value = res[1].data.totalRemain;
      donateRemain.value = res[1].data.donateRemain;
      loading.value = false;
    });
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }

  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal
      .confirm("是否确认删除选中的数据项？")
      .then(function () {
        return delDeduct(_ids);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功！");
      })
      .catch(() => { });
  }

  /** 导出按钮操作 */
  function handleExport() {
    exportDeduct(queryParams.value);
  }

  function startDateChangeHandler(row, startDate) {
    if (startDate) {
      // console.log(startDate)
      row.endMonth = proxy
        .moment(startDate)
        .add(row.monthNum - 1, "M")
        .format("YYYY-MM-DD");
    } else row.endMonth = null;
    console.log(row);
  }

  function saveHandler(row, field) {
    const index = prev.value.findIndex((l) => l.id === row.id);
    if (prev.value[index][field] === row[field]) {
      row.editStatus[field] = !row.editStatus[field];
    } else {
      proxy.$modal
        .confirm("确定修改起始月么?")
        .then((_) => {
          updateDeduct(row).then((res) => {
            getList();
          });
        })
        .catch((_) => { });
    }
  }

  function handleDetail(row) { }

  function handleStatementAccount(row) {
    statementAccountFormRef.value.open(row.id);
  }

  getList();
</script>
<style>
  .dashboard-total-item {
    display: flex;
    align-items: center;
    padding: 0 24px;
    height: 80px;
    background-color: #fff;
    font-size: 16px;
    justify-content: space-between;
  }
</style>