<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="场站名称" prop="siteStationName">
        <el-input
          v-model="queryParams.siteStationName"
          placeholder="请输入场站名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="场站类型" prop="siteStationType">
        <el-select v-model="queryParams.siteStationType" placeholder="请选择场站类型" clearable size="small">
          <el-option
            v-for="item in stationType"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </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="['baseInfo:station: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"
        >修改</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"
          v-hasPermi="['baseInfo:station:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="场站名称" align="center" prop="siteStationName" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="场站类型" align="center" prop="siteStationType" :formatter="siteStationFormatte" width="100"/>
      <el-table-column label="场站图片" align="center" prop="pictureAddress">
        <template slot-scope="scope">
          <el-image v-if="scope.row.pictureAddress" :src="scope.row.pictureAddress" :preview-src-list="[scope.row.pictureAddress]" :z-index="9999" style="width: 30px;height: 30px;"></el-image>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="厂站用途" align="center" prop="stationDetail" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="具体地址" align="center" prop="address" width="200" :show-overflow-tooltip="true"/>
      <el-table-column label="启动时间" align="center" prop="enableTiem" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.enableTiem, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="contacts"/>
      <el-table-column label="负责人电话" align="center" prop="phone" width="150"/>
      <!--<el-table-column label="气源种类" align="center" prop="gasSource" :formatter="gasSourceFormatte" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="最大储气量" align="center" prop="bigStorage" width="100"/>
      <el-table-column label="最大供气量" align="center" prop="bigProvide"  width="100"/>
      <el-table-column label="危险源级别" align="center" prop="dangerousLevel"  width="100" :formatter="dangerousLevelFormatte"/>
      <el-table-column label="危险源名称" align="center" prop="dangerousName" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="储罐容积" align="center" prop="volume" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
            v-hasPermi="['baseInfo:station:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['baseInfo:station:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['baseInfo:station: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="title" :visible.sync="open" width="900px" append-to-body destroy-on-close :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="场站名称" prop="siteStationName">
              <el-input v-model="form.siteStationName" placeholder="请输入场站名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="场站类型" prop="siteStationType">
              <el-select v-model="form.siteStationType" placeholder="请选择场站类型" style="width: 100%">
                <el-option
                  v-for="item in stationType"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="厂站用途" prop="stationDetail">
          <el-input v-model="form.stationDetail" type="textarea" placeholder="请输入内容" />
        </el-form-item>

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

        <el-form-item label="场站图片" prop="pictureAddress">
          <!--<el-input v-model="form.pictureAddress" placeholder="请输入图片存储地址" />-->
          <ImageUpload v-model="form.pictureAddress"/>
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="具体地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入具体地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="启动时间" prop="enableTiem">
              <el-date-picker clearable size="small"
                              v-model="form.enableTiem"
                              type="datetime"
                              style="width: 100%"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="选择启动时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="负责人" prop="contacts">
              <el-input v-model="form.contacts" placeholder="请输入负责人" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="负责人电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入负责人电话" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="气源种类" prop="gasSource">
              <el-select v-model="form.gasSource" placeholder="请选气源种类" style="width: 100%">
                <el-option
                  v-for="item in gasSource"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <!--<el-col :span="12">
            <el-form-item label="最大储气量(m³/t)" prop="bigStorage">
              <el-input v-model="form.bigStorage" type="number" placeholder="请输入最大储气量" />
            </el-form-item>
          </el-col>-->

          <el-col :span="12">
            <el-form-item label="最大供气量(m³/d)" prop="bigProvide">
              <el-input v-model="form.bigProvide" type="number" placeholder="请输入最大供气量" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>

          <el-col :span="12">
            <el-form-item label="危险源级别" prop="dangerousLevel">
              <el-select v-model="form.dangerousLevel" placeholder="请输入危险源级别" style="width: 100%">
                <el-option
                  v-for="item in dangerousLevel"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="危险源名称" prop="dangerousName">
              <el-input v-model="form.dangerousName" placeholder="请输入危险源名称" />
            </el-form-item>
          </el-col>
        </el-row>

       <!-- <el-row>
          &lt;!&ndash;<el-col :span="12">
            <el-form-item label="储罐容积" prop="volume">
              <el-input v-model="form.volume" type="number" placeholder="请输入储罐容积" />
            </el-form-item>
          </el-col>&ndash;&gt;
        </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"
      :devicePos="devicePos"
      @close="dialogcancelFun"
      @getPath="getPath"
      style="z-index: 3000"
    />

    <!-- 详情 -->
    <DetailInfo ref="detail"/>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo } from "@/api/baseinfo/stationInfo";
import GetPos from '@/components/GetPos';
import ImageUpload from "@/components/ImageUpload";
import DetailInfo from "./components/DetailInfo";
export default {
  name: "Info",
  components: {
    GetPos,
    ImageUpload,
    DetailInfo
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      stationNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 场站信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        siteStationType: null,
        pictureAddress: null,
        siteStationName: null,
        beyondEnterpriseId: null,
        beyondEnterpriseName: null,
        stationDetail: null,
        longitude: null,
        latitude: null,
        address: null,
        enableTiem: null,
        contacts: null,
        phone: null,
        gasSource: null,
        bigStorage: null,
        bigProvide: null,
        dangerousLevel: null,
        dangerousName: null,
        volume: null,
        isDel: null,
        remarks: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        siteStationName: [
          { required: true, message: "场站名称不能为空", trigger: "blur" }
        ],
        siteStationType: [
          { required: true, message: "请选择场站类型", trigger: "change" }
        ],
        stationDetail: [
          { required: true, message: "场站用途不能为空", trigger: "blur" }
        ],
        longitude: [
          { required: true, message: "经纬度不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "地址不能为空", trigger: "blur" }
        ],
        enableTiem: [
          { required: true, message: "请选择启动时间", trigger: "change" }
        ],
        contacts: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "负责人电话不能为空", trigger: "blur" },
          {
            type: 'number',
            message: '手机号格式不正确',
            trigger: 'blur',
            transform(value) {
              let phonereg = 11 && /^((13|14|15|16|17|18|19)[0-9]{1}\d{8})$/
              if (!phonereg.test(value)) {
                return false
              } else {
                return Number(value)
              }
            }
          }
        ],
        gasSource: [
          { required: true, message: "请选择气源种类", trigger: "change" }
        ],
      },
      stationType: [],
      gasSource: [],
      dangerousLevel: [],
      dialogTableVisibles: false,
      devicePos: []
    };
  },
  created() {
    this.getStationType();
    this.getGasSource();
    this.getDangerousType();
    this.getList();
  },
  methods: {
    /** 查询场站信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        siteStationId: null,
        siteStationType: null,
        pictureAddress: null,
        siteStationName: null,
        beyondEnterpriseId: null,
        beyondEnterpriseName: null,
        stationDetail: null,
        longitude: null,
        latitude: null,
        address: null,
        enableTiem: null,
        contacts: null,
        phone: null,
        gasSource: null,
        bigStorage: null,
        bigProvide: null,
        dangerousLevel: null,
        dangerousName: null,
        volume: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: 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.siteStationId);
      this.stationNames = selection.map(item => item.siteStationName);
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加场站信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const siteStationId = row.siteStationId || this.ids
      getInfo(siteStationId).then(response => {
        this.form = response.data;
        if(this.form.longitude){
          this.devicePos = [this.form.longitude,this.form.latitude];
        }
        this.open = true;
        this.title = "修改场站信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.siteStationId != null) {
            updateInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const siteStationIds = row.siteStationId || this.ids;
      const stationNames = row.siteStationName || this.stationNames;
      this.$confirm('是否确认删除场站名称为"' + stationNames + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfo(siteStationIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有场站信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    //场站类型
    getStationType(){
      this.getDicts("t_station_type").then((response) => {
          this.stationType = response.data;
      });
    },
    //气源中类
    getGasSource(){
      this.getDicts("t_gas_source").then((response) => {
          this.gasSource = response.data;
      });
    },
    //危险源级别
    getDangerousType(){
      this.getDicts("t_dangerous_level").then((response) => {
          this.dangerousLevel = response.data;
      });
    },
    /**
     * 经纬度坐标方法
     */
    MapdialogFun() {
      this.dialogTableVisibles = true;
    },
    /**
     * 地图关闭方法
     */
    dialogcancelFun() {
      this.dialogTableVisibles = false;
    },
    /**
     * 经纬度 选择
     * @param res
     */
    getPath(res){
      //确认选择经纬度
      this.form.longitude = res[0];
      this.form.latitude = res[1];
    },
    //场站类型格式化
    siteStationFormatte(row){
      return this.selectDictLabel(
        this.stationType,
        row.siteStationType
      );
    },
    //气体种类
    gasSourceFormatte(row){
      return this.selectDictLabel(
        this.gasSource,
        row.gasSource
      );
    },
    //危险源级别
    dangerousLevelFormatte(row){
      return this.selectDictLabel(
        this.dangerousLevel,
        row.dangerousLevel
      );
    },
    //详情
    handleDetail(row){
      this.$refs.detail.getDetailInfo(row.siteStationId);
    }
  }
};
</script>
