<template>
  <div>
    <div class="leakage-top">
      <div style="color: #666666"></div>
      <div class="operate-btn">
        <delids :multipleSelection2="multipleSelection" @del="del()"
          >删除</delids
        >
        <el-button type="primary" @click="refresh()">刷新</el-button>
        <el-button type="primary" @click="block = !block">查询</el-button>
        <el-button type="primary" @click="exportData()">导出</el-button>
      </div>
    </div>
    <el-form
      class="search-div"
      v-if="block"
      :model="railData"
      :inline="true"
      size="mini"
    >
      <el-form-item label="铁路名称：">
        <el-input
          placeholder="请输入铁路名称"
          style="width: 180px"
          v-model="railData.name"
          clearable
        >
        </el-input>
      </el-form-item>
      <el-form-item label="铁路起点站名：">
        <el-input
          placeholder="请输入起点站名"
          v-model="railData.startPointName"
          style="width: 180px"
          clearable
        >
        </el-input>
      </el-form-item>

      <el-form-item label="铁路终点站名：">
        <el-input
          placeholder="请输入终点站名"
          v-model="railData.endPointName"
          style="width: 180px"
          clearable
        >
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="success" @click="searchQuery()">查询</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="multipleTable"
      class="statistics-table"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      :row-class-name="tableRowClassName"
      :row-style="{ height: '50px' }"
      :header-cell-style="{
        background: '#eaf1fe',
        color: '#000',
        fontWeight: 700,
        height: '50px',
      }"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="name" label="铁路名称" align="center" />
      <el-table-column
        prop="startPointName"
        label="铁路起点站名"
        align="center"
      />
      <el-table-column
        prop="endPointName"
        label="铁路终点站名"
        show-overflow-tooltip
        align="center"
      />
      <el-table-column
        prop="totalLong"
        label="铁路总长度（公里）"
        show-overflow-tooltip
        align="center"
      />
      <el-table-column
        prop="action"
        label="操作"
        show-overflow-tooltip
        align="center"
      >
        <template slot-scope="{ row }">
          <el-link type="primary" :underline="false" @click="handleView(row)"
            >查看</el-link
          >
          <el-link type="primary" :underline="false" @click="toEdit(row)"
            >编辑</el-link
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :limit="railData.size"
      :page="railData.current"
      :total="total"
      class="pagination"
      @pagination="handlePageChange"
    />

    <el-dialog
      title="编辑-铁路线"
      :visible.sync="visible"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-form
        ref="formData"
        :model="formData"
        :rules="rules"
        label-width="200px"
        class="form"
      >
        <el-form-item label="铁路名：" prop="name">
          <el-input v-model="formData.name" placeholder="请输入铁路名" />
        </el-form-item>
        <el-form-item label="铁路线起点站名：" prop="startPointName">
          <el-input
            v-model="formData.startPointName"
            placeholder="请输入铁路线起点站名"
          />
        </el-form-item>
        <el-form-item label="铁路线终点站名：" prop="endPointName">
          <el-input
            v-model="formData.endPointName"
            placeholder="请输入铁路线终点站名"
          />
        </el-form-item>
        <el-form-item label="铁路全长（公里）：" prop="totalLong">
          <el-input
            v-model="formData.totalLong"
            placeholder="请输入铁路全长公里数"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { railWaylist, updateRailWay, railWaybatchDelete } from "../../api";
import { exportRailWay } from "@/api/export";
import { successAlert, warningAlert } from "@/utils/alert";
import download from "@/utils/download";

export default {
  data() {
    return {
      railData: formInit(),
      visible: false,
      tableData: [],
      params: {
        current: 1,
        size: 10,
      },
      total: 10,
      multipleSelection: [],
      ids: [],
      block: 0,
      istrue: 0,
      exids: [],
      formData: {},
      rules: {
        name: [{ required: true, message: "请输入铁路名", trigger: "blur" }],
        startPointName: [
          { required: true, message: "请输入铁路线起点站名", trigger: "blur" },
        ],
        endPointName: [
          { required: true, message: "请输入铁路线终点站名", trigger: "blur" },
        ],
        totalLong: [
          { required: true, pattern: /^0\.([1-9]|\d[1-9])$|^[1-9]\d{0,7}\.\d{0,2}$|^[1-9]\d{0,7}$/, message: "最大输入8位整数（小数点后最多2位）", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    var that = this;
    document.onkeydown = function (e) {
      var key = window.event.keyCode;
      if (key == 13) {
        that.searchQuery();
      }
    };
  },
  mounted() {
    this.getTableData();
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      return rowIndex % 2 === 0 ? "" : "single-row";
    },
    del() {
      let ids = this.ids;
      railWaybatchDelete({ ids }).then((res) => {
        if (res.code == 200) {
          successAlert("删除成功");
          this.getTableData();
        } else {
          warningAlert("删除失败");
        }
      });
    },
    refresh() {
      this.reset();
    },
    searchQuery() {
      this.istrue = 1;
      this.getTableData();
    },
    reset() {
      this.railData = formInit();
      this.searchQuery();
    },
    toEdit(row) {
      this.visible = true
      this.formData = editForm(row)
    },
    exportData() {
      if (this.exids.length == 0) {
        this.$message.warning("暂无数据");
        return false;
      } else {
        exportRailWay({ ids: this.exids }).then((res) => {
          download(res, "vnd.ms-excel", `铁路线表.xls`);
        });
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.ids = this.multipleSelection.map((i) => i.id);
    },
    handleView(row) {
      this.$router.push({
        path: "/detail",
        query: {
          id: row.id,
          type: 1,
        },
      });
    },
    handlePageChange(pageData) {
      this.railData.size = pageData.size;
      this.railData.current = pageData.page;
      this.getTableData();
    },
    getTableData() {
      railWaylist(this.railData).then((res) => {
        let list = res.records || [];
        this.tableData = list;
        this.total = res.total;
        this.exids = list.map((i) => i.id);
        if (this.istrue == 1) {
          if (this.tableData.length) {
            successAlert("操作成功");
          } else {
            warningAlert("查询结果为空");
          }
          this.istrue = 0;
        }
      });
    },
    confirm() {
      updateRailWay({ ...this.formData }).then(res => {
        if (res.code == 200) {
          this.$message.success('保存成功！')
        }
        this.getTableData()
        this.visible = false
      })
    }

  },
};

function formInit(data = {}) {
  return {
    endPointName: "",
    name: "",
    startPointName: "",
    current: 1,
    size: 10,
    ...data,
  };
}

function editForm(data = {}) {
  return {
    endPointName: "",
    name: "",
    startPointName: "",
    totalLong: '',
    ...data,
  }
}
</script>
<style lang="scss" scoped>
.leakage-cable {
  .leakage-top {
    margin-bottom: 20px;
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }
  & ::v-deep .cell {
    color: #333333;
  }
  & ::v-deep .stripe {
    background-color: #eaf1fe;
  }
  & ::v-deep .red {
    background-color: #f00;
  }
  & ::v-deep .green {
    background-color: green;
  }
  .page {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 20px 0;
    .pageNum {
      margin: 0 20px;
    }
  }
}
.el-link {
  margin-right: 20px;
}
</style>
<style lang="scss">
.statistics-table {
  .single-row {
    background: #f1f6ff;
  }
  td {
    padding: 5px !important;
  }
}
</style>