
<template>
  <div class="cityList app-container">
     <div class="search">
      <div>

        <el-input
          v-model="query.name"
          size="small"
          placeholder="请输入城市名称"
          clearable
          class="item-width-200 "
        ></el-input>
        <el-button
        class="ml10"
        type="primary"
        size="small"
        icon="el-icon-search"
        @click="handleSearch"
      >
        搜索
      </el-button>
      </div>
       <el-button class="add-btn" type="primary" size="small" @click="handleAdd"
        >添加</el-button
      >
    </div>

    <el-table
      :data="tableData"
      tooltip-effect="dark"
      border
      v-loading="loading"
      style="width: 100%"
    >
     <el-table-column
        align="center"
        label="行政区域名称"
        prop="name"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="区域编码"
        prop="code"
        show-overflow-tooltip
      >
      </el-table-column>
       <el-table-column
        align="center"
        label="创建时间"
        prop="createTime"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button type="text" size="small" @click="handleDel(scope.row.code)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="page-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="query.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="query.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog :title="title" width="550px" :visible.sync="dialogFormVisible">
    <el-form label-width="130px" :model="setQuery"
      ref="setQuery"
      :rules="rules">
        <el-form-item label="行政编码"  prop="code">
            <el-input
            v-model="setQuery.code"
            class="item-width-300"
            ></el-input>
        </el-form-item>
         <el-form-item label="城市名称"  prop="name">
            <el-input
            v-model.number="setQuery.name"
            class="item-width-300"
            ></el-input>
        </el-form-item>

    </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" :loading="btnLoading" @click="save">确 定</el-button>
  </div>
</el-dialog>


  </div>
</template>
  
  <script>
import { debounce } from "@/utils/index";
import { getCityList,putCity,delCity } from "@/api/common";
export default {
  name: "marketing",
  data() {
    return {
      date:'',
      title:'',
      tableData: [],
      loading: false,
      btnLoading: false,
      dialogFormVisible: false,
      query: {
        name:'',
        page: 1,
        size: 10,
      },
      total: 0,
      setQuery:{
        "code": "",
        "name": "",
        "type": 'City',
      },
      rules:{
         code: [{ required: true, message: "请输入行政编码", trigger: "blur" }],
         name: [{ required: true, message: "请输入城市名", trigger: "blur" }],
      }
    };
  },
  methods: {
    handleSearch(){
        this.query.page = 1;
        this.getList();
    },
    handleEdit(row){
      this.title = '编辑'
      this.dialogFormVisible = true
      this.setQuery = {
        "code": row.code,
        "name": row.name,
        "type": row.type,
      }
      this.date = [row.startTime, row.endTime]
    },
    handleAdd() {
      this.title = '添加'
      this.dialogFormVisible = true
      this.setQuery = {
        "code": "",
        "name": "",
        "type": 'City',
      }
    },
    // 删除
     handleDel(code) {
       this.$confirm(`此操作将删除该数据, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delCity({code}).then(res=>{
            if(res.state == 'Success'){
               this.$notify({
                title: '成功',
                message: '操作成功',
                type: 'success'
              });
              this.getList()
            }
          })
        })
    },
    handleSizeChange(val) {
      this.query.page = 1;
      this.query.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.query.page = val;
      this.getList();
    },

    search() {
      this.query.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true
      let query = JSON.parse(JSON.stringify(this.query));
      query.page-=1
      getCityList(query).then((res) => {
        this.loading = false
        if (res.state == "Success") {
          this.tableData = res.content.content;
          this.total = res.content.totalElements
        }
      });
    },
    save(){
        this.$refs.setQuery.validate((v) => {
        if (v) {
          this.btnLoading = true
          putCity([this.setQuery]).then(res=>{
            this.btnLoading = false
            if(res.state == 'Success'){
                this.dialogFormVisible = false
              this.$notify({
                  title: '成功',
                  message: '操作成功',
                  type: 'success'
                });
                this.query.page = 1
                this.getList();

            }
          })
              
        }
      });
    }
  },
  created() {
    this.getList();
  },
};
</script>
  
<style lang="scss" >
.cityList {
}
</style>
  