<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小区名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入小区名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="简称" prop="sname">
        <el-input
          v-model="queryParams.sname"
          placeholder="请输入小区（村)常用名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="区域类型" prop="areaType">
        <el-select v-model="queryParams.areaType" placeholder="请选择区域类型" clearable size="small" style="width: 200px;">
          <el-option label="城镇" value="1" />
          <el-option label="农村" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入详细地址"
          clearable
          size="small"
          @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-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"
          v-hasPermi="['system:community:add']"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:community:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:community:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:community:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="communityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="小区名称" align="center" prop="name" />
      <el-table-column label="简称(常用名)" align="center" prop="sname" >
        <template slot-scope="scope">
          <span v-if="scope.row.sname">{{scope.row.sname}}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="区域类型" align="center" prop="areatypeName" :show-overflow-tooltip="true"/>
      <el-table-column label="所属区域" align="center" prop="cityName" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ scope.row.cityName }}
          <span v-if="scope.row.countyName">-{{ scope.row.countyName }}</span>
          <span v-else></span>
          <span v-if="scope.row.streetName">-{{ scope.row.streetName }}</span>
          <span v-else></span>
        </template>
      </el-table-column>
     <!-- <el-table-column label="所属区(县)" align="center" prop="countyName" width="130px"/>
      <el-table-column label="所属街道" align="center" prop="streetName" />-->
      <el-table-column label="详细地址" align="center" prop="address" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.address">{{scope.row.address}}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="居民总户数" align="center" prop="usersNum" width="120px">
        <template slot-scope="scope">
          <span v-if="scope.row.usersNum">{{scope.row.usersNum}}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" >
        <template slot-scope="scope">
          <span v-if="scope.row.remark">{{scope.row.remark}}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <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-document"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:community:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:community:remove']"
          >删除</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="800px" append-to-body destroy-on-close :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="小区名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入小区名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="简称" prop="sname">
              <el-input v-model="form.sname" placeholder="请输入小区简称(常用名)" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="区域类型" prop="areaType">
              <el-select v-model="form.areaType" placeholder="请选择区域类型" style="width: 260px">
                <el-option label="城镇" value="1" />
                <el-option label="农村" value="2" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <!--<el-form-item label="居民总数" prop="usersNum">-->
              <!--<el-input v-model="form.usersNum" placeholder="请输入小区居民总户数" />-->
            <!--</el-form-item>-->
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="所属区域" prop="city">
              <el-col :span="8">
                <el-select v-model="form.city" placeholder="所属市" @change="(id) =>getAreaList(id,2)" style="width: 200px">
                  <el-option
                    v-for="dict in oneAreaList"
                    :key="dict.id"
                    :label="dict.areaName"
                    :value="dict.id"
                  />
                </el-select>
              </el-col>
              <el-col :span="8">
                <el-select v-model="form.county" placeholder="所属区(县)" @change="(id) =>getAreaList(id,3)" style="width: 200px">
                  <el-option
                    v-for="dict in twoAreaList"
                    :key="dict.id"
                    :label="dict.areaName"
                    :value="dict.id"
                  />
                </el-select>
              </el-col>
              <el-col :span="8">
                <el-select v-model="form.street" placeholder="所属乡镇(街道)" @change="(id) =>getAreaList(id,4)" style="width: 206px">
                  <el-option
                    v-for="dict in threeAreaList"
                    :key="dict.id"
                    :label="dict.areaName"
                    :value="dict.id"
                  />
                </el-select>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="详细地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入详细地址" />
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="经纬度坐标" prop="longitude">
              <el-col :span="8">
                <el-input v-model="form.longitude" placeholder="请输入经度" style="width: 200px"/>
              </el-col>
              <el-col :span="8" >
                <el-input v-model="form.latitude" placeholder="请输入纬度" style="width: 200px"/>
              </el-col>
              <el-col :span="4" style="margin-left: 20px">
                <el-button type="primary" plain @click="MapdialogFun">选择经纬度</el-button>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
        </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>

    <GetPos
      :dialogVisible.sync="dialogTableVisibles"
      :devicePos="devicePos"
      @close="dialogcancelFun"
      @getPath="getPath"
      style="z-index: 3000"
    />
    <DetailInfo ref="detail"/>
  </div>
</template>

<script>
import { listCommunity, getCommunity, delCommunity, addCommunity, updateCommunity, exportCommunity } from "@/api/baseinfo/community";
import GetPos from '@/components/GetPos';
import DetailInfo from "./components/DetailInfo";
import { listArea} from "@/api/baseinfo/area";
export default {
  name: "Community",
  components: {
    GetPos,
    DetailInfo
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 居民小区（村）表格数据
      communityList: [],
      queryParams2: {
        pageNum: 1,
        pageSize: 100,
        parentId:0
      },
      oneAreaList:[],
      twoAreaList:[],
      threeAreaList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        sname: null,
        county: null,
        city: null,
        street: null,
        areaType: null,
        address: null,
        usersNum: null,
        longitude: null,
        latitude: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "小区名称不能为空", trigger: "blur" }
        ],
        areaType:[
          { required: true, message: "区域类型不能为空",trigger: "blur"}
        ]
      },
      dialogTableVisibles: false,
      devicePos: [],
    };
  },
  created() {
    this.getList();
    this.getAreaList(0,1);
  },
  methods: {
    /** 查询居民小区（村）列表 */
    getList() {
      this.loading = true;
      listCommunity(this.queryParams).then(response => {
        this.communityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        sname: null,
        county: null,
        city: null,
        street: null,
        areaType: null,
        address: null,
        usersNum: null,
        longitude: null,
        latitude: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加居民小区（村）";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCommunity(id).then(response => {
        this.form = response.data;
        if(this.form.longitude){
          this.devicePos = [this.form.longitude,this.form.latitude];
        }
        this.getAreaListtwo(this.form.city,2);
        this.getAreaListtwo(this.form.county,3);
        this.getAreaListtwo(this.form.street,4);
        this.open = true;
        this.title = "修改居民小区（村）";
      });

    },
    //用来回显
    getAreaListtwo(e,level) {
      this.queryParams2.parentId = e;
      if(level==4){
        return
      }
      listArea(this.queryParams2).then(response => {
        if(level==1){
          this.oneAreaList = response.rows;
        }else if(level==2){
          this.twoAreaList = response.rows;
        }else if(level==3){
          this.threeAreaList = response.rows;
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCommunity(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCommunity(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除居民小区（村）编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCommunity(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有居民小区（村）数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCommunity(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    MapdialogFun() {
      this.dialogTableVisibles = true;
    },
    /**
     * 地图关闭方法
     */
    dialogcancelFun() {
      this.dialogTableVisibles = false;
    },
    getPath(res){
      //确认选择经纬度
      this.form.longitude = res[0];
      this.form.latitude = res[1];
    },
    //详情
    handleDetail(row){
      this.$refs.detail.getDetailInfo(row.id);
    },
    getAreaList(e,level) {
      this.queryParams2.parentId = e;
      if(level==4){

        return
      }
      listArea(this.queryParams2).then(response => {
        if(level==1){
          this.oneAreaList = response.rows;
        }else if(level==2){
          this.twoAreaList = response.rows;
          this.form.county ='';
          this.form.street ='';
          this.form.village ='';
        }else if(level==3){
          this.threeAreaList = response.rows;
          this.form.street ='';
          this.form.village ='';
        }
      });
    },
  }
};
</script>
