<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="权属单位名称" label-width="100px" prop="beyondEnterpriseName">
        <el-input
          v-model="queryParams.beyondEnterpriseName"
          placeholder="请输入权属单位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" label-width="50px" prop="remarks">
        <el-input
          v-model="queryParams.remarks"
          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"
          v-hasPermi="['supervise: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"
          v-hasPermi="['supervise:station: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="['supervise:station: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="['supervise:station:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="场站类型" align="center" prop="siteStationType" />
      <el-table-column label="场站名称" align="center" prop="siteStationName" />
      <el-table-column label="建设年代" align="center" prop="buildDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.buildDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="建设单位" align="center" prop="buildUnit" />
      <el-table-column label="权属单位" align="center" prop="beyondEnterpriseId" />
      <el-table-column label="权属单位名称" align="center" prop="beyondEnterpriseName" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <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="['supervise:station:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['supervise: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="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row>
          <el-col :span="11">
        <el-form-item label="场站类型" prop="siteStationType">
          <el-select v-model="form.siteStationType" placeholder="请选择场站类型">
            <el-option label="加气站" value="1" />
            <el-option label="门站" value="2" />
            <el-option label="调压站" value="3" />
            <el-option label="储备站" value="4" />
            <el-option label="气化站" value="5" />
          </el-select>
        </el-form-item>
          </el-col>

          <el-col :span="11">
        <el-form-item label="场站名称" prop="siteStationName">
          <el-input v-model="form.siteStationName" placeholder="请输入场站名称" />
        </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
        <el-form-item label="建设年代" prop="buildDate">
          <el-date-picker clearable size="small"
            v-model="form.buildDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择建设年代">
          </el-date-picker>
        </el-form-item>
          </el-col>

          <el-col :span="11">
        <el-form-item label="建设单位" prop="buildUnit">
          <el-input v-model="form.buildUnit" placeholder="请输入建设单位" />
        </el-form-item>
          </el-col>
        </el-row>
<!--        <el-form-item label="权属单位" prop="beyondEnterpriseId">-->
<!--          <el-input v-model="form.beyondEnterpriseId" placeholder="请输入权属单位" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="权属单位名称" prop="beyondEnterpriseName">-->
<!--          <el-input v-model="form.beyondEnterpriseName" placeholder="请输入权属单位名称" />-->
<!--        </el-form-item>-->

        <el-row>
          <el-col :span="11">
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
          </el-col>

          <el-col :span="11">
        <el-form-item label="纬度" prop="latitude">
          <el-input  v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="权属单位" prop="beyondEnterpriseName">
              <el-select v-model="form.beyondEnterpriseId"  placeholder="请在下拉框中选择权属单位" maxlength="255" :disabled="false" clearable>
                <el-option v-for="item in test" :key="item.enterpriseId" :label="item.enterpriseName" :value="item.enterpriseId">
                </el-option>
              </el-select>
            </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" type="textarea" 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>
  </div>
</template>

<script>
import { listStation, getStation, delStation, addStation, updateStation, exportStation ,selectTEnterprise} from "@/api/regulation/station";

export default {
  name: "Station",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 场站信息表格数据
      stationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 场站类型：1.加气站 2.门站 3.调压站 4.储备站 5.气化站字典
      siteStationTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        siteStationName: null,
        beyondEnterpriseName: null,
        remarks: null
      },
      // 表单参数
      form: {},
      // 动态下拉
      test: {},
      // 表单校验
      rules: {
        siteStationType: [
          { required: true, message: "请输入场站类型", trigger: "blur" },
        ],
        siteStationName: [
          { required: true, message: "请输入场站名称", trigger: "blur" },
        ],
        buildDate: [
          { required: true, message: "请选择建设年代", trigger: "blur" },
        ],
        buildUnit: [
          { required: true, message: "请输入建设单位", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("site_station_type").then(response => {
      this.siteStationTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询场站信息列表 */
    getList() {
      this.loading = true;
      listStation(this.queryParams).then(response => {
        this.stationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 场站类型：1.加气站 2.门站 3.调压站 4.储备站 5.气化站字典翻译
    siteStationTypeFormat(row, column) {
      return this.selectDictLabel(this.siteStationTypeOptions, row.siteStationType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        siteStationId: null,
        siteStationType: null,
        siteStationName: null,
        buildDate: null,
        buildUnit: null,
        beyondEnterpriseId: null,
        beyondEnterpriseName: null,
        longitude: null,
        latitude: 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.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      //查询企业名称下拉框数据
      selectTEnterprise().then(response => {
        this.test = response.data;
      });
      this.reset();
      this.open = true;
      this.title = "添加场站信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      //查询企业名称下拉框数据
      selectTEnterprise().then(response => {
        this.test = response.data;
      });
      this.reset();
      const siteStationId = row.siteStationId || this.ids
      getStation(siteStationId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改场站信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.siteStationId != null) {
            updateStation(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStation(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const siteStationIds = row.siteStationId || this.ids;
      this.$confirm('是否确认删除场站信息编号为"' + siteStationIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStation(siteStationIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有场站信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportStation(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
