<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="选择搜索条件">
        <el-select v-model="queryParams.searchField" placeholder="请选择搜索条件" clearable>
          <el-option
            v-for="dict in dict.type.buyer_process_file_search_fields"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键词搜索"
          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="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['buyer:processfile:export']"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table stripe v-loading="loading" :data="processfileList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="文件名" align="center" prop="fileName" />
      <el-table-column label="文件类型" align="center" prop="fileType" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="过程文件" align="center" prop="buyerFileInfos">
        <template slot-scope="scope">
          {{ scope.row.buyerFileInfos.length }}
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="updateBy" />
      <el-table-column label="操作时间" align="center" prop="updateTime" />
      <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="handleUpdate(scope.row)"
            v-hasPermi="['buyer:processfile:edit']"
            v-if="scope.row.status !== '0'"
          >上传
          </el-button>
          <el-button
            v-if="scope.row.status !== '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['buyer:processfile:edit']"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleChangeStatus(scope.row)"
            v-hasPermi="['buyer:processfile:remove']"
            v-if="scope.row.status !== '0'"
          >无需文件
          </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="110px" v-if="actionType==='upload'">
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" :disabled="true" />
        </el-form-item>
        <el-form-item label="文件名称" prop="fileName">
          <el-input v-model="form.fileName" placeholder="请输入文件名称" />
        </el-form-item>
        <el-form-item label="文件类型" prop="fileType">
          <el-input v-model="form.fileType" placeholder="请输入文件类型" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="选择上传文件" prop="buyerFileInfos">
          <FileUploadMultiple v-model:value="form.buyerFileInfos" :limit="10" :file-size="10" :drag="true" :fileType="null"></FileUploadMultiple>
        </el-form-item>
      </el-form>
      <div v-else>
        <el-table stripe v-loading="detailLoading" :data="detailList">
          <el-table-column label="文件名称" align="center" prop="fileAddr" />
          <el-table-column label="文件类型" align="center" prop="fileType">
            <template slot-scope="scope">
              {{ currentFileType }}
            </template>
          </el-table-column>
          <el-table-column label="操作人" align="center" prop="createBy" />
          <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="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleDownLoad(scope.row)"
              >下载
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="detailTotal>0"
          :total="detailTotal"
          :page.sync="detailParams.pageNum"
          :limit.sync="detailParams.pageSize"
          @pagination="getList"
        />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProcessfile, getProcessfile, delProcessfile, updateProcessfile, projectProcessfiles } from '@/api/buyer/processfile'
import { deepClone, fileDownload } from '@/utils'

export default {
  name: 'Processfile',
  dicts: ['buyer_process_file_search_fields'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目管理过程文件表格数据
      processfileList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchField: null,
        keyword: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectName: [
          { required: true, message: '请输入项目名称', trigger: 'blur' }
        ],
        fileName: [
          { required: true, message: '请输入文件名称', trigger: 'blur' }
        ],
        fileType: [
          { required: true, message: '请输入文件类型', trigger: 'blur' }
        ]
      },
      actionType: null,
      detailLoading: false,
      detailList: [],
      detailTotal: 0,
      detailParams: {
        pageNum: 1,
        pageSize: 10
      },
      currentProjectId: null,
      currentFileType: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询项目管理过程文件列表 */
    getList() {
      this.loading = true
      listProcessfile(this.addSearchField(this.queryParams, this.queryParams.searchField, this.queryParams.keyword)).then(response => {
        this.processfileList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remarks: null,
        delFlag: null,
        deptId: null,
        projectId: null,
        fileName: null,
        fileType: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams= {
        pageNum: 1,
          pageSize: 10,
          searchField: null,
          keyword: null
      }
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset()
      this.actionType = 'upload'
      this.open = true
      this.title = row.projectName + '过程文件明细'
      this.currentProjectId = row.projectId
    },
    /** 查看操作 */
    handleUpdate(row) {
      this.reset()
      this.actionType = 'upload'
      const id = row.id || this.ids
      getProcessfile(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = `上传过程文件`
      })
    },
    /** 查看操作 */
    handleDetail(row) {
      this.reset()
      this.actionType = 'list'
      const id = row.id || this.ids
      this.currentFileType = row.fileType
      projectProcessfiles({ linkId: id }).then(response => {
        this.detailList = response.data
        this.open = true
        this.title = `"${row.projectName}"过程文件明细`
      })
    },
    handleChangeStatus(row) {
      this.$confirm('确认关闭该项目过程文件上传吗？关闭后将无法恢复请谨慎操作', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = deepClone(row)
        params.status = '0'
        updateProcessfile(params).then(res => {
          this.$modal.msgSuccess('修改成功')
          this.getList()
        })

      }).catch(() => { })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.buyerFileInfos.forEach(item => {
            item.projectId = this.currentProjectId
          })
          updateProcessfile(this.form).then(() => {
            this.$modal.msgSuccess('新增成功')
            this.open = false
            this.getList()
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除项目管理过程文件选中的数据项？').then(function() {
        return delProcessfile(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('buyer/processfile/export', {
        ...this.queryParams
      }, `processfile_${new Date().getTime()}.xlsx`)
    },
    handleDownLoad(row) {
      const filePathArray = row.fileUrl
      fileDownload({ filePathArray })
    }
  }
}
</script>
