<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商机类型" prop="boType">
        <el-select
          v-model="queryParams.boType"
          @change="boTypeChange"
          clearable
        >
          <el-option
            v-for="dict in dict.type.mk_bo_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="模板名称" prop="name">
        <el-input
          v-model="queryParams.name"
          clearable
          @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="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="uploadAccessory"
        >上传</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="filetemplateList" @selection-change="handleSelectionChange">
      <el-table-column
        type="index"
        label="序号"
        width="55"
        align="center"
      />
      <el-table-column label="商机类型" align="center" prop="boType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mk_bo_type" :value="scope.row.boType"/>
        </template>
      </el-table-column>
      <el-table-column label="模板名称" align="center" prop="name" />
      <el-table-column label="上传者" align="center" prop="createByName" />
      <el-table-column label="上传时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="danger"
            plain
            icon="el-icon-delete"
            @click="deleteAccessory(scope.row.id)"
          >删除</el-button>
          <el-button
            type="success"
            plain
            icon="el-icon-download"
            size="small"
            @click="downloadAccessory(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"
    />

    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商机类型" prop="boType">
          <el-select v-model="form.boType">
            <el-option
              v-for="dict in dict.type.mk_bo_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls, .doc, .docx, .word, .wordx, .png, .jpg, .gif, .txt"
        :headers="upload.headers"
        :action="upload.url + '?boType=' + form.boType"
        :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 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 { listFiletemplate,delFiletemplate } from "@/api/business/spd/bo/filetemplate";
import { getToken } from "@/utils/auth";
import axios from "axios";

export default {
  name: "Filetemplate",
  dicts: ['mk_bo_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件模板表格数据
      filetemplateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        boType: null,
        url: null,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 上传参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 上传类型
        flag: "",
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/mk/bo/filetemplate/upload",
      },
      rules:{
        boType :[{required: true, message: '请选择商机类型', trigger: 'blur'}],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 查询文件模板列表 */
    getList() {
      this.loading = true;
      listFiletemplate(this.queryParams).then(response => {
        this.filetemplateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //上传附件公共方法
    uploadAccessory() {
      this.upload.open = true;
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.$refs.upload.submit();
        }
      });
    },
    // 文件上传中处理
    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(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "上传结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    //下载附件
    downloadAccessory(row) {
      var resUrl = "https://sy.derom.com/document-center/fastdfs/download?id=" + row.url;
      axios
        .create({
          timeout: 3000,
          responseType: "blob", // 响应类型, 将响应数据转换为二进制数据
          headers: {},
        })
        .get(resUrl)
        .then((res) => {
          console.log(res);
          // 地址转换
          let url = window.URL.createObjectURL(res.data);
          const a = document.createElement("a");
          a.setAttribute("href", url);
          a.setAttribute("download", row.name);
          document.body.append(a);
          a.click();
          document.body.removeChild(a);
        });
    },
    //删除附件
    deleteAccessory(id) {
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {})
        .then(() => {
          delFiletemplate(id).then((res) => {
            if (res.code == 200) {
              this.$modal.msgSuccess("删除成功");
              this.getList();
            } else {
              this.$modal.msgSuccess("删除失败");
            }
          });
        })
        .catch(() => {});
    },
  }
};
</script>
