<template>
  <div class="express-page">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>快递公司管理</span>
        </div>
      </template>

      <div class="search-form">
        <el-form :inline="true" :model="searchForm">
          <el-form-item label="快递公司">
            <el-input v-model="searchForm.name" placeholder="请输入快递公司名称" clearable style="width: 200px" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">搜索</el-button>
            <el-button @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="code" label="快递代码" width="120" />
        <el-table-column prop="name" label="快递公司名称" />
        <el-table-column label="状态" width="100">
          <template #default="scope">
            <el-tag type="success">启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleEdit(scope.row)"> 编辑 </el-button>
            <el-button type="danger" size="small" @click="handleDelete(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination v-model:current-page="pagination.page" v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]" :total="pagination.total" layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import useExpress from './express.js';

const { expressOptions, deliverCompany } = useExpress();

const searchForm = reactive({
  name: '',
});

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

const tableData = ref([]);

// 初始化数据
const initData = () => {
  tableData.value = expressOptions.value;
  pagination.total = expressOptions.value.length;
};

// 搜索
const handleSearch = () => {
  if (searchForm.name) {
    tableData.value = expressOptions.value.filter(
      (item) => item.name.includes(searchForm.name) || item.code.includes(searchForm.name),
    );
  } else {
    tableData.value = expressOptions.value;
  }
  pagination.total = tableData.value.length;
};

// 重置
const handleReset = () => {
  searchForm.name = '';
  initData();
};

// 编辑
const handleEdit = (row) => {
  ElMessage.info(t('message.editFeatureNotDeveloped'));
};

// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除这个快递公司吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    ElMessage.success(t('message.deleteSuccess'));
  });
};

// 分页大小改变
const handleSizeChange = (val) => {
  pagination.pageSize = val;
  handleCurrentChange(1);
};

// 当前页改变
const handleCurrentChange = (val) => {
  pagination.page = val;
};

onMounted(() => {
  initData();
});
</script>

<style scoped>
.express-page {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>
