<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文件名称" prop="serviceName">
        <el-input v-model="queryParams.serviceName"></el-input>
      </el-form-item>
      <el-form-item label="服务类型" prop="serviceType">
        <el-select v-model="queryParams.serviceType" placeholder="请选择服务类型" clearable filterable>
          <el-option v-for="dict in optionList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @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" icon="el-icon-plus" @click="handleAdd" v-hasPermi="['business:OPSERVICE:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" :disabled="single" @click="handleUpdate" v-hasPermi="['business:OPSERVICE:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['business:OPSERVICE:remove']">删除</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  :data="OPSERVICEList" @selection-change="handleSelectionChange">
      <el-table-column label="文件名称" align="left" prop="serviceName">
        <template slot-scope="scope">
          <div class="omit">{{ scope.row.serviceName }}</div>
        </template>
      </el-table-column>
      <el-table-column label="服务类型" align="center" prop="serviceType">
        <template slot-scope="scope">
          {{ searchType(scope.row.serviceType) }}
        </template>
      </el-table-column>
      <el-table-column label="上传时间" align="center" prop="createTime">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="上传人" align="center" prop="createBy_dictText">
        <template slot-scope="scope">
          {{ scope.row.createBy_dictText }}
        </template>
      </el-table-column>
      <el-table-column label="下载次数" align="center" prop="download">
        <template slot-scope="scope">
          {{ scope.row.download ? scope.row.download : '暂无下载' }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)" v-hasPermi="['business:OPSERVICE:edit']">修改</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)" v-hasPermi="['business:OPSERVICE:remove']">删除</el-button>
          <el-button size="mini" type="text" @click="handleDownload(scope.row)" v-hasPermi="['business:OPSERVICE:remove']">下载</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="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文件" prop="linkUrl">
          <el-upload :http-request="httprequest" :before-upload="uploadFile" :limit="1" class="upload-demo" action="#" :auto-upload="true" :on-change="handleChange" :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="服务类型" prop="serviceType">
          <el-select v-model="form.serviceType" placeholder="请选择服务类型" filterable>
            <el-option v-for="dict in optionList" :key="dict.value + 'serviceType'" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="文件名称" prop="serviceName">
          <el-input v-model="form.serviceName" disabled></el-input>
        </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>
  </div>
</template>

<script>
import { listOPSERVICE, getOPSERVICE, delOPSERVICE, addOPSERVICE, updateOPSERVICE, upload, download } from '@/api/portal/OPSERVICE/OPSERVICE.js';

import { saveAs } from 'file-saver';
export default {
  name: 'OPSERVICE',
  data() {
    return {
      fileList: [],
      //1=招商服务2培训服务
      optionList: [
        { label: '招商服务', value: '1' },
        { label: '培训服务', value: '2' }
      ],
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运营服务表格数据
      OPSERVICEList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        deptId: null,
        serviceName: null,
        linkUrl: null,
        download: null,
        serviceType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: '主键id不能为空', trigger: 'blur' }],
        delFlag: [{ required: true, message: '删除状态不能为空', trigger: 'blur' }],
        deptId: [{ required: true, message: '部门id不能为空', trigger: 'blur' }],
        serviceName: [{ required: true, message: '文件名称不能为空', trigger: 'blur' }],
        linkUrl: [{ required: true, message: '文件附件不能为空', trigger: 'blur' }],
        download: [{ required: true, message: '下载次数不能为空', trigger: 'blur' }],
        serviceType: [{ required: true, message: '服务类型不能为空', trigger: 'change' }]
      },
      setFileName: '', //保存文件名，点击下载时使用这个名字
      downloadLoadingInstance: ''
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询运营服务列表 */
    getList() {
      this.loading = true;
      listOPSERVICE(this.queryParams).then(response => {
        this.OPSERVICEList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleDownload(row) {
      this.setFileName = row.serviceName;
      download(row.linkUrl.replace('/profile',''), row.id).then(res => {
        const blob = new Blob([res]);
        saveAs(blob, this.setFileName);
      
        this.getList();
      });
    },
    httprequest() {},
    uploadFile(file) {
      let formData = new FormData();
      formData.append('file', file);
      upload(formData).then(res => {
        this.form.linkUrl = res.fileName;
        this.$set(this.form, 'serviceName', res.originalFilename);
        this.getList();
      });
    },
    handleChange(file, fileList) {
      this.fileList = fileList.slice(-3);
    },
    searchType(type) {
      let fined = this.optionList.find(item => item.value == type);
      if (fined.label != undefined) {
        return fined.label;
      } else {
        return '未选择类型！';
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.fileList =[]
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        deptId: null,
        serviceName: null,
        linkUrl: null,
        download: null,
        serviceType: 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;
      getOPSERVICE(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) {
            updateOPSERVICE(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.fileList =[]
              this.getList();
            });
          } else {
            addOPSERVICE(this.form).then(response => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.fileList =[]
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('确定删除吗？')
        .then(function () {
          return delOPSERVICE(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'business/OPSERVICE/export',
        {
          ...this.queryParams
        },
        `OPSERVICE_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
