<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>
        <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="area" width="300" />
      <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="liststationinfo"
    />
    <!-- 添加居住社区信息对话框 -->
    <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; border-bottom: 1px solid #f3f3f3"
        >
          <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="province2(item, index)"
              ></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="clickcity2(item, index)"
              ></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="clickarea2(item, index)"
              ></el-option>
            </el-select>
            <el-select v-model="item.code" clearable placeholder="请选择街道" style="margin-left: 10px">
              <el-option
                v-for="item in streetlist"
                :key="item.areaCode"
                :label="item.areaName"
                :value="item.areaCode"
                @click.native="clickcode(item, index)"
              ></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"
              type="number"
              oninput=" if(value.length>10){value=value.slice(0,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"
              type="number"
              oninput=" if(value.length>10){value=value.slice(0,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" style="margin-right: 45%">
        <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="1060px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true">
        <!-- <el-form-item label="所属区域" prop="areaCode">
          <el-input
            v-model="form.areaName"
            placeholder="请输入所属区域"
            maxlength="20"
          />
        </el-form-item>-->
        <el-form-item label="所属区域" prop="areaCode">
          <el-select v-model="form.province" clearable placeholder="请选择省">
            <el-option
              v-for="item in arealist"
              :key="item.areaCode"
              :label="item.areaName"
              :value="item.areaCode"
              @click.native="province3(item)"
            ></el-option>
          </el-select>

          <el-select v-model="form.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="clickcity3(item)"
            ></el-option>
          </el-select>
          <el-select v-model="form.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="clickarea3(item)"
            ></el-option>
          </el-select>
          <el-select v-model="form.code" clearable placeholder="请选择街道" style="margin-left: 10px">
            <el-option
              v-for="item in streetlist"
              :key="item.areaCode"
              :label="item.areaName"
              :value="item.areaCode"
              @click.native="clickcode2(item)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="社区名称" prop="communityName">
          <el-input
            v-model="form.communityName"
            placeholder="请输入社区名称"
            maxlength="30"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="社区经度" prop="communityLongitude">
          <el-input
            v-model="form.communityLongitude"
            placeholder="请输入社区经度"
            maxlength="20"
            style="width: 200px"
            type="number"
            oninput=" if(value.length>10){value=value.slice(0,20)}"
          />
        </el-form-item>
        <el-form-item label="社区纬度" prop="communityLatitude">
          <el-input
            v-model="form.communityLatitude"
            placeholder="请输入社区纬度"
            maxlength="20"
            style="width: 200px"
            type="number"
            oninput=" if(value.length>10){value=value.slice(0,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,
  SubordinateRegions
} 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: [
        {
          sysAreaVOList: [
            {
              provinceName: null,
              cityName: null,
              streetName: null
            }
          ]
        }
      ],
      // 所属区域
      arealist: [],
      citylist: [],
      arealists: [],
      streetlist: [],
      city: "",
      area: "",
      areaCode: "",
      // value: "",
      // value3: "",
      // value2: "",
      // value1: "",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        city: "",
        area: "",
        areaCode: "",
        province: ""
        // 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: {
    clickcode2(item) {
      this.form.areaCode = Number(item.areaCode);
      this.$forceUpdate();
    },
    clickcode(item, index) {
      this.form.communityInfoList[index].areaCode = Number(item.areaCode);
      // this.form.communityInfoList[index].areaCode = Number(this.form.communityInfoList[index].areaCode);
    },
    // 查询省事件
    province(item) {
      // this.form.xxx[index].code=''
      this.queryParams.city = "";
      this.queryParams.area = "";
      this.queryParams.areaCode = "";
      SecondaryLevelInfo(item.id).then(res => {
        // this.SecondaryLevelInfolist = res.data;
        this.citylist = res.data;
      });
    },

    province2(item, index) {
      this.form.communityInfoList[index].city = "";
      this.form.communityInfoList[index].area = "";
      this.form.communityInfoList[index].areaCode = "";
      SecondaryLevelInfo(item.id).then(res => {
        // this.SecondaryLevelInfolist = res.data;
        this.citylist = res.data;
      });
    },
    // 修改
    province3(item) {
      // this.form.xxx[index].code=''
      this.form.city = "";
      this.form.area = "";
      this.form.code = "";
      SecondaryLevelInfo(item.id).then(res => {
        // this.SecondaryLevelInfolist = res.data;
        this.citylist = res.data;
      });
    },
    // 点击市
    clickcity(item) {
      SecondaryLevelInfo(item.id).then(res => {
        this.queryParams.area = "";
        this.queryParams.areaCode = "";
        this.arealists = res.data;
      });
    },

    clickcity2(item, index) {
      // this.queryParams.area = "";
      // this.queryParams.areaCode = "";
      // this.value2 = "";
      // this.value3 = "";
      this.form.communityInfoList[index].area = "";
      this.form.communityInfoList[index].areaCode = "";
      SecondaryLevelInfo(item.id).then(res => {
        this.arealists = res.data;
      });
    },
    // 修改
    clickcity3(item) {
      SecondaryLevelInfo(item.id).then(res => {
        this.form.area = "";
        this.form.code = "";
        this.arealists = res.data;
      });
    },
    //点击区县城
    clickarea(item) {
      this.queryParams.areaCode = "";
      //  this.queryParams.areaCode = "";
      // this.value3 = "";
      SecondaryLevelInfo(item.id).then(res => {
        this.streetlist = res.data;
      });
    },

    clickarea2(item, index) {
      //  this.queryParams.areaCode = "";
      // this.value3 = "";

      this.form.communityInfoList[index].areaCode = "";
      SecondaryLevelInfo(item.id).then(res => {
        this.streetlist = res.data;
      });
    },

    clickarea3(item) {
      this.form.code = "";
      //  this.queryParams.areaCode = "";
      // this.value3 = "";
      SecondaryLevelInfo(item.id).then(res => {
        this.streetlist = res.data;
      });
    },
    //点击街道
    clickstreet(item) {
      this.form.areaCode = item.areaCode;
    },
    //  clickstreet3(item) {
    //   this.form.areaCode = item.areaCode;
    // },
    // 所属省下拉框
    loadMore() {
      var a = Math.ceil(this.total2 / 10);
      if (this.arealist.length + 1 >= this.total2) {
      } else {
        if (this.getProvincequeryParams.pageNum >= a) {
        } else {
          this.getProvincequeryParams.pageNum++;
          FirstLevels(this.getProvincequeryParams).then(res => {
            res.rows.forEach(e => {
              this.arealist.push(e);
            });
          });
        }
      }
    },
    Provincelist() {
      FirstLevels(this.getProvincequeryParams).then(res => {
        this.arealist = res.data;
        this.total2 = res.total;
      });
    },
    //删除delcommunity
    delcommunity(index) {
      this.form.communityInfoList.splice(index, 1);
    },
    //添加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.liststationinfo();
      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 = {
        code: null,
        communityInfoList: [
          {
            province: "",
            city: "",
            area: "",
            // areaCodes: "",
            areaCode: "",
            communityCode: "",
            communityName: "",
            communityLongitude: "",
            communityLatitude: ""
          }
        ]
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    liststationinfo() {
      listCommunityInfo(this.queryParams).then(response => {
        response.rows.forEach(e => {
          if (e.sysAreaVOList == []) {
          } else if (e.sysAreaVOList == null) {
          } else if (!e.sysAreaVOList) {
          } else if (e.sysAreaVOList.length == 0) {
          } else if (e.sysAreaVOList.length > 0) {
            e.area =
              e.sysAreaVOList[0].provinceName +
              "-" +
              e.sysAreaVOList[0].cityName +
              "-" +
              e.sysAreaVOList[0].regionName +
              "-" +
              e.sysAreaVOList[0].streetName;
          }
        });
        this.communityInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      // listStation(this.queryParams).then((response) => {
      //   response.rows.forEach((e) => {
      //     if (e.sysAreaVOList == []) {
      //     } else if (e.sysAreaVOList == null) {
      //     } else if (!e.sysAreaVOList) {
      //     } else if (e.sysAreaVOList.length == 0) {
      //     } else if (e.sysAreaVOList.length > 0) {
      //       e.area =
      //         e.sysAreaVOList[0].provinceName +
      //         "-" +
      //         e.sysAreaVOList[0].cityName +
      //         "-" +
      //         e.sysAreaVOList[0].regionName +
      //         "-" +
      //         e.sysAreaVOList[0].streetName;
      //     }
      //   });
      //   this.stationList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.liststationinfo();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      // this.arealist="",
      this.arealists = "";
      this.streetlist = "";
      this.citylist = "";
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        searchValue: null,
        province: null,
        city: null,
        area: null,
        areaCode: null,
        areaName: null,
        areaLevel: null
      };
      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();
      const id = row.id || this.ids;
      getCommunityInfo(id).then(response => {
        this.form = response.data;

        this.title = "修改居住社区信息";
        this.form.code = null;
        SubordinateRegions(response.data.areaCode).then(res => {
          if (res.data !== null) {
            this.form.province = String(res.data.provinceCode);
            // this.form.province = res.data.provinceCode;
            this.form.city = res.data.cityName;
            this.form.area = res.data.regionName;
            // this.form.areaCode = res.data.streetName;
            this.form.code = res.data.streetName;
            this.open = true;
          }
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // let newStr = JSON.stringify(this.form.areaCode);
          // let newObj = JSON.parse(newStr);

          if (this.form.id != null) {
            updateCommunityInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open2 = false;
              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("是否确认删除居住社区信息的数据项？")
        .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>
