<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="100px">
            <el-form-item label="供应商编号" prop="supplierNo">
              <el-input v-model="queryParams.supplierNo" placeholder="请输入供应商编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>

            <el-form-item label="供应商名称" prop="enterpriseName">
              <el-input v-model="queryParams.enterpriseName" placeholder="请输入供应商名称" clearable @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-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          供应商列表
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="supplierList">
        <el-table-column label="主键ID" align="center" prop="id" v-if="false" />
        <el-table-column label="供应商编号" align="center" prop="supplierNo" />
        <el-table-column label="供应商名称" align="left" prop="enterpriseName" min-width="220" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['supplier:contractsupply:edit']">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="Contractsupply" lang="ts">
import { getApproveList } from '@/api/customer/info';
import { InfoVO, InfoQuery } from '@/api/customer/info/types';
import { useRouter } from 'vue-router';

const router = useRouter();

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const supplierList = ref<InfoVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const data = reactive<PageData<Record<string, any>, InfoQuery>>({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    supplierNo: undefined,
    enterpriseName: undefined,
    params: {
    }
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询供应商列表 */
const getList = async () => {
  loading.value = true;
  const res = await getApproveList(queryParams.value);
  supplierList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 新增按钮操作 */
const handleAdd = () => {
  router.push('/supplier/contractsupply/add');
}

/** 修改按钮操作 - 跳转合约供货编辑页，传入供应商ID */
const handleUpdate = async (row?: InfoVO) => {
  const _id = row?.id;
  if (!_id) {
    proxy?.$modal.msgWarning('请选择要编辑的数据');
    return;
  }
  router.push(`/supplier/contractsupply/edit/${_id}`);
}

onActivated(() => {
  getList();
});
</script>

<style scoped>

</style>
