<template>
  <el-container>
    <el-header class="gva-search-box">
      <div style="line-height: 60px">
        <el-button
          type="success"
          @click="regionAddDialog = true"
        >添加</el-button>
      </div>
    </el-header>
    <el-main class="gva-table-box">
      <el-table
        :data="filterRegion"
        style="min-height: 700px"
      >
        <el-table-column
          prop="name"
          label="名称"
        />
        <el-table-column
          prop="remarks"
          label="备注"
        />
        <el-table-column
          label="操作"
          width="280"
          fixed="right"
        >
          <template #header>
            <el-input
              v-model="search"
              placeholder="搜索"
            />
          </template>
          <template #default="scope">
            <el-button
              type="primary"
              @click="regionEditDialog = true; regionData = {...scope.row}"
            >
              编辑
            </el-button>
            <el-button
              type="danger"
              @click="regionDelete(scope.row.ID)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <!--    地区新增-->
    <el-dialog
      v-model="regionAddDialog"
      title="地区新增"
      width="300"
      align-center
    >
      <el-form v-model="regionData">
        <el-form-item
          label="名称"
          prop="name"
        >
          <el-input v-model="regionData.name" />
        </el-form-item>
        <el-form-item
          label="备注"
          prop="remarks"
        >
          <el-input v-model="regionData.remarks" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="regionAddDialog = false">取消</el-button>
          <el-button
            type="primary"
            @click="regionAdd"
          >
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
    <!--    地区修改-->
    <el-dialog
      v-model="regionEditDialog"
      title="地区新增"
      width="300"
      align-center
    >
      <el-form v-model="regionData">
        <el-form-item
          label="名称"
          prop="name"
        >
          <el-input v-model="regionData.name" />
        </el-form-item>
        <el-form-item
          label="备注"
          prop="remarks"
        >
          <el-input v-model="regionData.remarks" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="regionEditDialog = false">取消</el-button>
          <el-button
            type="primary"
            @click="regionEdit"
          >
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </el-container>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { createRegion, deleteRegion, queryAllRegions, updateRegion } from '@/api/region'

const region = ref()

const getData = async() => {
  await queryAllRegions().then(res => {
    region.value = res.data
  })
}

const search = ref('')
const filterRegion = computed(() => {
  // 确保 region.value 存在并且是一个数组
  const regionData = region.value || []

  return regionData.filter(data =>
    !search.value ||
      data.name.toLowerCase().includes(search.value.toLowerCase())
  )
})

// 初始数据
const regionData = ref({
  id: 0,
  name: '',
  remarks: ''
})

// 新增
const regionAddDialog = ref(false)

const regionAdd = async() => {
  await createRegion(regionData.value).then(res => {
    regionAddDialog.value = false
    if (res.code === 0) {
      ElMessage.success('添加成功')
    }
    getData()
  })
}

// 修改
const regionEditDialog = ref(false)

const regionEdit = async() => {
  await updateRegion(regionData.value).then(res => {
    regionEditDialog.value = false
    if (res.code === 0) {
      ElMessage.success('修改成功')
    }
    getData()
  })
}

// 删除

const regionDelete = (val) => {
  ElMessageBox.confirm(
    '将永久删除改数据，请问继续吗?',
    '删除',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async() => {
      await deleteRegion(val).then(res => {
        if (res.code === 0) {
          ElMessage.success('删除成功')
        }
        getData()
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '删除已取消',
      })
    })
}

onMounted(() => {
  getData()
})
</script>
