<template>
  <div class="page-container list-container">
    <!-- 功能按钮区 -->
    <div class="list-btns-container">
      <el-button type="primary" size="small" icon="Plus" @click="handleAdd"
        v-hasPermi="['business:payment:add']">新增</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:payment:export']">
              导出</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
    <!-- 搜索区 -->
    <el-form class="list-search-container" size="small" :model="queryParams" ref="queryRef" :inline="true"
      label-width="68px">
      <el-form-item label="客户名称：" prop="companyName">
        <el-input v-model="queryParams.companyName" style="width: 150px" placeholder="请输入客户名称" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>

      <el-form-item label="合同编号：" prop="contractNo">
        <el-input v-model="queryParams.contractNo" style="width: 150px" placeholder="请输入合同编号" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>

      <el-form-item prop="applierTime" label="审核时间：">
        <el-date-picker v-model="queryParams.dateFilter" type="daterange" clearable format="YYYY - MM - DD "
          value-format="YYYY-MM-DD" range-separator="至" start-placeholder="业务日期" end-placeholder="业务日期"
          style="width: 100%;"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表区 -->
    <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 type="index" label="序号" width="50" align="center" /> -->
      <el-table-column v-if="getShowStatus(2)" label="客户名称" prop="companyName" min-width="250" align="center" />
      <el-table-column v-if="getShowStatus(16)" label="服务公司" prop="serviceCompanyName" width="200" align="center" />
      <el-table-column v-if="getShowStatus(3)" label="合同号" prop="formNo" min-width="200" align="center" />
      <el-table-column v-if="getShowStatus(4)" label="申请日期" prop="formDate" min-width="90" align="center">
        <template #default="scope">
          {{
          scope.row.formDate
          ? moment(scope.row.formDate).format("YYYY-MM-DD")
          : ""
          }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="服务公司" prop="subsidiary" width="150" align="center" /> -->
      <el-table-column v-if="getShowStatus(5)" label="支出事由" prop="paymentCause" min-width="90" align="center" />
      <el-table-column v-if="getShowStatus(6)" label="支出科目" prop="paymentSubject" min-width="90" align="center"
        show-overflow-tooltip />
      <el-table-column v-if="getShowStatus(7)" label="支出金额" prop="sumAmount" width="80" header-align="center"
        align="right">
        <template #default="scope">
          <span>{{ rowNum(scope.row.sumAmount) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核日期" prop="verifyDate" width="150" align="center" />
      <el-table-column v-if="getShowStatus(8)" label="审核状态" prop="status" min-width="120" align="center">
        <template #header>
          <div>审核状态</div>
        </template>
        <template #default="scope">
          <span v-if="scope.row.verifyStatus === 0" style="color: #cd853f">未审核</span>
          <span v-else-if="scope.row.verifyStatus === 1" style="color: #32cd32">财务审核通过</span>
          <span v-else-if="scope.row.verifyStatus === 2" style="color: #dc143c">财务审核不通过</span>
          <span v-else-if="scope.row.verifyStatus === 3" style="color: #32cd32">出纳审核通过</span>
          <span v-else-if="scope.row.verifyStatus === 4" style="color: #dc143c">出纳审核不通过</span>
        </template>
      </el-table-column>
      <el-table-column v-if="getShowStatus(9)" label="支付状态" min-width="100" prop="arriveStatus" align="center">
        <template #default="scope">
          <span v-if="scope.row.paymentStatus === 0" style="color: #dc143c">未支付</span>
          <span v-else-if="scope.row.paymentStatus === 1" style="color: #32cd32">已支付</span>
          <span v-else-if="scope.row.paymentStatus === 2" style="color: #cd853f">部分支付</span>
        </template>
      </el-table-column>
      <el-table-column v-if="getShowStatus(10)" label="收款账户名" prop="acceptAccount" min-width="100" align="center" />
      <el-table-column v-if="getShowStatus(11)" label="收款账号" prop="acceptAccountNo" width="90" align="center" />

      <el-table-column v-if="getShowStatus(13)" label="申请人" prop="applierName" min-width="130" align="center" />
      <el-table-column v-if="getShowStatus(14)" label="备注" prop="remark" align="center" />
      <el-table-column label="操作" fixed="right" width="60" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleFormPageOpen(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <payment-form ref="paymentRef" :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="Payment">
  import paymentForm from "./form";
  import { listPayment, exportPayment } from "@/api/business/financial/payment";
  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 showRows = ref([2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13]);
  const rows = ref([
    {
      key: 2,
      label: "客户名称",
    },
    {
      key: 3,
      label: "合同编号",
    },
    {
      key: 4,
      label: "收款日期",
    },
    {
      key: 5,
      label: "收款时间",
    },
    {
      key: 9,
      label: "申请人",
    },
    {
      key: 11,
      label: "审核状态",
    },
    {
      key: 12,
      label: "支付状态",
    },
    {
      key: 14,
      label: "确认到账时间",
    },
    {
      key: 13,
      label: "备注",
    },
  ]);

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

  /***********************  方法区  ****************************/
  onActivated(() => {
    // 你的逻辑
    getList();
  });
  /** 查询company列表 */
  function getList() {
    loading.value = true;
    listPayment(queryParams.value).then((response) => {
      orderList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }

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

  /** 重置按钮操作 */
  function resetQuery() {
    // proxy.resetForm("queryRef");
    queryParams.value = {
      pageNum: 1,
      pageSize: 20,
      nonpayment: true,
    };
    handleQuery();
  }

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

  /** 新增按钮操作 */
  function handleView(id) {
    proxy.$refs.paymentRef.open(id);
  }

  // 数字格式化
  function rowNum(num) {
    if (!num) {
      return;
    }
    num = num.toLocaleString(); // 3,000
    if (num.indexOf(".") == -1) {
      num = num + ".00"; //3,000.00
    } else if (num.charAt(num.indexOf(".") == num.length - 2)) {
      num = num + "0";
    }
    return num;
  }

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

  function handleAlter(row) {
    const id = row.id || ids.value;
    proxy.$refs.paymentRef.openSimple(id, "alterOrder");
  }

  function handleDissolution() {
    const row = orderList.value.find((item) => item.id === ids.value[0]);
    proxy.$modal
      .confirm("是否确认解除合同？")
      .then(function () {
        return dissolutionOrder(row);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功！");
      })
      .catch(() => { });
  }

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

  function handleImport() { }

  function getShowStatus(key) {
    const index = showRows.value.findIndex((l) => l === key);
    return index >= 0;
  }

  function handleAdd() {
    proxy.$refs.paymentRef.open();
  }

  function handleFormPageOpen(id) {
    proxy.$refs.paymentRef.open(id);
  }

  getList();
</script>