<template>
  <div class="page-container list-container">
    <div class="list-btns-container"></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"
          placeholder="请输入客户名称"
          clearable
          style="width: 150px"
          @keyup.enter="handleQuery"
        />
      </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
        label="客户名称"
        align="center"
        prop="companyName"
        min-width="250"
      />
      <el-table-column
        label="税号"
        align="center"
        prop="socialCreditCode"
        min-width="180"
      />
      <el-table-column
        label="合同编号"
        align="center"
        prop="contractNo"
        min-width="150"
      />
      <el-table-column
        label="签约日期"
        align="center"
        prop="formDate"
        width="85"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.formDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="签约金额"
        header-align="center"
        align="right"
        prop="trueAmount"
        min-width="90"
      >
        <template #default="scope">
          <span>{{ rowNum(scope.row.trueAmount) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="签单人"
        align="center"
        prop="signerName"
        min-width="100"
      />
      <el-table-column
        label="收款状态"
        align="center"
        prop="collectionStatus"
        min-width="140"
      >
        <template #default="scope">
          <dict-tag
            :options="contract_receipt_verify"
            :value="scope.row.collectionStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="合同来源"
        align="center"
        prop="sourceCategoryName"
        min-width="130"
      />
      <!-- <el-table-column
        label="审核状态"
        align="center"
        prop="verifyStatus"
        min-width="100"
      >
        <template #default="scope">
          <dict-tag
            :options="contract_verify_status"
            :value="scope.row.verifyStatus"
          />
        </template>
      </el-table-column> -->
      <el-table-column
        label="合同类型"
        align="center"
        width="80"
        prop="contractType"
      >
        <template #default="scope">
          {{ scope.row.contractType === 0 ? "新签" : "续签" }}
        </template>
      </el-table-column>
      <el-table-column
        label="任务类型"
        align="center"
        width="80"
        prop="serviceType"
      >
        <template #default="scope">
          {{ scope.row.serviceType === 1 ? "循环" : "代办" }}
        </template>
      </el-table-column>
      <el-table-column
        label="合同状态"
        min-width="100"
        align="center"
        prop="status"
      >
        <template #default="scope">
          <dict-tag :options="contract_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        min-width="100"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="warning"
            size="small"
            @click="handleAdd(scope.row)"
            v-hasPermi="['business:collection:add']"
            >新增收款</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <contract-form ref="contractRef" :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="WaitForCollection">
import contractForm from "./form";
import { listContract } from "@/api/business/financial/collection";
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 { contract_verify_status } = proxy.useDict("contract_verify_status");
const { contract_status } = proxy.useDict("contract_status");
const { contract_receipt_verify } = proxy.useDict("contract_receipt_verify");
console.log(22,contract_verify_status);

/** 查询对象 */
const queryParams = ref({
  pageNum: 1,
  pageSize: 20,
  nonpayment: true,
  orderByColumn: "create_time",
});

/***********************  方法区  ****************************/
onActivated(() => {
    // 你的逻辑
    getList();
  });
/** 查询company列表 */
function getList() {
  loading.value = true;
  listContract(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,
    orderByColumn: "create_time",
  };
  handleQuery();
}

// 数字格式化
function rowNum(num) {
  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 handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd(row) {
  proxy.$refs.contractRef.open(row);
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const id = row.id || ids.value;
  proxy.$refs.contractRef.open(id);
}

/** 删除按钮操作 */
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.contractRef.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() {
  exportOrder(queryParams.value);
}

function handleImport() {}

getList();
</script>