<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:acceptfile:export']"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table stripe v-loading="loading" :data="acceptfileList" @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="name" />
      <el-table-column label="验收人" align="center" prop="personLiable" />
      <el-table-column label="验收日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" >
        <template slot-scope="scope">
          <span>{{scope.row.remarks && scope.row.remarks.length > 10 ? (scope.row.remarks.substring(0,10) + '...') : scope.row.remarks}}</span>
        </template>
      </el-table-column>
      <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="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.buyer_accept_file_status" :value="scope.row.status" />
        </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="handleUpdate(scope.row,'update')"
            v-hasPermi="['buyer:acceptfile:add']"
            v-if="scope.row.status === '0'"
          >验收
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,'detail')"
            v-hasPermi="['buyer:acceptfile:edit']"
            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="80px">
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" :disabled="true" />
        </el-form-item>
        <el-form-item label="验收名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入验收名称" :disabled="modalType==='detail'" />
        </el-form-item>
        <el-form-item label="验收人" prop="personLiable">
          <el-input v-model="form.personLiable" placeholder="请输入验收人" :disabled="modalType==='detail'" />
        </el-form-item>
        <el-form-item label="验收日期" prop="date">
          <el-date-picker
            clearable
            v-model="form.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择验收日期"
            :disabled="modalType==='detail'"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="项目状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.buyer_accept_file_status"
              :key="dict.value"
              :label="dict.value"
              :disabled="modalType==='detail'"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="实际费用" prop="actual">
          <el-input v-model="form.actual" type="number" placeholder="请输入实际费用" style="width: 80%" :disabled="modalType==='detail'" />
          <template v-if="form.actual">
            <span class="budget text-danger" v-if="form.budget < form.actual">超预算</span>
            <span class="budget text-success" v-else>预算内</span>
          </template>
        </el-form-item>
        <el-form-item label="说明" prop="result">
          <el-input v-model="form.result" type="textarea" placeholder="请输入内容" :disabled="modalType==='detail'" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" :disabled="modalType==='detail'" />
        </el-form-item>
        <el-form-item v-if="form.statuss == '0'" label="上传附件" prop="buyerFileInfos">
          <FileUploadMultiple v-model:value="form.buyerFileInfos" :limit="null" :file-size="10" :drag="true" :fileType="null"></FileUploadMultiple>
        </el-form-item>
        <el-form-item v-else label="附件列表" prop="buyerFileInfos">
          <div class="upload-file">
            <!-- 文件列表 -->
            <transition-group class="upload-file-list el-upload-list el-upload-list--text" name="el-fade-in-linear" tag="ul">
              <span key="empty" v-if="!form.buyerFileInfos || form.buyerFileInfos.length == 0">暂无附件</span>
              <template v-else>
                <li class="el-upload-list__item ele-upload-list__item-content" v-for="(file, index) in form.buyerFileInfos" :key="file.fileName">
              <span class="el-icon-document pointer link" @click="handleDownLoad(file)"> {{ file.fileAddr }} </span>
              </li>
            </template>
            </transition-group>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="form.statuss == '0'" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAcceptfile, getAcceptfile, delAcceptfile, addAcceptfile, updateAcceptfile } from '@/api/buyer/acceptfile'
import { fileDownload } from '@/utils'
export default {
  name: 'Acceptfile',
  dicts: ['buyer_process_file_search_fields', 'buyer_accept_file_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 立项管理验收资料表格数据
      acceptfileList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        remarks: null,
        deptId: null,
        projectId: null,
        name: null,
        date: null,
        status: null,
        result: null,
        actual: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectName: [
          { required: true, message: '项目名称不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请填写验收名称', trigger: 'blur' }
        ],
        personLiable: [
          { required: true, message: '请填写验收人', trigger: 'blur' }
        ],
        date: [
          { required: true, message: '请选择验收日期', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择验收状态', trigger: 'blur' }
        ]
        ,buyerFileInfos: [
          { required: true, message: '请选择上传附件', trigger: 'blur' }
        ],
        remarks: [
          { min: 0, max: 200, message: "备注限制200字", trigger: "change" }
        ]
      },
      modalType: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleDownLoad(file) {
      const filePathArray = file.fileUrl
      fileDownload({ filePathArray })
    },
    /** 查询立项管理验收资料列表 */
    getList() {
      this.loading = true
      listAcceptfile(this.addSearchField(this.queryParams, this.queryParams.searchField, this.queryParams.keyword)).then(response => {
        this.acceptfileList = 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,
        name: null,
        date: null,
        status: '0',
        result: null,
        actual: 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() {
      this.reset()
      this.open = true
      this.form.statuss = '0';
      this.title = '项目验收'
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.reset()
      this.modalType = type
      const id = row.id || this.ids
      getAcceptfile(id).then(response => {
        this.form = response.data
        this.form.statuss = response.data.status
        this.open = true
        this.title = '项目验收'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAcceptfile(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addAcceptfile(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除立项管理验收资料选中数据项？').then(function() {
        return delAcceptfile(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('buyer/acceptfile/export', {
        ...this.queryParams
      }, `acceptfile_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style scoped lang="scss">
.budget {
  display: inline-block;
  margin-left: 15px;
}

.upload-file-list .el-upload-list__item {
  border: 1px solid #e4e7ed;
  line-height: 5;
  margin-bottom: 0px;
  position: relative;
}

.upload-file-list .ele-upload-list__item-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: inherit;
}

.ele-upload-list__item-content-action .el-link {
  margin-right: 10px;
}

</style>
