<template>
  <div class="app-container">
    <el-form v-model="queryParams" :inline="true">
      <el-form-item :label="$t('station.StationName')">
        <el-input
          v-model="queryParams.stationName"
          :placeholder="
            $t('share.PleaseEnter') + ' ' + $t('station.StationName')
          "
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="handleQuery"
          size="mini"
          >{{ $t("share.search") }}</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery" size="mini">{{
          $t("share.reset")
        }}</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          plain
          size="mini"
          @click="add()"
          >{{ $t("share.new") }}</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" plain size="mini">{{
          $t("share.edit")
        }}</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          plain
          size="mini"
          @click="handleDeleteAll"
          :disabled="ids.length <= 0"
          >{{ $t("share.delete") }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          plain
          size="mini"
          @click="handleExport"
          >{{ $t("share.export") }}</el-button
        >
      </el-col>
    </el-row>
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column
        prop="stationId"
        :label="$t('station.StationID')"
        align="center"
      />
      <el-table-column
        prop="stationNo"
        :label="$t('station.StationNumber')"
        align="center"
      />
      <el-table-column
        prop="stationName"
        :label="$t('station.StationName')"
        align="center"
      />
      <el-table-column
        prop="stationType"
        :label="$t('station.StationType')"
        align="center"
      />
      <el-table-column
        prop="descride"
        :label="$t('station.StationDescription')"
        align="center"
      />

      <el-table-column
        prop="nodeId"
        :label="$t('station.EdgeNodeIdentificationID')"
        align="center"
      />
      <el-table-column
        prop="voltLevel"
        :label="$t('station.VoltageLevelOfProductionStation')"
        align="center"
      />
      <el-table-column
        prop="provinceName"
        :label="$t('station.province')"
        align="center"
      />
      <el-table-column
        prop="cityName"
        :label="$t('station.city')"
        align="center"
      />
      <el-table-column :label="$t('share.operate')" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="edit(scope.row.stationId)"
            >{{ $t("share.edit") }}</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="onDelete(scope.row.stationId)"
            >{{ $t("share.delete") }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="init"
    />
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form label-width="150px" v-model="dialogForm">
        <el-form-item :label="$t('station.StationType')">
          <el-select
            v-model="dialogForm.stationType"
            :placeholder="
              $t('share.PleaseChoose') + ' ' + $t('station.StationType')
            "
          >
            <!-- <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option> -->
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('station.StationNumber')">
          <el-input
            v-model="dialogForm.stationNo"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('station.StationNumber')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('station.StationName')">
          <el-input
            v-model="dialogForm.stationName"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('station.StationName')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('station.StationDescription')">
          <el-input
            v-model="dialogForm.descride"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('station.StationDescription')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('station.EdgeNodeIdentificationID')">
          <el-input
            v-model="dialogForm.nodeId"
            :placeholder="
              $t('share.PleaseEnter') +
              ' ' +
              $t('station.EdgeNodeIdentificationID')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('station.VoltageLevelOfProductionStation')">
          <el-input
            v-model="dialogForm.voltLevel"
            :placeholder="
              $t('share.PleaseEnter') +
              ' ' +
              $t('station.VoltageLevelOfProductionStation')
            "
            >></el-input
          >
        </el-form-item>
        <el-form-item :label="$t('station.province')">
          <el-input
            v-model="dialogForm.provinceName"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('station.province')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('station.city')">
          <el-input
            v-model="dialogForm.cityName"
            :placeholder="$t('share.PleaseEnter') + ' ' + $t('station.city')"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">{{ $t("share.cancel") }}</el-button>
        <el-button type="primary" @click="submitForm">{{
          $t("share.confirm")
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  postRemove,
  postAdd,
  getItem,
  edit,
  Export,
} from "@/api/basedata/station";
export default {
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      tableData: [],
      dialogVisible: false,
      title: this.$t("station.AddStationInformation"),
      dialogForm: {},
      ids: [],
      type: 0,
      total: 0,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      getList(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.tableData = res.rows;
          this.total = res.total;
          this.$message({
            message: `查询成功`,
            type: "success",
          });
        }
      });
    },
    handleQuery() {
      this.init();
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.init();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.stationId);
    },
    // 删除
    onDelete(id) {
      this.$confirm(`确定要删除Id为${id}的数据吗？`, "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          postRemove(id).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.init();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 多选删除
    handleDeleteAll() {
      let ids = this.ids.toString();
      this.onDelete(ids);
    },
    add() {
      this.type = 0;
      this.dialogForm = {};
      this.dialogVisible = true;
      this.title = this.$t("station.AddStationInformation");
    },
    // 弹框确定
    submitForm() {
      if (this.type == 0) {
        postAdd(this.dialogForm).then((res) => {
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "添加成功!",
            });
            this.dialogVisible = false;
            this.init();
          }
        });
      } else {
        edit(this.dialogForm).then((res) => {
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "编辑成功!",
            });
            this.dialogVisible = false;
            this.init();
          }
        });
      }
    },
    // 取消确定
    cancel() {
      this.dialogVisible = false;
    },
    handleClose() {
      this.dialogVisible = false;
    },
    // 编辑
    edit(id) {
      this.type = 1;
      this.dialogVisible = true;
      this.title = this.$t("station.ModifyStationInformation");
      getItem(id).then((res) => {
        if (res.code == 200) {
          this.dialogForm = res.data;
        }
      });
    },
    // 导出
    handleExport() {
       this.download(
        "/basedata/station/export",
        {
          ...this.queryParams,
        },
        `station_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style scoped></style>
