<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="钢印号" prop="vaseCode">
        <el-input
          v-model="queryParams.vaseCode"
          placeholder="请输入钢印号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="气瓶条码" prop="barCode">
        <el-input
          v-model="queryParams.barCode"
          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="warning"
          plain
          icon="el-icon-download"
          size="mini"
		      :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      
      <el-col :span="1.5">
        <el-button
          type="info"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport">导入</el-button>
      </el-col> 

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="filesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="充装记录编号" align="center" prop="gzId" />
      <el-table-column label="充装单位" align="center" prop="stationName" />
      <el-table-column label="气瓶条码" align="center" prop="barCode" />
      <el-table-column label="钢印号" align="center" prop="vaseCode" />
      <el-table-column label="秤号" align="center" prop="scaleNum" />
      <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="handleDetail(scope.row)"
          >详情</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"
    />

    <!-- 详情 -->
    <DetailInfo ref="detail"/>
    
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>

          <br> 
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listFiles, exportFiles,importTemplate } from "@/api/lpgRegulation/airchargefiles";
import DetailInfo from "./components/indexInfo";
import { getToken } from "@/utils/auth";

export default {
  name: "Files",
  components: {
    DetailInfo
  },
  data() {
    return {

      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/lpg/airchargefiles/importData"
      },  

      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 液化石油气-充装记录表格数据
      filesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vaseCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    
    /** 下载模板操作 */
    importTemplate() {
      importTemplate().then(response => {
          this.download(response.msg);
      });
    }, 

    handleImport(){
      this.upload.title = "充装记录导入"; // todo
      this.upload.open = true;
    },
    // 文件上传处理中
    handleFileUploadProgress(event,file,fileList) {
      this.upload.isUploading = true;
      this.$showLoading.show();
    },

    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      if (response.data != null) {
        if (response.data.errorMsg != null && response.data.errorMsg !== "") {
          this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.data.errorMsg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
        }else{
          this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + "成功：" + response.data.successNum + "条，失败：" + response.data.errorNum + "条" + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
        }
      } else {
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      }
      this.getList();
      this.$showLoading.hide();
    },

    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit(); 
    },

    /** 查询液化石油气-充装记录列表 */
    getList() {
      this.loading = true;
      listFiles(this.queryParams).then(response => {
        this.filesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gasAirChargeId: null,
        gzId: null,
        stationName: null,
        barCode: null,
        vaseCode: null,
        scaleNum: null,
        fillStatrTime: null,
        fillEndTime: null,
        setWeight: null,
        wtNet: null,
        setBottleWeight: null,
        bottleWeight: null,
        wtGross: null,
        oprateName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    //详情
    handleDetail(row){
      this.$refs.detail.getDetailInfo(row.gasAirChargeId);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.gasAirChargeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有液化石油气-充装记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.$showLoading.show()
          this.exportLoading = true;
          return exportFiles(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
          this.$showLoading.hide()
        }).catch(() => {});
    }
  }
};
</script>
