<template>
  <div class="fullHeight whiteBlock">
    <div style="padding: 15px 15px 0">
      <p class="pageTitle">隐藏验收记录</p>
      <el-form
        :inline="true"
        ref="searchForm"
        :model="searchForm"
        size="medium"
      >
        <el-form-item label="项目名称">
          <el-input
            v-model="searchForm.itemName"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
     
        <el-form-item>
          <el-button type="primary" plain @click="getList">{{
            $t('message.energyManage.waybill.query')
          }}</el-button>
          <el-button type="warning" plain @click="refresh">{{
            $t('message.deviceManage.refresh')
          }}</el-button>
          <el-button type="primary" size="medium" @click="add">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table_wrap whiteBlock">
      <el-table class="tables" :data="List">
        <el-table-column
          prop="itemName"
          label="项目名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="acceptanceSite"
          align="center"
          label="验收部位"
        ></el-table-column>
        <el-table-column
          prop="acceptor"
          align="center"
          label="验收人"
        ></el-table-column>
        <el-table-column
          prop="acceptanceTime"
          align="center"
          label="验收时间"
        ></el-table-column>
        <el-table-column
          prop="acceptanceStatus"
          align="center"
          label="验收状态"
        ></el-table-column>
        <el-table-column prop="testReport" align="center" label="资料附件">
          <template slot-scope="scope">
            <el-button
              @click="download(scope.row.testReport)"
              type="text"
              size="small"
              >下载附件</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="180px">
          <template slot-scope="scope">
            <div class="tableBtns" style="margin-left: 20px !important">
              <div @click="edit(scope.row)" class="operationText">
                <img
                  src="@/assets/images/icon-edit.png"
                  width="15px"
                  height="15px"
                />
                <span>编辑</span>
              </div>
              <div @click="deleteDev(scope.row)" class="operationText">
                <img
                  src="@/assets/images/icon-delete.png"
                  width="15px"
                  height="15px"
                />
                <span>删除</span>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagerBox"
        @size-change="SizeChange"
        @current-change="CurrentChange"
        :current-page="pagInfo.pageNo"
        :page-sizes="$store.state.PAGESIZRS"
        :page-size="pagInfo.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="Number(pagInfo.total)"
        background
      ></el-pagination>
    </div>
    <el-dialog
      :modal-append-to-body="false"
      @close="close"
      :title="title"
      :visible.sync="dialogShow"
      width="667px"
    >
      <div class="dialog_content">
        <el-form
          size="medium"
          ref="addEditForm"
          :model="addEditForm"
          :rules="addEditRules"
          label-width="120px"
          class="dialogFormBox"
        >
          <el-form-item label="项目名称" prop="itemName">
            <el-input
              v-model="addEditForm.itemName"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="验收部位" prop="acceptanceSite">
            <el-input
              v-model="addEditForm.acceptanceSite"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="验收人" prop="acceptor">
            <el-input
              v-model="addEditForm.acceptor"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
         
          <el-form-item label="验收日期" prop="acceptanceTime">
            <el-date-picker
              v-model="addEditForm.acceptanceTime"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>

          <el-form-item label="验收状态" prop="acceptanceStatus">
            <el-input
              v-model="addEditForm.acceptanceStatus"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
         
          <el-form-item label="检测报告">
            <el-upload
              class="upload-demo"
              name="files"
              :action="$store.state.UPLOADURL"
              :on-remove="(file, fileList) => handleRemove(file, fileList)"
              :multiple="false"
              :on-success="(res, file) => handleSuccess(res, file)"
              :file-list="surveySchemeList"
              accept=".doc, .docx, .xls, .xlsx, .ppt, .pptx, .pdf"
            >
              <el-button size="medium" type="primary" plain>
                上传附件
              </el-button>
            </el-upload>
          </el-form-item>
          <div class="dialog-footer">
            <el-button
              class="cancleBtn"
              @click="dialogShow = false"
              icon="el-icon-circle-close"
              size="medium"
              >{{ $t('message.deviceManage.cancel') }}
            </el-button>
            <el-button
              type="primary"
              icon="el-icon-circle-check"
              @click="submit"
              size="medium"
              >{{ $t('message.deviceManage.save') }}
            </el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  addgtMaterialAcceptanceCheckApi,
  delgtMaterialAcceptanceCheckApi,
  editgtMaterialAcceptanceCheckApi,
  getgtMaterialAcceptanceCheckApi
} from '@/assets/js/api/materialManagement.js'
export default {
  mounted() {
    this.getList()
  },
  data() {
    return {
      title: "",
      dialogShow: false,
      pagInfo: {
        pageNo: 1, //页数
        pageSize: 10, //条数
        total: 0 //总条数
      },
      List: [],
      addEditForm: {
        itemName:'',
        acceptanceSite:'',
        acceptor:'',
        acceptanceTime:'',
        acceptanceStatus:'',
        testReport:'',
        type:'',
      },
      addEditRules: {
        itemName:[
        {
            required: true,
            message: '必填',
            trigger: 'blur'
          },
        ] 
      },
      searchForm: {
        itemName: '',
        
      },
      surveySchemeList: [],
    }
  },
  methods: {
    //下载附件
    download(val) {
      if (val == '[]') {
        this.$message.error('未查询到附件')
      } else {
        let arr = []
        arr = JSON.parse(val)
        arr.forEach(element => {
          let url = this.$http.defaults.baseURL + 'xmgl/upload/getRenameFile?fileUrl=' + element.url + '&fileName=' + element.name
          window.location.href = url;
        });
      }
    },
    // 删除文件
    handleRemove(file, fileList) {
      this.surveySchemeList = fileList
    },
    handleSuccess(res, file, type) {
      if (res.status == 'SUCCESS') {

        this.surveySchemeList.push({
          name: file.name,
          url: this.$store.state.FILEURL + file.response.data[0].imageUrl
        })


      }
    },
    //查询仓库数据
    getList() {
      getgtMaterialAcceptanceCheckApi({
        pageNo: this.pagInfo.pageNo,
        pageSize: this.pagInfo.pageSize,
        projectSn: this.$store.state.projectSn,
        itemName: this.searchForm.itemName,
        type:4
      }).then((result) => {
        if (result.success) {
          this.List = result.result.records
          this.pagInfo.total = result.result.total
        }
      })
    },

    add() {
      this.title = '新增'
      this.dialogShow = true
      this.surveySchemeList = []
      this.close()
    },

    edit(obj) {
      this.title = '编辑'
      this.dialogShow = true
      this.addEditForm = JSON.parse(JSON.stringify(obj))
      this.surveySchemeList = obj.testReport == '' ? obj.testReport : []
      if (typeof obj.testReport === 'string') {
        this.addEditForm.testReport = JSON.parse(obj.testReport)
        this.surveySchemeList = this.addEditForm.testReport

      }
    },
    submit() {
      let params = JSON.parse(JSON.stringify(this.addEditForm))
      params.projectSn = this.$store.state.projectSn
      params.testReport = JSON.stringify(this.surveySchemeList)
      params.type=4
      this.$refs.addEditForm.validate((valid) => {
        if (valid) {
          if (this.title == '新增') {
            addgtMaterialAcceptanceCheckApi(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          } else if (this.title == '编辑') {
            editgtMaterialAcceptanceCheckApi(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          }
          this.dialogShow = false
        } else {
          return false
        }
      })
    },
    deleteDev(obj) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delgtMaterialAcceptanceCheckApi({ id: obj.id }).then((res) => {
            if (res.success) {
              this.getList()
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            } else {
              this.$message({
                type: "error",
                message: res.message,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    close() {
      this.surveySchemeList = []
      this.addEditForm = {}
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
    SizeChange(val) {
      this.pagInfo.pageSize = val
      this.getList()
    },
    CurrentChange(val) {
      this.pagInfo.pageNo = val
      this.getList()
    },
    refresh() {
      this.searchForm = {}
      this.pagInfo.pageNo = 1 //页数
      this.pagInfo.pageSize = 10 //条数
      this.getList()
    },
  }
}
</script>
<style lang="less" scoped>
.tables2 {
  min-height: auto;
}
</style>