<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!--
      <el-form-item label="导入时间" prop="preImpTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.preImpTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择导入时间">
        </el-date-picker>
      </el-form-item>

      -->

      <el-form-item label="源文件名" prop="resFileName" label-width="100px">
        <el-input
          v-model="queryParams.resFileName"
          placeholder="请输入源文件名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="impState">
        <el-select
          v-model="queryParams.impState"
          placeholder="状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </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" style="margin-left: 25px;">
      <!--
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dimp:dHighValueInfoPreImp:export']"
        >导出</el-button>
      </el-col>
      -->

      <el-col :span="1.5">
        <el-button
          type="primary"
          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="dMarketsubDataInfoPreImpList"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ background: '#2a8eed', color: '#fafcfe' }"
      border
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        v-if="false"
      />
      <el-table-column label="id" align="center" prop="id" />

      <el-table-column label="文件名" align="center" prop="fileName">
        <template slot-scope="scope">
          <a
            :href="scope.row.fileUrl"
            target="_blank"
            style="text-decoration: underline"
            >{{ scope.row.fileName }}
          </a>
        </template>
      </el-table-column>
      <el-table-column label="年份" align="center" prop="year" />
      <el-table-column label="操作人" align="center" prop="createBy" />
      <el-table-column
        label="导入时间"
        align="center"
        prop="preImpTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.preImpTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="impState">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_imp_status"
            :value="scope.row.impState"
          />
        </template>
      </el-table-column>
      <el-table-column label="总耗时(秒)" align="center" prop="useTime" />
      <el-table-column label="总条数" align="center" prop="totalNum" />
      <el-table-column label="插入条数" align="center" prop="insertNum" />
      <el-table-column label="更新条数" align="center" prop="updateNum" />
      <el-table-column label="跳过条数" align="center" prop="skipNum" />
      <el-table-column label="出错条数" align="center" prop="errorNum" />
      <!--
      <el-table-column label="数据来源" align="center" prop="dataSrc" />
      <el-table-column label="原因" align="center" prop="errReason" />
       <el-table-column label="源文件路径" align="center" prop="resFileUrl" />
      -->

      <el-table-column label="结果文件" align="center" prop="resFileName">
        <template slot-scope="scope">
          <a
            :href="scope.row.resFileUrl"
            target="_blank"
            style="text-decoration: underline"
            >{{ scope.row.resFileName }}
          </a>
        </template>
      </el-table-column>

      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dimp:dHighValueInfo:edit']"
          >调整</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"
    />

    <!-- 添加或修改高价值数据导入记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="导入时间" prop="preImpTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.preImpTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择导入时间"
          >
          </el-date-picker>
        </el-form-item>

        <!--
        <el-form-item label="文件名" prop="fileName">
          <el-input v-model="form.fileName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="访问路径" prop="fileUrl">
          <el-input v-model="form.fileUrl" placeholder="请输入访问路径" />
        </el-form-item>
        <el-form-item label="租户编号" prop="tenantNo">
          <el-input v-model="form.tenantNo" placeholder="请输入租户编号" />
        </el-form-item>
        -->

        <el-form-item label="状态" prop="impState">
          <el-select
            v-model="form.impState"
            placeholder="请选择状态"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>

        <!--
        <el-form-item label="插入条数" prop="insertNum">
          <el-input v-model="form.insertNum" placeholder="请输入插入条数" />
        </el-form-item>
        <el-form-item label="更新条数" prop="updateNum">
          <el-input v-model="form.updateNum" placeholder="请输入更新条数" />
        </el-form-item>
        <el-form-item label="跳过条数" prop="skipNum">
          <el-input v-model="form.skipNum" placeholder="请输入跳过条数" />
        </el-form-item>
        <el-form-item label="出错条数" prop="errorNum">
          <el-input v-model="form.errorNum" placeholder="请输入出错条数" />
        </el-form-item>
        <el-form-item label="数据来源" prop="dataSrc">
          <el-input v-model="form.dataSrc" placeholder="请输入数据来源" />
        </el-form-item>

        <el-form-item label="原因" prop="errReason">
          <el-input v-model="form.errReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>

        <el-form-item label="源文件名" prop="resFileName">
          <el-input v-model="form.resFileName" placeholder="请输入源文件名" />
        </el-form-item>
        <el-form-item label="源文件路径" prop="resFileUrl">
          <el-input v-model="form.resFileUrl" placeholder="请输入源文件路径" />
        </el-form-item>
        -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
      <el-form :model="upload" :rules="rules" ref="ruleForm" class="demo-ruleForm">
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls, .csv"
        :headers="upload.headers"
        :action="upload.url + '?year=' + upload.year"
        :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>
      </el-upload>

        <!---->
        <div class="el-upload__tip">
          <el-form-item label="年份" prop="year">
            <el-select
              v-model="upload.year"
              placeholder="请选择年份"
              clearable
            >
              <el-option
                v-for="year in yearList"
                :key="year"
                :label="year"
                :value="year"
              />
            </el-select>
          </el-form-item>
          <div class="el-upload__tip" slot="tip">
            <el-link type="info" style="font-size: 12px" @click="importTemplate"
              >下载模板</el-link
            >
          </div>
          <div class="el-upload__tip" style="color: red" slot="tip">
            提示：仅允许导入“csv”、“xls”或“xlsx”格式文件！
          </div>
        </div>
    </el-form>


      <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 {
  listDMarketsubDataInfoPreImp,
  getDMarketsubDataInfoPreImp,
  delDMarketsubDataInfoPreImp,
  addDMarketsubDataInfoPreImp,
  importTemplate,
  updateDMarketsubDataInfoPreImp,
} from "@/api/dimp/dMarketsubDataInfoPreImp";
import { getYearList } from "@/api/report/rzlViewStatisticsInfo"
/** 获取token */
import { getToken } from "@/utils/auth";
export default {
  dicts: ["sys_imp_status"],
  name: "DMarketsubDataInfoPreImp",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 查询年份数据
      yearList: [],
      // 总条数
      total: 0,
      // 市场主体数据导入记录表格数据
      dMarketsubDataInfoPreImpList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        preImpTime: null,
        fileName: null,
        fileUrl: null,
        impState: null,
        tenantNo: null,
        insertNum: null,
        updateNum: null,
        skipNum: null,
        errorNum: null,
        dataSrc: null,
        errReason: null,
        resFileName: null,
        resFileUrl: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        year: [
          { required: true, message: '请选择年份', trigger: 'blur' },
        ],
      },
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        year:null,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url:
          process.env.VUE_APP_BASE_API +
          "/dimp/dMarketsubDataInfoPreImp/import",
      },
      statusOptions: [],
    };
  },
  created() {
    this.getList();
    this.getYear();
    this.getDicts("sys_imp_status").then((response) => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 获取查询年份列表 */
    getYear() {
      getYearList().then((response) => {
        //this.hasYearList=response.data;
        this.yearList = response.data;
      });
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "导入万户市场主体数";
      this.upload.open = true;
      this.upload.year = null;
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      // if(!this.upload.year){
      //   this.$message('请选择导入年份');
      //   return;
      // }
      this.$refs.ruleForm.validate((valid)=>{
        if(valid){
          this.$refs.upload.submit()
        }
      })
    },
    /** 下载模板操作 */
    importTemplate() {
      // importTemplate().then((response) => {
      //   this.download(response.msg);
      // });
      this.download('dimp/dMarketsubDataInfoPreImp/importTemplate', {
        ...this.queryParams
      }, `万户市场主体数导入模板.xlsx`)
    },

    /** 查询市场主体数据导入记录列表 */
    getList() {
      this.loading = true;
      listDMarketsubDataInfoPreImp(this.queryParams).then((response) => {
        this.dMarketsubDataInfoPreImpList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        preImpTime: null,
        fileName: null,
        fileUrl: null,
        impState: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        tenantNo: null,
        insertNum: null,
        updateNum: null,
        skipNum: null,
        errorNum: null,
        dataSrc: null,
        errReason: null,
        resFileName: null,
        resFileUrl: 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.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加市场主体数据导入记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getDMarketsubDataInfoPreImp(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改市场主体数据导入记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateDMarketsubDataInfoPreImp(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDMarketsubDataInfoPreImp(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(
          '是否确认删除市场主体数据导入记录编号为"' + ids + '"的数据项？'
        )
        .then(function () {
          return delDMarketsubDataInfoPreImp(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "dimp/dMarketsubDataInfoPreImp/export",
        {
          ...this.queryParams,
        },
        `dMarketsubDataInfoPreImp_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
