<template>
  <div class="h-full flex flex-col bg-white p-2">
    <p class="font-bold text-lg leading-10 border-b-2">工厂客户</p>
    <el-form class="pt-2" :model="formState" inline>
      <el-form-item label="客户名称">
        <el-input v-model="formState.customerName"></el-input>
      </el-form-item>
      <el-form-item label="客户简称">
        <el-input v-model="formState.customerNickName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetForm">重置</el-button>
        <el-button @click="queryCustomer" type="primary">查询</el-button>
      </el-form-item>
    </el-form>

    <vxe-table
      ref="xTable"
      min-height="460"
      border
      :data="tableData"
      :loading="loading"
      size="small"
    >
      <vxe-column field="customerName" title="客户名称"></vxe-column>
      <vxe-column field="customerNickName" title="客户简称"></vxe-column>
      <vxe-column field="action" title="操作" width="100">
        <template #default="{ row }">
          <el-button type="primary" text size="small" @click="toEmail(row)">查看邮箱</el-button>
        </template>
      </vxe-column>
    </vxe-table>

    <vxe-pager
      size="small"
      background
      v-model:current-page="formState.currentPage"
      v-model:page-size="formState.pageSize"
      :total="formState.total"
      @page-change="queryCustomer"
      :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'Sizes', 'FullJump', 'Total']"
    >
    </vxe-pager>
  </div>
</template>

<script lang="ts" setup>
import { defineComponent, ref, onMounted, reactive, watch, unref } from 'vue'
import { getCustomerPage } from '@/api/customer'
import type { VxeTableInstance } from 'vxe-table'
import { useRouter } from 'vue-router'
import { queryParamsFilter } from '@/utils/query';

const loading = ref(false)
const router = useRouter()

const xTable = ref<VxeTableInstance>()
const tableData = ref([])
const formState = reactive({
  customerName: '',
  customerNickName: '',
  currentPage: 1,
  pageSize: 10,
  total: 0
})

const toEmail = (row: any) => {
  router.push({
    name: 'OrderCustomerEmail',
    query: {
      customerId: row.customerId,
      customerName: row.customerName
    }
  })
}

const queryCustomer = async () => {
  loading.value = true
  try {
    // 模糊查询
    
    const { data } = await getCustomerPage(queryParamsFilter(unref(formState)))
    tableData.value = data.result.records
    formState.total = +data.result.total
  } catch {}
  loading.value = false
}
const resetForm = () => {
  Object.assign(formState, {
    customerName: '',
    customerNickName: ''
  })
}

onMounted(() => {
  queryCustomer()
})
</script>
<style lang="scss" scoped></style>
