<template>
    <el-dialog title="庭院管网目录" :visible.sync="show" width="1100px" append-to-body>
      <el-tabs  v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane label="庭院管网" name="1">
          <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
          <el-form-item label="行政区" prop="district">
            <el-select v-model="queryParams.district" placeholder="请选择行政区" clearable size="small">
              <el-option
                v-for="dict in dict.type.district"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <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>
              <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 @click="getData">选择</el-button>
            </el-form-item>
          </el-form>
          <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="序列" width="70">
              <template slot-scope="scope">
                {{(queryParams.pageNum-1)*queryParams.pageSize+scope.$index+1}}
              </template>
            </el-table-column>
            <el-table-column label="小区名" align="center" prop="name"/>
            <el-table-column label="行政区" align="center" prop="district">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.district" :value="scope.row.district"/>
              </template>
            </el-table-column>
            <el-table-column label="小区规模" align="center" prop="areaSize"/>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-tab-pane>
        <el-tab-pane label="我管理的" name="2">
          <el-table v-loading="loading1" :data="myList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="序列" width="70">
              <template slot-scope="scope">
                {{(queryParams1.pageNum-1)*queryParams1.pageSize+scope.$index+1}}
              </template>
            </el-table-column>
            <el-table-column label="小区名" align="center" prop="name"/>
            <el-table-column label="行政区" align="center" prop="district">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.district" :value="scope.row.district"/>
              </template>
            </el-table-column>
            <el-table-column label="小区规模" align="center" prop="areaSize"/>
            <el-table-column label="操作" align="center" fixed>
              <template slot-scope="scope">
                <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="total1>0"
            :total="total1"
            :page.sync="queryParams1.pageNum"
            :limit.sync="queryParams1.pageSize"
            @pagination="getlist"
          />
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('closeF')">关 闭</el-button>
      </div>
    </el-dialog>
</template>


<script>
import {addNetworkRelation, delNetworkRelation, listNetworkRelation, myListArea} from "@/api/gas/networkRelation";
import Cookies from 'js-cookie'
import {listArea} from "@/api/gas/area";
import {addValvewellRelation} from "@/api/gas/valvewellRelation";
export default {
  name: "areas",
  dicts: ['district'],
  data() {
    return {
      activeName:'1',
      show:false,
      // 遮罩层
      loading: false,
      loading1: false,
      total: 0,
      total1: 0,
      list: [],
      myList: [],
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        userId: undefined,
        jobNum: undefined,
        district: undefined,
        ranks: (Cookies.get("postName").indexOf('xx') > -1 ? parseInt(Cookies.get("postId")) : undefined),
      },
      postName: Cookies.get("postName"),
      queryParams1: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        userId: undefined,
        jobNum: undefined,
        district: undefined,
        ranks: (Cookies.get("postName").indexOf('xx') > -1 ? parseInt(Cookies.get("postId")) : undefined),
      },
      ids:[],
      workerListSelect:[],
      jobNum:undefined,
    };
  },
  beforeDestroy() {
    this.list = []
    this.ids = []
  },created() {
  },
  methods: {
    handleDelete(row) {
      const ids = row.id;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(() => {
        this.loading1 = true;
        return delNetworkRelation(ids,this.jobNum);
      }).then(() => {
        this.loading1 = false;
        this.getlist();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading1 = false;
      });
    },
    getlist(){
      this.loading1 = true;
      myListArea(this.queryParams1).then(response => {
        this.myList = response.rows;
        this.total1 = response.total;
        this.loading1 = false;
      });
    },
    handleClick(tab,event){
      if (this.activeName=='2'){
        this.queryParams1.pageNum=1
        this.queryParams1.jobNum=this.jobNum
        this.getlist()
      }else {
        this.queryParams.pageNum=1;
        this.queryParams.jobNum=this.jobNum
        this.getList();
      }
    },
    getData(){
      this.loading1=true
      let entity = {ids:[],jobNum:this.jobNum};
      entity.ids = this.ids;
      addNetworkRelation(entity).then(res=>{
        if (res.code===200){
          this.$msgbox.alert('添加成功')
          this.loading1=false
          this.getList()
        }
      }).catch(e=>{
        this.loading1=false
        this.getList()
      })
    },
    init(user){
      if (!user) return;
      console.log(user)
      this.show=true
      this.jobNum = user.id
      this.getList(user)
      console.log(123)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.createTimes = [];
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      console.log(this.ids
      )
    },
    /** 查询调压箱管理列表 */
    getList(user) {
      this.loading = true;
      this.queryParams.jobNum=this.jobNum
      listArea(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
  }
};
</script>
