<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="城市名称" prop="cityName">
        <el-input
          v-model="queryParams.cityName"
          placeholder="请输入城市名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="cityList" @selection-change="handleSelectionChange" border stripe>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center" />
      <el-table-column prop="cityCode" label="城市编码" align="center" width="150" />
      <el-table-column prop="cityName" label="城市名称" align="center" width="200" />
      <el-table-column prop="provinceName" label="所属省份" align="center" width="150" />
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="城市编码" prop="cityCode">
          <el-input v-model="form.cityCode" placeholder="请输入城市编码" />
        </el-form-item>
        <el-form-item label="城市名称" prop="cityName">
          <el-input v-model="form.cityName" placeholder="请输入城市名称" />
        </el-form-item>
        <el-form-item label="所属省份" prop="provinceName">
          <el-input v-model="form.provinceName" placeholder="请输入所属省份" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCity, getCity, delCity, addCity, updateCity } from "@/api/system/city";
import Pagination from "@/components/Pagination";

export default {
  name: "City",
  components: { Pagination },
  data() {
    return {
      loading: false,
      cityList: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cityName: null
      },
      ids: [],
      multiple: true,
      title: "",
      open: false,
      form: {},
      rules: {
        cityCode: [{ required: true, message: "城市编码不能为空", trigger: "blur" }],
        cityName: [{ required: true, message: "城市名称不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listCity(this.queryParams).then(response => {
        this.cityList = response.rows || [];
        this.total = response.total || 0;
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = { pageNum: 1, pageSize: 10, cityName: null };
      this.getList();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cityId);
      this.multiple = !selection.length;
    },
    handleAdd() {
      this.form = {};
      this.open = true;
      this.title = "添加城市";
    },
    handleUpdate(row) {
      getCity(row.cityId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改城市";
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cityId) {
            updateCity(this.form).then(() => {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCity(this.form).then(() => {
              this.$message.success("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDelete(row) {
      const cityIds = row.cityId || this.ids.join(",");
      this.$confirm("确认删除选中的城市吗？", "提示", { type: "warning" }).then(() => {
        delCity(cityIds).then(() => {
          this.$message.success("删除成功");
          this.getList();
        });
      });
    },
    cancel() {
      this.open = false;
    }
  }
};
</script>