<script setup lang="ts">
import { reactive, ref, unref } from "vue";
import { httpOrderList } from "/@/api/InvoiceSales/invoiceApply";
import { responseHandle } from "/@/utils/responseHandle";
import { useNav } from "/@/layout/hooks/nav";
import { useColumns } from "./columns";
import { type PaginationProps } from "@pureadmin/table";
import { PageSearch, usePageSearch } from "/@/components/PageSearch";
import searchFormConfig from "./../../config/search.config";
import { ElMessage } from "element-plus";

const { columns } = useColumns();
const { logout } = useNav();
const showModel = ref(false);
const loading = ref(false);
const dataList = ref([]);
const tableRef = ref();
const handleSelection = ref([]);

const pagination = reactive<PaginationProps>({
  total: 0,
  pageSize: 15,
  currentPage: 1,
  background: true
});
const emit = defineEmits(["reload", "save-btn-click"]);
let otherParams: Record<any, any> = {};
const initform = {
  page: 1,
  size: 10,
  start: "",
  end: "",
  total_min: "",
  total_max: "",
  inv_status: "1", //部分开/未开
  pay_status: "",
  sequenceNo: "",
  customerNo: "",
  customer: "",
  platName: "",
  status: "0"
};
const ruleForm = ref({ ...initform });

const { handleSearchClick, handleResetClick } = usePageSearch(
  params => {
    otherParams = params;
    onSearch();

    return {
      result: {},
      deleteProps: []
    };
  },
  () => {
    otherParams = {};
    console.log(123);
    onSearch();
  }
);

const handleCurrentChange = (current: number) => {
  ruleForm.value.page = current;
  onSearch();
};

async function show() {
  showModel.value = true;
  await onSearch();
}

function handleSelectionChange(val) {
  handleSelection.value = val;
}

//列表展示
async function onSearch() {
  loading.value = true;
  const { code, data, message } = await httpOrderList({
    cat_status: "1",
    ...ruleForm.value,
    ...otherParams
  });

  responseHandle({
    code,
    message,
    logout,
    handler: () => {
      const { list, count } = data ?? {};
      dataList.value = list ?? [];
      pagination.total = count ?? 0;
      pagination.pageSize = ruleForm.value.size;
      pagination.currentPage = ruleForm.value.page;
    }
  });
  loading.value = false;
}

function handleConfirm() {
  const selectList = unref(handleSelection);
  if (selectList.length === 0) return ElMessage.error("至少选择一个销售订单！");

  //商品数量或开票金额为空
  const noConfirm = selectList.filter(
    o => o.goodNum === "0" || o.winv_fee === "0.00"
  );

  if (noConfirm.length !== 0) {
    return ElMessage.error(
      `销售单 ${noConfirm[0].SequenceNo} 开票金额或数量为空`
    );
  }

  emit("save-btn-click", selectList);
  showModel.value = false;
}

defineExpose({
  show
});
</script>

<template>
  <div>
    <el-dialog
      v-model="showModel"
      :close-on-press-escape="false"
      center
      append-to-body
      destroy-on-close
      top="5vh"
      title="添加销售订单"
      :width="'1040px'"
    >
      <PageSearch
        @reset-btn-click="handleResetClick"
        @search-btn-click="handleSearchClick"
        :form-config="searchFormConfig"
      />
      <div v-loading="loading">
        <PureTable
          ref="tableRef"
          border
          align="left"
          row-key="id"
          table-layout="auto"
          :size="'small'"
          :data="dataList"
          :columns="columns"
          :pagination="pagination"
          :paginationSmall="true"
          :header-cell-style="{ background: '#fafafa', color: '#606266' }"
          @selection-change="handleSelectionChange"
          @current-change="handleCurrentChange"
        />
      </div>

      <el-button type="primary" @click="handleConfirm">保存</el-button>
    </el-dialog>
  </div>
</template>
