<script setup lang="ts">
import { reactive, ref, unref } from "vue";
import { ElTable } from "element-plus";
import { useResponseHandle } from "/@/hooks";
import { httpList } from "/@/api/InvoiceSales/invoiceApply";
import { inv_columns } from "../../config/configs";
import { useCompany } from "/@/hooks/core/useCompany";

const paymentList = ref<Array<Record<string, string>>>([]);
const selectOrder = ref<Array<Record<string, string>>>([]);

const params = ref({
  invNo: "",
  inv_out: "",
  inv_company: ""
});

const loading = ref(false);
const visible = ref(false);

const emit = defineEmits(["on-add-order"]);
const tableRef = ref<InstanceType<typeof ElTable>>(null);
const responseHandle = useResponseHandle();
const { currentCompany } = useCompany();

const pagination = reactive({
  pageSize: 10,
  currentPage: 1,
  total: 0
});

async function requestPaymentList() {
  const { pageSize: size, currentPage: page } = pagination;
  loading.value = true;

  console.log(currentCompany.value);

  const { code, message, data } = await httpList({
    status: "3",
    inv_out: currentCompany.value.companyNo,
    size,
    page,
    ...params.value
  });

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

  loading.value = false;
}

function handleConfirm() {
  visible.value = false;
  emit("on-add-order", unref(selectOrder.value[0]));
}

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

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

function onSearch(_isReset = false) {
  if (_isReset) {
    params.value = {
      invNo: "",
      inv_out: "",
      inv_company: ""
    };
  }

  pagination.currentPage = 1;
  requestPaymentList();
}

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

<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    v-model="visible"
    title="发票"
    center
    width="1040px"
    @open="() => requestPaymentList()"
  >
    <div flex mb-2>
      <div flex flex-1 gap="5" pr="20px">
        <el-input
          placeholder="发票申请编码"
          size="small"
          v-model="params.invNo"
        />
        <el-input
          placeholder="业务公司编码"
          size="small"
          v-model="params.inv_out"
        />

        <el-input
          placeholder="业务公司名称"
          size="small"
          v-model="params.inv_company"
        />
      </div>

      <div flex width="120px">
        <el-button size="small" type="primary" @click="() => onSearch()"
          >搜索</el-button
        >
        <el-button size="small" @click="() => onSearch(true)">重置</el-button>
      </div>
    </div>

    <el-table
      v-loading="loading"
      border
      ref="tableRef"
      :data="paymentList"
      size="small"
      row-key="id"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" width="40"  fixed="left" />
      <el-table-column
        v-for="(col, index) in inv_columns"
        :key="index"
        v-bind="col"
        show-overflow-tooltip
      />
    </el-table>

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

      <el-button type="primary" size="small" @click="handleConfirm"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>

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