<template>
    <div class="app-container">
        <div class="background-color-fff padding-20 border-radius-8px margin-bottom-20">
            <el-form
                :model="queryParams"
                ref="queryRef"
                :inline="true"
                v-show="showSearch"
                label-width="68px"
            >
                <el-form-item label="区域代码" prop="code">
                    <el-input
                        v-model="queryParams.code"
                        placeholder="请输入区域代码"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="名称" prop="name">
                    <el-input
                        v-model="queryParams.name"
                        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>
        </div>

        <div class="background-color-fff padding-20 border-radius-8px margin-bottom-20">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button
                        type="primary"
                        plain
                        icon="Plus"
                        @click="handleAdd"
                        v-hasPermi="['third:district:add']"
                    >
                        新增
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="success"
                        plain
                        icon="Edit"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['third:district:edit']"
                    >
                        修改
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['third:district:export']"
                    >
                        导出
                    </el-button>
                </el-col>
                <right-toolbar
                    v-model:showSearch="showSearch"
                    @queryTable="getList"
                ></right-toolbar>
            </el-row>
        </div>

        <div class="background-color-fff padding-20 border-radius-8px margin-bottom-20">
            <el-table
                v-loading="loading"
                :data="districtList"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="区域代码" align="center" prop="code" />
                <el-table-column label="名称" align="center" prop="name" />
                <el-table-column label="父类区域代码" align="center" prop="parentCode" />
                <el-table-column label="级别" align="center" prop="level" />
                <el-table-column label="纬度" align="center" prop="lat" />
                <el-table-column label="经度" align="center" prop="lng" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <el-button
                            link
                            type="primary"
                            icon="Edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['third:district: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"
            />
        </div>

        <!-- 添加或修改全国省市区/县/辖区/街道对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="districtRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="区域代码" prop="code">
                    <el-input v-model="form.code" placeholder="请输入区域代码" />
                </el-form-item>
                <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="父类区域代码" prop="parentCode">
                    <el-input v-model="form.parentCode" placeholder="请输入父类区域代码" />
                </el-form-item>
                <el-form-item label="级别" prop="level">
                    <el-input v-model="form.level" placeholder="请输入级别1省、2市、3县区、4街道" />
                </el-form-item>
                <el-form-item label="纬度" prop="lat">
                    <el-input v-model="form.lat" placeholder="请输入纬度" />
                </el-form-item>
                <el-form-item label="经度" prop="lng">
                    <el-input v-model="form.lng" placeholder="请输入经度" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="District">
import {
    listDistrict,
    getDistrict,
    delDistrict,
    addDistrict,
    updateDistrict,
} from '@/api/third/district';

const { proxy } = getCurrentInstance();

const districtList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref('');

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        parentCode: null,
        level: null,
        lat: null,
        lng: null,
    },
    rules: {},
});

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

/** 查询全国省市区/县/辖区/街道列表 */
function getList() {
    loading.value = true;
    listDistrict(queryParams.value).then(response => {
        districtList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        code: null,
        name: null,
        parentCode: null,
        level: null,
        lat: null,
        lng: null,
    };
    proxy.resetForm('districtRef');
}

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

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm('queryRef');
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.code);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = '添加全国省市区/县/辖区/街道';
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const _code = row.code || ids.value;
    getDistrict(_code).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = '修改全国省市区/县/辖区/街道';
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs['districtRef'].validate(valid => {
        if (valid) {
            if (form.value.code != null) {
                updateDistrict(form.value).then(response => {
                    proxy.$modal.msgSuccess('修改成功');
                    open.value = false;
                    getList();
                });
            } else {
                addDistrict(form.value).then(response => {
                    proxy.$modal.msgSuccess('新增成功');
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _codes = row.code || ids.value;
    proxy.$modal
        .confirm('是否确认删除全国省市区/县/辖区/街道编号为"' + _codes + '"的数据项？')
        .then(function () {
            return delDistrict(_codes);
        })
        .then(() => {
            getList();
            proxy.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download(
        'third/district/export',
        {
            ...queryParams.value,
        },
        `district_${new Date().getTime()}.xlsx`
    );
}

getList();
</script>
