<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="所属区域编码" prop="areaCode">
        <el-input
          v-model="queryParams.areaCode"
          placeholder="请输入所属区域编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="社区编码" prop="communityCode">
        <el-input
          v-model="queryParams.communityCode"
          placeholder="请输入社区编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="所属区域" prop="areaCode">
        <el-select v-model="queryParams.province" clearable placeholder="请选择省">
          <el-option
            v-for="item in arealist"
            :key="item.id"
            :label="item.areaName"
            :value="item.id"
            @click.native="province(item)"
          >
          </el-option>
        </el-select>

        <el-select
          v-model="queryParams.city"
          clearable
          placeholder="请选择市"
          style="margin-left: 10px"
        >
          <el-option
            v-for="item in citylist"
            :key="item.areaCode"
            :label="item.areaName"
            :value="item.areaCode"
            @click.native="clickcity(item)"
          >
          </el-option>
        </el-select>
        <el-select
          v-model="queryParams.area"
          clearable
          placeholder="请选择区"
          style="margin-left: 10px"
        >
          <el-option
            v-for="item in arealists"
            :key="item.areaCode"
            :label="item.areaName"
            :value="item.areaCode"
            @click.native="clickarea(item)"
          >
          </el-option>
        </el-select>
        <el-select
          v-model="queryParams.areaCode"
          clearable
          placeholder="请选择街道"
          style="margin-left: 10px"
        >
          <el-option
            v-for="item in streetlist"
            :key="item.areaCode"
            :label="item.areaName"
            :value="item.areaCode"
            @click.native="clickstreet(item)"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="社区名称"
        prop="communityName"
        style="margin-left: 20px"
      >
        <el-input
          v-model="queryParams.communityName"
          placeholder="请输入社区名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="社区经度" prop="communityLongitude">
        <el-input
          v-model="queryParams.communityLongitude"
          placeholder="请输入社区经度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="社区纬度" prop="communityLatitude">
        <el-input
          v-model="queryParams.communityLatitude"
          placeholder="请输入社区纬度"
          clearable
          @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"
          @click="handleAdd"
          v-hasPermi="['system:communityInfo:add']"
          >新增</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:communityInfo: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:communityInfo:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:communityInfo:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="communityInfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
      <!-- <el-table-column label="所属区域编码" align="center" prop="areaCode" />
      <el-table-column label="社区编码" align="center" prop="communityCode" /> -->
      <el-table-column label="所属区域" align="center" prop="areaName" />
      <el-table-column label="社区名称" align="center" prop="communityName" />
      <el-table-column
        label="社区经度"
        align="center"
        prop="communityLongitude"
      />
      <el-table-column
        label="社区纬度"
        align="center"
        prop="communityLatitude"
      />
      <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:communityInfo:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:communityInfo: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="open2"
      width="1060px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        :inline="true"
      >
        <div
          v-for="(item, index) in form.communityInfoList"
          :key="index"
          style="margin-top: 20px"
        >
          <el-form-item label="所属区域" 
          :rules="rules.communityInfoList.areaCode"
          :prop="`communityInfoList.${index}.areaCode`">
            <el-select v-model="item.province" clearable placeholder="请选择省">
              <el-option
                v-for="item in arealist"
                :key="item.areaCode"
                :label="item.areaName"
                :value="item.areaCode"
                @click.native="province(item)"
              >
              </el-option>
            </el-select>

            <el-select
              v-model="item.city"
              clearable
              placeholder="请选择市"
              style="margin-left: 10px"
            >
              <el-option
                v-for="item in citylist"
                :key="item.areaCode"
                :label="item.areaName"
                :value="item.areaCode"
                @click.native="clickcity(item)"
              >
              </el-option>
            </el-select>
            <el-select
              v-model="item.area"
              clearable
              placeholder="请选择区"
              style="margin-left: 10px"
            >
              <el-option
                v-for="item in arealists"
                :key="item.areaCode"
                :label="item.areaName"
                :value="item.areaCode"
                @click.native="clickarea(item)"
              >
              </el-option>
            </el-select>
            <el-select
              v-model="item.areaCode"
              clearable
              placeholder="请选择街道"
              style="margin-left: 10px"
            >
              <el-option
                v-for="item in streetlist"
                :key="item.areaCode"
                :label="item.areaName"
                :value="item.areaCode"
                @click.native="clickstreet(item)"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="社区名称"
           :rules="rules.communityInfoList.communityName"
          :prop="`communityInfoList.${index}.communityName`"
          >
            <el-input
              v-model="item.communityName"
              placeholder="请输入社区名称"
              maxlength="30"
            />
          </el-form-item>
          <el-form-item label="社区经度"
            :rules="rules.communityInfoList.communityLongitude"
          :prop="`communityInfoList.${index}.communityLongitude`"
          >
            <el-input
              v-model="item.communityLongitude"
              placeholder="请输入社区经度"
              maxlength="20"
              
            />
          </el-form-item>
          <el-form-item label="社区纬度"
          :rules="rules.communityInfoList.communityLatitude"
          :prop="`communityInfoList.${index}.communityLatitude`"
          >
            <el-input
              v-model="item.communityLatitude"
              placeholder="请输入社区纬度"
              maxlength="20"
            />
          </el-form-item>
          <el-button
            type="primary"
            circle
            plain
            icon="el-icon-circle-plus-outline"
            @click="addcommunity"
            v-if="index == 0"
            style="margin-left: 10px"
          ></el-button>

          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            plain
            @click="delcommunity(index)"
            style="margin-left: 10px"
            v-if="index != 0"
          ></el-button>
        </div>
      </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>

    <!-- 修改居住社区信息对话框 -->
    <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="areaCode">
          <el-input v-model="form.areaCode" placeholder="请输入所属区域编码" />
        </el-form-item> -->
        <el-form-item label="所属区域" prop="areaCode">
          <el-input v-model="form.areaName" placeholder="请输入所属区域" maxlength="20"/>
        </el-form-item>
        <el-form-item label="社区名称" prop="communityName">
          <el-input v-model="form.communityName" placeholder="请输入社区名称" maxlength="30"/>
        </el-form-item>
        <el-form-item label="社区经度" prop="communityLongitude" >
          <el-input
            v-model="form.communityLongitude"
            placeholder="请输入社区经度"
            maxlength="20"
          />
        </el-form-item>
        <el-form-item label="社区纬度" prop="communityLatitude">
          <el-input
            v-model="form.communityLatitude"
            placeholder="请输入社区纬度"
            maxlength="20"
          />
        </el-form-item>
      </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>
  </div>
</template>

<script>
import {
  listCommunityInfo,
  getCommunityInfo,
  delCommunityInfo,
  addCommunityInfo,
  updateCommunityInfo,
  FirstLevels,
  SecondaryLevelInfo,
} from "@/api/system/communityInfo";

export default {
  name: "CommunityInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total2: 0,
      total: 0,
      // 居住社区信息表格数据
      communityInfoList: [],
      // 所属区域
      arealist: [],
      citylist: [],
      arealists: [],
      streetlist: [],
      city:"",
      area:"",
      areaCodes:"",
      value: "",
      value3: "",
      value2: "",
      value1: "",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // areaCode: null,
        // communityCode: null,
        // communityName: null,
        // communityLongitude: null,
        // communityLatitude: null,
      },
      getProvincequeryParams: {
        pageNum: 1,
        pageSize: 10,
        pageNum: 1,
        // pageSize: 10,
        // searchValue: null,
        // createBy: null,
        // createTime: null,
        // updateBy: null,
        // updateTime: null,
        // remark: null,
        // areaCode: null,
        // areaName: null,
        // areaLevel: null,
        // sort: null,
      },
      getProvincequeryParams2: {
        pageNum: 1,
        pageSize: 10,
        pageNum: 1,
        // pageSize: 10,
        // searchValue: null,
        // createBy: null,
        // createTime: null,
        // updateBy: null,
        // updateTime: null,
        // remark: null,
        // areaCode: null,
        // areaName: null,
        // areaLevel: null,
        // sort: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        areaCode: 
            {
              required: true,
              message: "所属区域不能为空",
              trigger: "blur",
            },
             communityName: 
            {
              required: true,
              message: "社区名称不能为空",
              trigger: "blur",
            },
            communityLongitude: {
              required: true,
              message: "经度不能为空",
              trigger: "blur",
            },
            communityLatitude:{
              required: true,
              message: "纬度不能为空",
              trigger: "blur",
            },
        communityInfoList:{
           areaCode: 
            {
              required: true,
              message: "所属区域不能为空",
              trigger: "blur",
            },
             communityName: 
            {
              required: true,
              message: "社区名称不能为空",
              trigger: "blur",
            },
            communityLongitude: {
              required: true,
              message: "经度不能为空",
              trigger: "blur",
            },
            communityLatitude:{
              required: true,
              message: "纬度不能为空",
              trigger: "blur",
            }
        }
         
          
          
        
      },
    };
  },
  created() {
    this.getList();
    this.Provincelist();
  },
  methods: {
    // 查询省事件
    province(item) {
      console.log(item);
      // this.item.city=""
      // this.value1 = "";
      // this.value2 = "";
      // this.value3 = "";
      SecondaryLevelInfo(item.id).then((res) => {
        console.log(res);
        this.SecondaryLevelInfolist = res.data;
        // console.log(this.SecondaryLevelInfolist);
        this.citylist = res.data;
      });
    },
    // 点击市
    clickcity(item) {
      this.value2 = "";
      this.value3 = "";
      console.log(item);
      SecondaryLevelInfo(item.id).then((res) => {
        console.log(res);
        this.arealists = res.data;
      });
    },
    //点击区县城
    clickarea(item) {
      this.value3 = "";
      console.log(item);
      SecondaryLevelInfo(item.id).then((res) => {
        console.log(res);
        this.streetlist = res.data;
      });
    },
    //点击街道
    clickstreet(item) {
      this.form.areaCode = item.areaCode;
      console.log(this.form);
    },
    // 所属省下拉框
    loadMore() {
      console.log("1");
      var a = Math.ceil(this.total2 / 10);
      console.log(this.arealist.length);
      if (this.arealist.length + 1 >= this.total2) {
      } else {
        if (this.getProvincequeryParams.pageNum >= a) {
        } else {
          this.getProvincequeryParams.pageNum++;
          FirstLevels(this.getProvincequeryParams).then((res) => {
            console.log(res);
            res.rows.forEach((e) => {
              this.arealist.push(e);
              console.log(this.arealist);
            });
          });
        }
      }
    },
    Provincelist() {
      FirstLevels(this.getProvincequeryParams).then((res) => {
        console.log(res);
        this.arealist = res.data;
        this.total2 = res.total;
        console.log(this.arealist);
      });
    },
    //删除delcommunity
    delcommunity(index) {
      this.form.communityInfoList.splice(index, 1);
      console.log(this.form.communityInfoList);
    },
    //添加addcommunity
    addcommunity() {
      var obj = {
        areaCode: "",
        communityCode: "",
        communityName: "",
        communityLongitude: "",
        communityLatitude: "",
        // diseaseName: "",
        // description: "",
        // sort: "",
      };
      if (this.form.communityInfoList.length == 5) {
        this.$message.error("最多只能5条");
      } else {
        this.form.communityInfoList.push(obj);
      }
    },
    /** 查询居住社区信息列表 */
    getList() {
      this.loading = true;
      listCommunityInfo(this.queryParams).then((response) => {
        this.communityInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        // id: null,
        // areaCode: null,
        // communityCode: null,
        // communityName: null,
        // communityLongitude: null,
        // communityLatitude: null,
        // createBy: null,
        // createTime: null,
        // updateBy: null,
        // updateTime: null,
        communityInfoList: [
          {
            province:'',
            city:'',
            area:'',
            areaCodes: '',
            areaCode: "",
            communityCode: "",
            communityName: "",
            communityLongitude: "",
            communityLatitude: "",
          },
        ],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
   
      this.getList();

    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
        this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        searchValue: null,
        province:null,
        city:null,
        area:null,
        areaCodes:null,

        // createBy: null,
        // createTime: null,
        // updateBy: null,
        // updateTime: null,
        // remark: null,
        areaCode: null,
        areaName: null,
        areaLevel: null,
        // sort: null,
      },
      
      //  FirstLevels(this.getProvincequeryParams).then((res) => {
      //   console.log(res);
      //   this.arealist = res.data;
      //   this.total2 = res.total;
      //   console.log(this.arealist);
      // });
      //  SecondaryLevelInfo(this.getProvincequeryParams2).then((res) => {
      //   console.log(res);
      //   this.citylist = res.data;
      //   // this.arealists=res.data;
      //   // this.streetlist=res.data;
      //   this.total2 = res.total;
      //   // console.log(this.arealist);
      // });

      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.open2 = true;
      this.title = "添加居住社区信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      console.log(row)
      const id = row.id || this.ids;
      getCommunityInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改居住社区信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCommunityInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;

              this.getList();
            });
          } else {
            addCommunityInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.open2 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除居住社区信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCommunityInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/communityInfo/export",
        {
          ...this.queryParams,
        },
        `communityInfo_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
