<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="居住区(村庄)名称" prop="fVillageName">
        <el-input
          v-model="queryParams.fVillageName"
          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"
          @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"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="villageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" /> 
      <!-- <el-table-column label="所属企业" align="center" prop="beyondEnterpriseName" >
        <span slot-scope="scope" v-if="scope.row.beyondEnterpriseName">{{scope.row.beyondEnterpriseName}}</span>
        <span v-else>-</span>
      </el-table-column> -->

      <el-table-column label="居住区(村庄)名称" align="center" prop="fVillageName" >
        <span slot-scope="scope" v-if="scope.row.fVillageName">{{scope.row.fVillageName}}</span>
        <span v-else>-</span>
      </el-table-column>

      <el-table-column label="所在县" align="center" prop="fRegionName" >
        <span slot-scope="scope" v-if="scope.row.fRegionName">{{scope.row.fRegionName}}</span>
        <span v-else>-</span>
      </el-table-column> 
      
      <el-table-column label="居民总户数" align="center" prop="fTotalNumHouse" >
        <span slot-scope="scope" v-if="scope.row.fTotalNumHouse">{{scope.row.fTotalNumHouse}}</span>
        <span v-else>-</span>
      </el-table-column> 
 
      <el-table-column label="管道燃气用户数" align="center" prop="fPipeGasUsers" >
        <span slot-scope="scope" v-if="scope.row.fPipeGasUsers">{{scope.row.fPipeGasUsers}}</span>
        <span v-else>-</span>
      </el-table-column>  

      <el-table-column label="瓶装燃气用户数" align="center" prop="fBottledGasUsers" >
        <span slot-scope="scope" v-if="scope.row.fBottledGasUsers">{{scope.row.fBottledGasUsers}}</span>
        <span v-else>-</span>
      </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-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</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>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        
        <el-row>
          <!-- <el-col :span="11"> 
            <el-form-item label="所属企业" prop="beyondEnterpriseId">  
              <el-select v-model="form.beyondEnterpriseId"  placeholder="请在下拉框中选择名称" maxlength="255" :disabled="false" clearable style="width: 100%;">
                <el-option v-for="item in enterpriseList" :key="item.enterpriseId" :label="item.enterpriseName" :value="item.enterpriseId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col> -->

          <el-col :span="11">
            <el-form-item label="居住区(村庄)名称" prop="fVillageName">
              <el-input v-model="form.fVillageName" placeholder="请输入居住区(村庄)名称" />
            </el-form-item>
          </el-col>
          
          <el-col :span="11"> 
            <el-form-item label="县级行政区" prop="fRegion">
              <el-select
                v-model="form.fRegion"
                placeholder="请选择县级行政区"
                style="width: 100%"
              >
                <el-option
                  v-for="county in countyInfo"
                  :key="county.fId"
                  :label="county.fName.trim()"
                  :value="county.fId"
                />
              </el-select>
            </el-form-item>
          </el-col>


        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="居民总户数" prop="fTotalNumHouse">
              <el-input @input="fTotalNumHousefilterInput" v-model="form.fTotalNumHouse" placeholder="请输入居民总户数" />
            </el-form-item>
          </el-col>
          
          <el-col :span="11"> 
            <el-form-item label="瓶装燃气用户数" prop="fBottledGasUsers">
              <el-input @input="fBottledGasUsersfilterInput" v-model="form.fBottledGasUsers" placeholder="请输入瓶装燃气用户数" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11"> 
            <el-form-item label="管道燃气用户数" prop="fPipeGasUsers">
              <el-input @input="fPipeGasUsersfilterInput" v-model="form.fPipeGasUsers" placeholder="请输入管道燃气用户数" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="22">
            <el-form-item label="经纬度坐标" prop="longitude">
              <el-col :span="9">
                <el-input v-model="form.longitude" placeholder="请输入经度" />
              </el-col>
              <el-col :span="9" style="margin-left: 15px">
                <el-input v-model="form.latitude" placeholder="请输入纬度"/>
              </el-col>
              <el-col :span="4" style="margin-left: 15px">
                <el-button type="primary" plain @click="MapdialogFun">选择经纬度</el-button>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row> 

        <el-row>
          <el-col :span="22"> 
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" placeholder="请输入备注" />
            </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>

    <GetPos
      :dialogVisible.sync="dialogTableVisibles"
      device=""
      :devicePos="devicePos"
      @close="dialogcancelFun"
      @getPath="getPath"
    />
  </div>
</template>

<script>
import { listVillage, getVillage, delVillage, addVillage, updateVillage, exportVillage } from "@/api/regulation/userManagement/village";
import { selectTEnterprise} from "@/api/regulation/supervise";
import { getDefaultCountyList } from "@/api/area/county";
import GetPos from '@/components/GetPos';

export default {
  name: "Village",
  components: {
    GetPos
  },
  data() {
    return {
      // 燃气企业下拉数据
      enterpriseList: {},
      countyInfo: [],
      /**--------------地图使用数据---------------*/
      dialogTableVisibles:false,
      devicePos: [],
      /**--------------地图使用数据---------------*/
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 基本信息维护-用户管理-居住区(村庄)表格数据
      villageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fVillageName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fVillageName: [
          { required: true, message: "请输入居住区(村庄)名称", trigger: "blur" }, 
        ],
        fTotalNumHouse: [
          { required: true, message: "请输入居住总户数", trigger: "blur" }, 
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getCountyInfo();
  },
  methods: {
    fTotalNumHousefilterInput(value) {
      this.form.fTotalNumHouse = value.replace(/[^\d.]/g, '').replace(/(\..*)\./g, '$1');
    },
    fPipeGasUsersfilterInput(value) {
      this.form.fPipeGasUsers = value.replace(/[^\d.]/g, '').replace(/(\..*)\./g, '$1');
    },
    fBottledGasUsersfilterInput(value) {
      this.form.fBottledGasUsers = value.replace(/[^\d.]/g, '').replace(/(\..*)\./g, '$1');
    },
    /**
     * 经纬度坐标方法
     */
    MapdialogFun() {
      this.dialogTableVisibles = true;
    },
    /**
     * 地图关闭方法
     */
    dialogcancelFun() {
      this.dialogTableVisibles = false;
    },
    /**
     * 经纬度 选择
     * @param res
     */
    getPath(res){
      //确认选择经纬度
      this.form.longitude = res[0];
      this.form.latitude = res[1];
    },
    //获取县级
    getCountyInfo(){
      getDefaultCountyList().then(res =>{
        if(res.code == 200 && res.data){
        this.countyInfo =  res.data;
      }
    })
    },
    /** 查询基本信息维护-用户管理-居住区(村庄)列表 */
    getList() {
      this.loading = true;
      listVillage(this.queryParams).then(response => {
        this.villageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fVillageId: null,
        fVillageName: null,
        fRegion: null,
        fTotalNumHouse: null,
        fPipeGasUsers: null,
        fBottledGasUsers: null,
        longitude: null,
        latitude: null,
        remarks: 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.fVillageId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      //查询企业名称下拉框数据
      selectTEnterprise().then(response => {
        this.enterpriseList = response.data;
      });

      this.devicePos = [];
      this.reset();
      this.open = true;
      this.title = "添加居住区(村庄)";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      //查询企业名称下拉框数据
      selectTEnterprise().then(response => {
        this.enterpriseList = response.data;
      });
      this.reset();
      const fVillageId = row.fVillageId || this.ids
      getVillage(fVillageId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改居住区(村庄)";
        if (response.data.longitude == null || response.data.latitude == null) {
          this.devicePos = [];
        } else {
          this.devicePos = [response.data.longitude, response.data.latitude];
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fVillageId != null) {
            updateVillage(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVillage(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fVillageIds = row.fVillageId || this.ids;
      this.$confirm('是否确认删除基本信息维护-用户管理-居住区(村庄)编号为"' + fVillageIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delVillage(fVillageIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有基本信息维护-用户管理-居住区(村庄)数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportVillage(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
