<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="生产(制造)单位" prop="makeInfo">
        <el-input
          v-model="queryParams.makeInfo"
          placeholder="请输入生产(制造)单位"
          clearable
          size="small"
        />
      </el-form-item>

      <el-form-item label="气瓶条码" prop="qrcode">
        <el-input
          v-model="queryParams.qrcode"
          placeholder="请输入气瓶条码"
          clearable
          size="small"
        />
      </el-form-item>

      <!-- <el-form-item label="钢瓶状态" prop="fState"> 2024-09-13 wh 去掉，感觉监管不需要
        <el-select v-model="queryParams.state" placeholder="请选择钢瓶状态" clearable>
          <el-option
            v-for="item in stateOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </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"
          v-hasPermi="['system:files:export']"
        >导出</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="enterpriseName" :show-overflow-tooltip="true"/>
      <el-table-column label="生产(制造)单位" align="center" prop="makeInfo" :show-overflow-tooltip="true"/>
      <el-table-column label="气瓶条码" align="center" prop="qrcode" :show-overflow-tooltip="true"/>
      <el-table-column label="钢瓶规格名称" align="center" prop="botSpecName" :show-overflow-tooltip="true"/>
      <el-table-column label="钢瓶型号" align="center" prop="bottleType" :show-overflow-tooltip="true"/>
      <el-table-column label="上检日期" align="center" prop="pchkDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pchkDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下检日期" align="center" prop="nchkDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.nchkDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生产日期" align="center" prop="productDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.productDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报废日期" align="center" prop="discardDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.discardDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="安全评定日期" align="center" prop="safeJudgeDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.safeJudgeDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已检验次数" align="center" prop="inspectedTimes"/>
      <!-- <el-table-column label="钢瓶状态" align="center"  prop="state"/> 2024-09-13 wh 去掉，感觉监管不需要 -->
      <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-document"
            @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, getFiles, delFiles, addFiles, updateFiles, exportFiles,importTemplate } from "@/api/lpgRegulation/files";
import ImageUpload from '@/components/ImageUpload';
import DetailInfo from "./components/indexInfo";
import { getToken } from "@/utils/auth"; 

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

      stateOptions:[
        {
          value: 1,
          label: '新增'
        },
        {
          value: 2,
          label: '修改'
        },
        {
          value: -1,
          label: '删除'
        },
      ],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 液化石油气-气瓶档案表格数据
      filesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cChkDate: null,
        nchkDate: null,
        productDate: null,
        discardDate: null,
        safeJudgeDate: null,
        gpressure: null,
        wpressure: null,
        selfWeight: null,
        permitNo: null,
        valveName: null,
        state: null,
        makeInfo: 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();        
      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 });
      this.getList();
      this.$showLoading.hide();
    },

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


    // 是否下发 0-不下发 1 - 下发字典翻译
    fStateFormat(row, column ) {
      if (row.state === 1) {
          return "新增"
      } else if(row.state  === 2) {
          return "修改"
      } else if(row.state  === -1) {
          return "删除"
      }
    },

    /** 查询液化石油气-气瓶档案列表 */
    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 = {
        gasBottleFilesId: null,
        stationId: null,
        regId: null,
        regCode: null,
        equNo: null,
        equTypeCode: null,
        equType: null,
        mediumCode: null,
        medium: null,
        makeDate: null,
        makeInfo: null,
        selfId: null,
        number: null,
        allowedFillingWeight: null,
        wayCard: null,
        provinceCode: null,
        provinceName: null,
        cityCode: null,
        cityName: null,
        countryCode: null,
        countryName: null,
        pressure: null,
        volume: null,
        volumeUnit: null,
        checkId: null,
        checkReportNo: null,
        isCard: null,
        departmentId: null,
        checkDeptCode: null,
        checkDeptName: null,
        checkDate: null,
        nextCheckDate: null,
        scrapId: null,
        scrapDate: null,
        scrapNo: null,
        bottleStatus: "0",
        reportStatus: 0,
        changeStatus: 0,
        status: "0",
        sbCode: null,
        cardRecId: null,
        appId: null,
        qrcode: null,
        cardSendMan: null,
        cardSendDeptCode: null,
        cardSendDeptName: null,
        cardSendTime: null,
        buildUserId: null,
        buildUser: null,
        bak0: null,
        bak1: null,
        bak2: null,
        pchkDate: null,
        nchkDate: null,
        productDate: null,
        discardDate: null,
        safeJudgeDate: null,
        gpressure: null,
        wpressure: null,
        selfWeight: null,
        thickness: null,
        permitNo: null,
        valveType: null,
        valveName: null,
        imageList: null,
        payImage: null,
        batchImage: null,
        deviceImage: null,
        checkImage: null,
        equNoImage: null,
        selfIdImage: null,
        botImage: null,
        botSpecType: null,
        botSpecName: null,
        bottleType: null,
        inspectedTimes: null,
        isSpecialized: null,
        valveManuFacturer: null,
        inspectionLabel: null,
        thirdPartyLabel: null,
        holeCode: null,
        bottleStorageType: null,
        sourceFrom: null,
        rowVersion: null,
        state: null,
        optId: null,
        optName: null,
        addTime: null,
        upTime: null,
        upOptId: null,
        upOptName: 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.gasBottleFilesId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    //详情
    handleDetail(row){
      this.$refs.detail.getDetailInfo(row.gasBottleFilesId);
    },
    /** 导出按钮操作 */
    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>
