<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="managementOffice">
              <el-select v-model="queryParams.managementOffice" placeholder="请输入管理所" style="width: 100%"
                         @keyup.enter.native="handleQuery">
                <el-option
                  v-for="dict in dict.type.administrative_office"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="编号" prop="number">
              <el-input
                v-model="queryParams.number"
                placeholder="请输入编号"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </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 label="时间范围" prop="createTimes">
              <el-date-picker
                v-model="queryParams.createTimes"
                type="datetimerange"
                :picker-options="pickerOptions"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                align="right">
              </el-date-picker>
            </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="nature">
            </el-table-column>
            <el-table-column label="类型" align="center" prop="type">
            </el-table-column>
            <el-table-column label="路数" align="center" prop="numberOfRoutes">
            </el-table-column>
            <el-table-column label="管理所" align="center" prop="managementOffice">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.administrative_office" :value="scope.row.managementOffice"/>
              </template>
            </el-table-column>
            <el-table-column label="编号" align="center" prop="number"/>
            <el-table-column label="名称" align="center" prop="name"/>
            <el-table-column label="经度" align="center" prop="longitude"/>
            <el-table-column label="纬度" align="center" prop="latitude"/>
            <el-table-column label="巡检次数" align="center" prop="inspectionFrequency"/>
            <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip/>
          </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">
            <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="nature">
            </el-table-column>
            <el-table-column label="类型" align="center" prop="type">
            </el-table-column>
            <el-table-column label="路数" align="center" prop="numberOfRoutes">
            </el-table-column>
            <el-table-column label="管理所" align="center" prop="managementOffice">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.administrative_office" :value="scope.row.managementOffice"/>
              </template>
            </el-table-column>
            <el-table-column label="编号" align="center" prop="number"/>
            <el-table-column label="名称" align="center" prop="name"/>
            <el-table-column label="经度" align="center" prop="longitude"/>
            <el-table-column label="纬度" align="center" prop="latitude"/>
            <el-table-column label="巡检次数" align="center" prop="inspectionFrequency"/>
            <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip/>
            <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('closeE')">关 闭</el-button>
      </div>
    </el-dialog>
</template>


<script>
import {listPressureRegulatingBox} from "@/api/gas/pressureRegulatingBox";
import {
  addRegulatorBoxRelation,
  delRegulatorBoxRelation,
  listRegulatorBoxRelation,
  myListRegulatorBox
} from "@/api/gas/regulatorBoxRelation";
import {delValveWellInspection} from "@/api/gas/valveWellInspection";
export default {
  name: "tyx",
  dicts: ['administrative_office'],
  data() {
    return {
      activeName:'1',
      show:false,
      // 遮罩层
      loading: false,
      loading1: false,
      total: 0,
      total1: 0,
      list: [],
      myList: [],
      title: "",
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nature: undefined,
        jobNum: undefined,
        type: undefined,
        numberOfRoutes: undefined,
        managementOffice: undefined,
        number: undefined,
        name: undefined,
        longitude: undefined,
        latitude: undefined,
        createTimes: []
      },
      queryParams1: {
        pageNum: 1,
        pageSize: 10,
        jobNum: undefined,
      },
      ids:[],
      jobNum:undefined,
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
    };
  },
  beforeDestroy() {
    this.list = []
    this.ids = []
  },
  methods: {
    handleDelete(row) {
      const ids = [row.id];
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(() => {
        this.loading1 = true;
        return delRegulatorBoxRelation(ids,this.jobNum);
      }).then(() => {
        this.loading1 = false;
        this.getlist();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading1 = false;
      });
    },
    getlist(){
      this.loading1=true
      myListRegulatorBox(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;
      addRegulatorBoxRelation(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)
    },
    /** 搜索按钮操作 */
    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
      listPressureRegulatingBox(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
  }
};
</script>
