<script setup lang="ts">
import { ref, unref, reactive } from "vue";
import { ElMessage, ElTable } from "element-plus";
import { columns } from "../config/_details";
import { useResponseHandle } from "/@/hooks";
import { payStatusOptions } from "/@/utils/status";
import { httpList } from "/@/api/purchase/orderPay";
import { useCompany } from "/@/hooks/core/useCompany";
import { BusinessQuery, DateRange } from "/@/components/BasicForm";

const emit = defineEmits(["choose"]);

const visible = ref(false);
const paymentList = ref<Array<Record<string, string>>>([]);
const selects = ref<Array<Record<string, string>>>([]);

const pagination = reactive({
  total: 0,
  size: 15,
  page: 1,
  background: true
});

const formData = ref<Record<string, any>>({
  pay_status: "",
  currentValue: "",
  companyNo: "",
  timer: []
});

const tableRef = ref<InstanceType<typeof ElTable>>(null);
const currentKey = ref("dzNo");
const loading = ref(false);
const responseHandle = useResponseHandle();
const { currentCompany } = useCompany();

//初始化订单对账列表
async function requestPaymentList() {
  const { size, page } = pagination;
  loading.value = true;

  const { currentValue, timer, ...otherParams } = formData.value;

  const { code, message, data } = await httpList({
    status: "4",
    [currentKey.value]: currentValue,
    supplierNo: currentCompany.value.companyNo,
    startTime: timer[0],
    endTime: timer[1],
    noRela: true,
    ...otherParams,
    page,
    size
  });

  responseHandle({
    code,
    message,
    handler: () => {
      pagination.total = data.count;
      paymentList.value = data.list;
    }
  });
  loading.value = false;
}

function handleConfirm() {
  const [order] = selects.value;
  if (!order) return ElMessage.warning("选择一条采购付款");
  emit("choose", unref(order.dzNo));
  visible.value = false;
}

function handleSelectionChange(values) {
  if (values.length > 1) {
    const value = values.pop();
    tableRef.value.clearSelection();
    tableRef.value.toggleRowSelection(value, true);
    selects.value = [value];
  } else {
    selects.value = [values[0]];
  }
}

function handleSizeChange() {
  pagination.page = 1;
  requestPaymentList();
}

function handleSearch(_isReset?: boolean) {
  if (_isReset) {
    formData.value = {
      pay_status: "",
      currentValue: "",
      companyNo: "",
      timer: []
    };
  }

  pagination.page = 1;
  requestPaymentList();
}

defineExpose({
  onDisplay: () => (visible.value = true)
});
</script>

<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visible"
    title="采购付款"
    center
    width="1040px"
    @open="() => requestPaymentList()"
  >
    <el-form size="small">
      <el-row :gutter="10">
        <el-col :span="8">
          <el-form-item>
            <el-input
              clearable
              v-model="formData.currentValue"
              placeholder="对账编码"
            >
              <template #prepend>
                <el-select v-model="currentKey" style="width: 135px">
                  <el-option label="对账付款编码" value="dzNo" />
                  <el-option label="对账编码" value="payNo" />
                </el-select>
              </template>
            </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <date-range v-model="formData.timer" />
        </el-col>

        <el-col :span="8">
          <BusinessQuery
            v-model="formData.companyNo"
            placeholder="买入方公司"
          />
        </el-col>

        <el-col :span="24">
          <div w-full flex justify-end>
            <el-form-item>
              <el-button
                size="small"
                type="primary"
                @click="() => handleSearch()"
                >搜索</el-button
              >
              <el-button size="small" @click="() => handleSearch(true)"
                >重置</el-button
              >
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    </el-form>

    <el-table
      border
      ref="tableRef"
      :data="paymentList"
      size="small"
      row-key="id"
      @selection-change="handleSelectionChange"
      mb-2
      v-loading="loading"
      max-height="520px"
    >
      <el-table-column type="selection" align="center" width="40" />
      <el-table-column
        v-for="(col, index) in columns"
        :key="index"
        v-bind="col"
        show-overflow-tooltip
      >
        <template #="{ row }">
          <el-tag v-if="col.prop === 'pay_status'">{{
            payStatusOptions.find(p => p.value === row.pay_status)?.label
          }}</el-tag>

          <span v-else>{{ row[col.prop] }}</span>
        </template>
      </el-table-column>
    </el-table>

    <div flex>
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.size"
        :total="pagination.total"
        :page-sizes="[15, 50, 100]"
        layout="sizes, prev, pager, next"
        @current-change="requestPaymentList"
        @size-change="handleSizeChange"
      />

      <div w-full flex justify-end mt-2>
        <el-button type="primary" @click="handleConfirm">保存</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<style lang="scss" scoped>
:deep(.el-table__header) {
  .el-checkbox {
    display: none;
  }
}
</style>
