<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" 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-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="['gas:pressureRegulatingBox: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="['gas:pressureRegulatingBox: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="['gas:pressureRegulatingBox:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['gas:pressureRegulatingBox:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['gas:pressureRegulatingBox:import']"
        >导入
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pressureRegulatingBoxList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
<!--      <el-table-column label="序号" align="center" prop="id" v-if="false"/>-->
      <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="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip/>
      <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="['gas:pressureRegulatingBox:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gas:pressureRegulatingBox: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="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--<div class="el-upload__tip" slot="tip">-->
          <!--<el-checkbox v-model="upload.updateSupport"/>-->
          <!--是否更新已经存在的用户数据-->
          <!--</div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate"
          >下载模板
          </el-link>
        </div>
      </el-upload>
      <div style="padding-top: 15px;line-height: 25px;font-size: 14px;">
        完成度：{{ bfb }}%
        <progress max="100" :value="bfb" style="width: 200px;height: 18px;"></progress>
        <br>
        导入结果：{{ jdtMsg }}<br>
        <span style="color: red">*提示:导入表格中数据项【类型、路数、管理所、性质】<br>
          请根据系统内部名称填写全称</span>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false;bfb='0';jdtMsg=''">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改调压箱管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="性质" prop="nature">
                <el-input v-model="form.nature"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="类型" prop="type">
              <el-input v-model="form.type"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="路数" prop="numberOfRoutes">
              <el-input v-model="form.numberOfRoutes"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="管理所" prop="managementOffice">
              <el-select v-model="form.managementOffice" placeholder="请输入管理所" style="width: 100%">
                <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-col>
          <el-col :span="8">
            <el-form-item label="编号" prop="number">
              <el-input v-model="form.number" placeholder="请输入编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="经度" prop="longitude">
              <el-input v-model="form.longitude" placeholder="请输入经度"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="纬度" prop="latitude">
              <el-input v-model="form.latitude" placeholder="请输入纬度"/>
            </el-form-item>
          </el-col>
<!--          <el-col :span="8">-->
<!--            <el-form-item label="巡检人" >-->
<!--              <el-select v-model="form.inspector" filterable placeholder="请选择巡检人" multiple clearable size="small">-->
<!--                <el-option v-for="item in workerList"-->
<!--                           :key="item.id"-->
<!--                           :label="item.name"-->
<!--                           :value="item.id"/>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="form.remark" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="总户数" prop="remark">
              <el-input-number v-model="pressureRegulatingBoxDetailsBo.totalNumberOfHousehold" :controls="false"
                               style="width: 100%"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="调压箱位置" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.positionOfPressureRegulatingBox"
                        placeholder="请输入调压箱位置"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="控制范围" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.controlRange" placeholder="请输入控制范围"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="接收时间" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.receivingTime" placeholder="请输入接收时间"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="阀井位置" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.valveWellPosition" placeholder="请输入阀井位置"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="阀井钥匙形状" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.valveWellKeyShape" placeholder="请输入阀井钥匙形状"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="厂家" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.manufacturer" placeholder="请输入厂家"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="流量" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.flow" placeholder="请输入流量"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="进口管径" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.importPipeDiameter" placeholder="请输入进口管径"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="出口管径" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.outletPipeDiameter" placeholder="请输入出口管径"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="旁通管径" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.bypassPipeDiameter" placeholder="请输入旁通管径"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="调压器" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.voltageRegulator" placeholder="请输入调压器"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="设施规格型号" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.facilitySpecificationsAndModels"
                        placeholder="请输入设施规格型号"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="入口压力" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.inletPressure" placeholder="请输入入口压力"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出口压力" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.outletPressure" placeholder="请输入出口压力"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="切断压力" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.cutOffPressure" placeholder="请输入切断压力"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="放散压力" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.releasePressure" placeholder="请输入放散压力"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出厂编号" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.factoryNumber" placeholder="请输入出厂编号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="出厂日期" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.factoryDate" placeholder="请输入出厂日期"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="主电源位置" prop="remark">
              <el-input v-model="pressureRegulatingBoxDetailsBo.mainPowerSupplyPosition"
                        placeholder="请输入主电源位置"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="巡检次数" >
              <el-input v-model="form.inspectionFrequency" placeholder="请输入巡检次数"/>
            </el-form-item>
          </el-col>
          <!--          <el-col :span="16">-->
          <!--            <el-form-item label="备注" prop="remark">-->
          <!--              <el-input type="textarea" v-model="form.releasePressure" placeholder="请输入备注"/>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPressureRegulatingBox,
  getPressureRegulatingBox,
  delPressureRegulatingBox,
  addPressureRegulatingBox,
  updatePressureRegulatingBox
} from "@/api/gas/pressureRegulatingBox";
import {getToken} from "@/utils/auth";
import {clearFlushProgresddd, clearFlushProgress, flushProgress, flushProgressqqq} from "@/api/gas/area";
import {getWorker} from "@/api/gas/user";

export default {
  name: "PressureRegulatingBox",
  dicts: ['administrative_office','tyx_nature','tyx_type','tyx_numberOfRoutes',],
  data() {
    return {// 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/gas/pressureRegulatingBox/importData"
      },
      bfb: '0',
      jdtMsg: '',
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      workerList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 调压箱管理表格数据
      pressureRegulatingBoxList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      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]);
          }
        }]
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nature: undefined,
        type: undefined,
        numberOfRoutes: undefined,
        managementOffice: undefined,
        number: undefined,
        name: undefined,
        longitude: undefined,
        latitude: undefined,
        createTimes: []
      },
      // 表单参数
      form: {},
      pressureRegulatingBoxDetailsBo: {},
      // 表单校验
      rules: {
        id: [
          {required: true, message: "序号不能为空", trigger: "blur"}
        ],
        managementOffice: [
          {required: true, message: "管理所不能为空", trigger: "blur"}
        ],
        number: [
          {required: true, message: "编号不能为空", trigger: "blur"}
        ],
        name: [
          {required: true, message: "名称不能为空", trigger: "blur"}
        ],
        longitude: [
          {required: true, message: "经度不能为空", trigger: "blur"}
        ],
        latitude: [
          {required: true, message: "纬度不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getWorkerList();
  },
  methods: {
    getWorkerList(ranks) {
      let data = null
      if(ranks){
        data = {
          ranks:ranks
        }
      }
      getWorker(data).then(req => {
        this.workerList = req.data
      })
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.getList()
    },
    /** 下载模板操作 */
    importTemplate() {
      this.$download.excel('/gas/pressureRegulatingBox/importTemplate')
    },
    // 提交上传文件
    submitFileForm() {
      const ther = this
      this.timeInterval = setInterval(function() {
        ther.flushProgress()
      }, 1500)
      this.$refs.upload.submit()
    },
    // 提交上传文件
    flushProgress() {
      flushProgressqqq().then(response => {
        this.bfb = parseInt(response.data.bfb) >= 100 ? 100 : response.data.bfb
        this.jdtMsg = response.data.message
        if (response.data.message != null && response.data.message != '') {
          clearFlushProgresddd().then(response => {
          })
          clearInterval(this.timeInterval)
        }
      })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = '用户导入'
      this.upload.open = true
    },

    /** 查询调压箱管理列表 */
    getList() {
      this.loading = true;
      listPressureRegulatingBox(this.queryParams).then(response => {
        this.pressureRegulatingBoxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.pressureRegulatingBoxDetailsBo = {}
      this.form = {
        id: undefined,
        nature: undefined,
        type: undefined,
        numberOfRoutes: undefined,
        managementOffice: undefined,
        number: undefined,
        name: undefined,
        longitude: undefined,
        latitude: undefined,
        remark: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        pressureRegulatingBoxDetailsBo: undefined,
        updateTime: undefined,
        // inspector:undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    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)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加调压箱管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getPressureRegulatingBox(id).then(response => {
        this.loading = false;
        this.form = response.data;
        // this.form.inspector = response.data.inspector != null ? response.data.inspector.split(",").map(Number):undefined;
        this.pressureRegulatingBoxDetailsBo = response.data.pressureRegulatingBoxDetailsVo[0];
        this.open = true;
        this.title = "修改调压箱管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.form.pressureRegulatingBoxDetailsBo = this.pressureRegulatingBoxDetailsBo
          if (this.form.id != null) {
            // this.form.inspector = this.form.inspector.join(',')
            updatePressureRegulatingBox(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            // this.form.inspector = this.form.inspector.join(',')
            addPressureRegulatingBox(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除调压箱管理编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delPressureRegulatingBox(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$download.excel('/gas/pressureRegulatingBox/export', this.queryParams);
    }
  }
};
</script>
