<template>
  <div>
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href="#">基础信息管理</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href="#">站点费用管理</a>
      </el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-row class="button-group">
      <el-button
          type="primary"
          size="small"
          icon="ios-search"
          @click="changePage()"
          :loading="loading"
          >刷新</el-button
        >
        <el-button
          type="primary"
          size="small"
          icon="ios-search"
          @click="editPrice()"
          :loading="loading"
          >编辑</el-button
        >
      <el-button
        type="primary"
        size="small"
        plain
        icon="el-icon-upload2"
        @click="batchImportVisible = true"
        >导入</el-button
      >
      <el-button
        type="primary"
        size="small"
        plain
        icon="el-icon-download"
        :loading="xlsLoading"
        @click="downloadAll"
      >导出数据</el-button>
    </el-row>
    <el-table
      :data="tableData"
      style="min-height: 400px"
      v-loading="loading"
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="index"
        label="序号"
        :index="indexMethod"
        width="50"
      ></el-table-column>
      <el-table-column prop="name" label="站名" width="200px"></el-table-column>
      <template v-for="col in stationPriceColumns">
        <el-table-column
          :label="col.name"
          :prop="col.id"
          :key="col.id"
          width="200px"
        >
          <template slot-scope="{ row }">
            <div style="display: flex; flex-direction: column">
              <template v-for="(item, index) in row.stationPriceMap[col.id]">
                <div v-html="showItem(item, col.id)" :key="index"></div>
              </template>
            </div>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <!-- 批量导入s -->
    <el-dialog
      title="导入"
      :visible.sync="batchImportVisible"
      :modal-append-to-body="false"
      style="text-align: left"
      :close-on-click-modal="false"
    >
      <el-form label-width="150px">
        <el-form-item label="模板下载">
          <el-link
            href="http://ipcps.oss-cn-hangzhou.aliyuncs.com/upload/picture/2019/4/importTemplate.xlsx"
            type="primary"
            target="_blank"
            >点击下载模板</el-link
          >
        </el-form-item>
        <el-form-item label="上传文件">
          <el-upload
            class="upload-demo"
            accept=".xlsx"
            :action="uploadUrlXls"
            :data="uploadXlsData"
            :headers="headers"
            :on-preview="handleBatchImportPreview"
            name="uploadFile"
            :multiple="true"
            :limit="1"
            :on-remove="handleBatchImportRemove"
            :before-remove="beforeBatchImportRemove"
            :before-upload="beforeUpload"
            :on-exceed="handleBatchImportExceed"
            :on-success="handleBatchImportSuccess"
            :file-list="batchImportFileList"
          >
            <el-button size="small" type="primary" :loading="xlsLoading"
              >点击上传</el-button
            >
            <div slot="tip" class="el-upload__tip">
              只能上传xlsx文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="batchImportVisible = false"
          >关 闭</el-button
        >
      </div>
    </el-dialog>
    <!--批量导入E-->

    <routeInfo-price-detail
      v-if="showModal"
      :title="modalTitle"
      :routeId="routeId"
      @close="onDetailModalClose"
    ></routeInfo-price-detail>
    
  </div>
</template>
<script>
import Constant from "@/constant";

import priceDetail from "./routeInfo-price-detail";
import shiftInfoApi from "@/api/bus/shiftInfo";

import passengerInfoApi from "@/api/bus/passengerInfo";
import priceInfoApi from "@/api/bus/priceInfo";

import { getToken } from "@/utils/auth"; // get token from cookie
export default {
  props: ["businessKey", "title"],
  data() {
    return {
      queryModel: {
        vehicleShiftId: "",
        vehiclePersonId: "",
        userId: "",
        userName: "",
        status: "",
        payStatus: "",
      },
      loading: false,
      tableData: [],
      pageIndex: 1,
      pageSize: 5,
      totalPages: 0,
      totalElements: 0,
      field: "",
      direction: "",
      pageSizeList: [5, 15, 30],
      multipleSelection: [],
      showModal: false,
      modalTitle: "",
      showDialog: true,
      stationPriceColumns: [],
      batchImportVisible: false,
      routeId:"",
      uploadUrlXls: Constant.serverUrl + "/bus/priceInfo/importXls",
      uploadXlsData: {
        subFolder: "driver",
        routeId:""
      },
      headers: {
        Authorization: getToken(),
      },
      batchImportFileList:[],
      xlsLoading:false,
    };
  },
  methods: {
    indexMethod(index) {
      return (this.pageIndex - 1) * this.pageSize + (index + 1);
    },
    changePage() {
      var self = this;
      self.loading = true;
      var formData = new FormData();

      formData.append("routeId", self.routeId);

      priceInfoApi
        .searchRoutePrice(formData)
        .then(function (response) {
          self.loading = false;

          var jsonData = response.data;
          self.tableData = jsonData.stationInfoList;
          self.stationPriceColumns = jsonData.stationPriceColumns;
        })
        .catch((error) => {
          self.loading = false;
          // self.$message.error(error + "");
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleReset(name) {
      this.$refs[name].resetFields();
    },
    editPrice() {
      this.modalTitle = "编辑";
      this.routeId = this.routeId;
      this.showModal = true;
    },
    closeDialog() {
      this.$emit("close", false);
    },
    onDetailModalClose(refreshed) {
      //保存成功后回调
      this.showModal = false;

      if (refreshed) {
        this.changePage();
      }
    },

    showItem(item, id) {
      return item;
    },
    beforeUpload(file, fileList) {
      //导入前判断
      var self = this;
      self.uploadXlsData.routeId = self.routeId;
      self.xlsLoading = true;
      self.loading = true;
    },
    //批量导入-上传成功
    handleBatchImportSuccess(response, file, fileList) {
      var self = this;
      self.xlsLoading = false;
      self.loading = false;
      if (response.result) {
        self.$message.success(response.message);
        this.batchImportFileList = [];
        this.changePage();
        this.uploadCompanyId = "";
        this.uploadXlsData.companyId = "";
        this.batchImportVisible = false;
      } else {
        //this.$message.error(response.message);
        this.batchImportFileList = [];
        this.changePage();

        if (response.data != null) {
          //下载有错误信息提示的报表
          //window.open(response.data);
          self.$message({
            showClose: true,
            dangerouslyUseHTMLString: true,
            message:
              response.message +
              `,<a href="${response.data}" target="_blank">点击下载未导入的数据报表</a>&nbsp;`,
            duration: 30000,
          });
        }
      }
    },
    //批量导入-预览
    handleBatchImportPreview(file) {
      console.log(file.url);
    },
    //批量导入-移除
    handleBatchImportRemove(file, fileList) {
      console.log(file, fileList);
    },
    //批量导入-移除前操作
    beforeBatchImportRemove(file, fileList) {
      console.log(file, fileList);
    },
    //批量导入-文件超出个数限制时的钩子
    handleBatchImportExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    downloadAll() {
      //导出
      var self = this;
      var formData = new FormData();

      formData.append("routeId", self.routeId);

      self.xlsLoading = true;
      self.loading = true;

      priceInfoApi.exportXls(formData).then(function (response) {
        self.xlsLoading = false;
        self.loading = false;
        var jsonData = response.data;
        if (jsonData.result) {
          if (jsonData.data != null) {
            self.$message({
              showClose: true,
              type: "success",
              dangerouslyUseHTMLString: true,
              message: `报表已生成，<a href="${jsonData.data}" target="_blank">点击下载报表</a>&nbsp;`,
              duration: 30000,
            });
          }
        }
      });
    },
  },
  mounted: function () {
    var routeId = this.$route.query.routeId;
    this.routeId = routeId;
    this.changePage();
  },
  
  components: {
    "routeInfo-price-detail": priceDetail,
  },
};
</script>
<style scoped>
.el-divider {
  margin: 5px 0;
}

.el-breadcrumb {
  margin: 10px;
  line-height: 20px;
}
.order-list {
  text-align: left;
}
.button-group {
  padding: 10px;
  text-align: left;
}
</style>