<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="建筑名称：" prop="name">
        <el-input
          v-model.trim="queryParams.name"
          placeholder="请输入建筑名称"
          clearable
          size="small"
          style="width: 160px"
          @keyup.enter.native="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-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:admin:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="adminList" height="calc(100vh - 28vh)">
      <el-table-column label="建筑名称" align="center" prop="buildName"  show-overflow-tooltip />
      <el-table-column label="地理坐标" align="center" prop="posistion"  show-overflow-tooltip />
      <el-table-column label="省" align="center" prop="data2"  show-overflow-tooltip />
      <el-table-column label="市" align="center" prop="data3"  show-overflow-tooltip />
      <el-table-column label="区/县" align="center" prop="data4"  show-overflow-tooltip />
      <el-table-column label="详细地址" align="center" prop="address"  show-overflow-tooltip />
      <el-table-column label="创建人" align="center" prop="addMan"  show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="addTime"  show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:admin:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:admin:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 修改、新增对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :before-close="cancel" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="auto" >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="建筑名称" prop="buildName">
              <el-input v-model.trim="form.buildName" placeholder="请填写建筑名称" @input="onInput()" maxlength="15" show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="建筑坐标" prop="posistion">
              <el-input v-model.trim="form.posistion" placeholder="请填写建筑坐标" @input="onInput()" maxlength="15" show-word-limit/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="省" prop="province">
              <el-select v-model="form.province" filterable size="small" @change="selectProvinceFun($event)" placeholder="请选择省份">
                <!-- <el-option value="" label="请选择省份"></el-option> -->
                <el-option :value="item" :label="item.label" v-for="(item, index) in addr1" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="市" prop="city">
              <el-select v-model="form.city" filterable size="small" @change="selectCityFun($event)" placeholder="请选择城市">
                <!-- <el-option value="" label="请选择城市"></el-option> -->
                <el-option :value="item" :label="item.label" v-for="(item, index) in addr2" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="区/县" prop="area">
              <el-select v-model="form.area" filterable size="small" @change="selectAreaFun($event)" placeholder="请选择区县">
                <!-- <el-option value="" label="请选择区县"></el-option> -->
                <el-option :value="item" :label="item.label" v-for="(item, index) in addr3" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="详细地址" prop="address">
              <el-input v-model.trim="form.address" placeholder="请填写详细地址" @input="onInput()" maxlength="28" show-word-limit/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { addBuilding, delBuilding, updateBuilding, getBuilding } from "@/api/business/buildMange";
import city from '@/assets/json/city.json'
export default {
  name: "Building",
  data() {
    return {
      exportLoading:false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      adminList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 20,
        name: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        buildName: [
          { required: true, message: "建筑名称不能为空", trigger: "blur" }
        ],
        posistion: [
          { required: true, message: "地理坐标不能为空", trigger: "blur" }
        ],
        province: [
          { required: true, message: "省不能为空", trigger: ["blur",'change'] }
        ],
        city: [
          { required: true, message: "市不能为空", trigger: ["blur",'change']}
        ],
        area: [
          { required: true, message: "区/县不能为空", trigger: ["blur",'change'] }
        ],
        address: [
          { required: true, message: "详细地址不能为空", trigger: "blur" }
        ],
      },
      // 整个省市县数据
      addr1: city,
      // 被选中的市数据
      addr2: [],
      // 被选中的县数据
      addr3: [],
      selectProvince: {},
      selectCity: {},
      selectArea: {}
    };
  },
  created() {
    this.getList();//初始化table
    // this.company();//初始化单位选项
    // this.facilityType();//初始化类别选项
  },
  methods: {
    //强制el-input刷新
    onInput(){
        this.$forceUpdate();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams = {
        name:undefined,
        current:1,
        size:20
      }
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      getBuilding(this.queryParams).then(response => {
        this.adminList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.title = "修改";
      this.form = JSON.parse(JSON.stringify(row))
      for(let i = 0;i<city.length;i++){ //省市区赋值
        if(row.addr1 == city[i].value){
          this.form.province = {
            value:city[i].value,
            label:city[i].label
          }
          let cityData = city[i].children
          this.selectProvinceFun(city[i])
          for(let a = 0; a<cityData.length;a++){
            if(row.addr2 == cityData[a].value){
              this.form.city = {
                value:cityData[a].value,
                label:cityData[a].label
              }
              let areaData = cityData[a].children
              this.selectCityFun(cityData[a])
              for(let b = 0; b<areaData.length;b++){
                if(row.addr3 == areaData[b].value){
                  this.form.area = {
                    value:areaData[b].value,
                    label:areaData[b].label
                  }
                }
              }
            }
          }
        }
      }
      this.open = true;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    /** 提交按钮 */
    submitForm(row) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.form)
          this.loading = true
          this.form.addr1 = this.form.province.value
          this.form.addr2 = this.form.city.value
          this.form.addr3 = this.form.area.value

          this.form.data2 = this.form.province.label
          this.form.data3 = this.form.city.label
          this.form.data4 = this.form.area.label

          let data = this.form

          let provinceX = data.province
          let cityX = data.city
          let areaX = data.area
          delete data.province
          delete data.city
          delete data.area
          if (this.form.id != undefined) {
            updateBuilding(this.form).then(response => {
              this.loading = false,
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch(()=>{
              this.loading = false
              data.province = provinceX
              data.city = cityX
              data.area = provinceX
            })
          } else {
            addBuilding(data).then(response => {
              this.loading = false,
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).catch(()=>{
              this.loading = false
              data.province = provinceX
              data.city = cityX
              data.area = provinceX
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBuilding(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },

    // 省份 市 县联动
    selectProvinceFun (event) {
      console.log(event)
      if (event) {
        this.addr2 = event.children
        this.addr3 = []
      }

      // this.$emit('getLawyerListInfo', [event.label, 'province'])
      // this.$emit('getLawyerListInfo', [event.label, 'area'])
    },
    selectCityFun (event) {
      if (event) {
        this.addr3 = event.children
      }
      // this.$emit('getLawyerListInfo', [event.label, 'city'])
    },
    selectAreaFun (event) {
      // this.$emit('getLawyerListInfo', [event.label, 'area'])
    }
  }
};
</script>
