<template>
  <at-table
    @load="loadData"
    :data="data"
    :page-result="pageResult"
    ref="AtTable"
    @select-all="handleSelect"
    @select="handleSelect"
    @row-dblclick="handleRowDBClick"
  >
    <template v-slot:searchInput>
      <el-form
        ref="searchForm"
        :model="searchForm"
        label-width="80px"
        class="search-top"
        @submit.native.prevent
      >
       <project-info-selector
          v-if="isShow"
          v-model="searchForm.projectId"
          @keyup.enter.native="findinfo"
        ></project-info-selector>
        <el-input
          v-model="searchForm.name"
          style="width: 100%"
          placeholder="请输入文件名称"
          clearable
          @keyup.enter.native="findinfo"
        ></el-input>
        <!-- <el-select
          v-model="searchForm.type"
          placeholder="请选择文件类型"
          clearable
          @keyup.enter.native="findinfo"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select> -->
         <dict-select
            v-model="searchForm.type"
            dictCode="tyfj-fjlx"
            validate="required"
            placeholder="请选择文件类型"
            :clearable="true"
            @keyup.enter.native="findinfo"
          />
        <!-- <el-input
          v-model="searchForm.createName"
          style="width: 100%"
          placeholder="请输入上传人"
          clearable
          @keyup.enter.native="findinfo"
        ></el-input> -->
        <el-button
          size="small"
          @click="findinfo"
          type="primary"
          class="el-icon-search search-btn"
          >查询</el-button
        >
        <el-button
          size="small"
          @click="handleReset"
          type="primary"
          plain
          class="el-icon-refresh"
          >重置</el-button
        >
      </el-form>
    </template>
    <template v-slot:operationBtn>
      <el-button
        size="small"
        type="primary"
        @click="handleAdd"
        icon="el-icon-plus"
        >添加</el-button
      >
      <ht-delete-button
        :url="deleteUrl"
        :requestMethod="'POST'"
        :htTable="$refs.AtTable"
        >删除</ht-delete-button
      >
      <import-dialog ref='import'></import-dialog>
    </template>
    <template>
      <el-table-column type="selection" width="55" align="center">
      </el-table-column>
      <el-table-column type="index" label="序号" width="55" align="center">
      </el-table-column>
        <!-- <el-table-column
        prop="projectName"
        label="项目名称"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column> -->
      <el-table-column
        prop="name"
        label="文件名称"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="type"
        label="文件类型"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template v-slot="{row}">
          {{getDictName('tyfj-fjlx',row.type)}}
        </template>
      </el-table-column>
      <el-table-column
        prop="fileSize"
        label="文件大小"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="createName"
        label="上传人"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="上传时间"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <ht-table-column label="操作" width="200">
        <template v-slot="{row}">
          <el-dropdown
            size="mini"
            split-button
            @command="handleCommand"
            @click="handleCommand({row, command: 'preview'})"
          >
            <span> <i class="el-icon-view"></i>查看</span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                icon="el-icon-download"
                :command="{row: row, command: 'download'}"
                >下载</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </ht-table-column>
    </template>
    <el-dialog
      title="预览"
      class="dialog"
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      v-if="dialogVisible"
      destroy-on-close
      @close="closeDialog"
      width="100%"
    >
      <div>
        <iframe
          :src="url"
          frameborder="0"
          style="z-index: 1000; height: 800px; width: 100%"
        ></iframe>
      </div>
    </el-dialog>
  </at-table>
</template>
<script>
import ImportDialog from './module/ImportDialog.vue'
import req from '@/request.js'
export default {
  name: 'ConstructionEnvironmentProtection',
    components:{
    ImportDialog
  },
   props:{
    pid:{
       type: String,
      default:''
    },
    isShow:{
      type:Boolean,
      default:true
    }
  },
  data() {
    return {
      searchForm: {
        name: '',
        type: '',
        createName: '',
      },
      loading: false,
      searchForm: {}, //顶部搜索参数
      data: [], //表格数据
      pageResult: {
        page: 1,
        pageSize: 20,
        total: 0,
      },
      dialogVisible: false,
      projectObj:[]
    }
  },
    created() {
    this.$bus.$off('fileAdded')
    this.$bus.$off('fileSuccess')
  },
  computed: {
    deleteUrl: function () {
      return window.context.cqxy + '/biz/universal/accessory/v1/delete'
    },
  },
  mounted() {
    this.initProjectAll()
     // 文件选择后的回调
    this.$bus.$on('fileAdded', () => {
      console.log('文件已选择')
    })
    // 文件上传成功的回调
    this.$bus.$on('fileSuccess', (file) => {
      console.log(file)
      let params = [
        {
          sourceId: 'environmentalManagement',
          projectId: this.pid,
          ...file,
          group: 'environmentalManagement',
          
        },
      ]
      this.$http
        .post('${cqxy}/biz/universal/accessory/v1/save', params)
        .then((res) => {
          if (res.data.state) {
            this.$message.success(res.data.message)
          } else {
            this.$message.error(res.data.message)
          }
          this.$refs.AtTable.load()
        })
    })
  },
  methods: {
    handleReset() {
      this.searchForm.projectId=''
      this.searchForm.name = ''
      this.searchForm.type = ''
      this.searchForm.createName = ''
      this.$refs['searchForm'].resetFields()
        this.pageResult={
        page:1,
        pageSize:20,
        total:0
      }
      this.$nextTick(()=>{
        this.$refs.AtTable.load()
      })
    },
    handleAdd() {
      this.$bus.$emit('openUploader')
      // if(!this.isShow){
      //   this.$refs.import.open(this.pid)
      // }
      // this.$refs.import.open()
      // if(!this.isShow){
      //    let selectedProject = this.projectObj.find((item) => {
      //      return item.id=this.pid
      //   // if (item.currentProject && item.currentProject == 1) {
      //   //   return item.currentProject == 1
      //   // }
      // })
      // this.$refs.import.open(selectedProject)
      // }else{
      //   this.$refs.import.open()
      // }
    },
     //初始获取所有项目数据
     initProjectAll() {
      //初始获取所有项目数据
      // if(this.isShow){
      //     this.$http.post('${cqxy}/biz/projectManagement/pm/v1/All').then(
      //   (res) => {
      //     this.projectObj = res.data.value
      //   },
      //   (error) => {
      //     reject(error)
      //   }
      // )
      // }else{
        let params={
          pageBean:{
            page:1,
            pageSize:-1,
            total:0
          },
          querys:[
            {
               group: 'main',
            operation: 'EQUAL',
            property: 'id',
            relation: 'AND',
            value: this.pid,
            }
          ]
        }
        this.$http.post('${cqxy}/biz/projectManagement/pm/v1/page',params).then(res=>{
          if(res.data.state){
            let resp=res.data.value
            this.projectObj=resp.rows
          }
        })
      // }
    },
    handleRowDBClick() {},
    handleSelect(selection) {},
    loadData(param, cb) {
      param.querys = [
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'SOURCE_ID_',
          relation: 'AND',
          value: 'environmentalManagement',
        },
      ]
      let query = []
      let search = {
        'project_id_':this.searchForm.projectId,
        // name_: this.searchForm.name,
        // type_: this.searchForm.type,
        // create_name_: this.searchForm.createName,
      }
     if(!this.isShow){
        search['project_id_']=this.pid
      }
      for (let i in search) {
        if (search[i] && i !== 'project_id_') {
          query.push({
            group: 'advance',
            operation: 'LIKE',
            property: i,
            relation: 'AND',
            value: search[i],
          })
        }else if(search[i] && i == 'project_id_'){
          query.push({
            group: 'advance',
            operation: 'EQUAL',
            property: i,
            relation: 'AND',
            value: search[i],
          })
        }
          else {
          query.push({
            group: 'advance',
            operation: 'NOTNULL',
            property: i,
            relation: 'AND',
            value: search[i],
          })
        }
      }
       
      if(param.querys && param.querys.length){
        param.querys.push(...query)
      }else{
        param.querys=query
      }
        this.$http
          .post('${cqxy}/biz/universal/accessory/v1/getPageBySourceId', param)
          .then(
            (resp) => {
              this.data = resp.data.value.rows
              
              this.pageResult = {
                page: resp.data.value.page,
                pageSize: resp.data.value.pageSize,
                total: resp.data.value.total,
              }
            },
            (error) => {
              reject(error)
            }
          )
          .finally(() => {
            if (cb) {
              cb()
            }
          })
    },
    findinfo() {
      this.$refs.AtTable.load()
    },
    handleCommand(params) {
      console.log(params)
      if (params.command == 'preview') {
        let type = params.row.suffix
        if (
          type.indexOf('xsl') !== -1 ||
          type.indexOf('xslx') !== -1 ||
          type.indexOf('xls') !== -1 ||
          type.indexOf('docx') !== -1 ||
          type.indexOf('doc') !== -1 ||
          type.indexOf('pdf') !== -1 ||
          type.indexOf('jpg') !== -1 ||
          type.indexOf('png') !== -1 ||
          type.indexOf('jpeg') !== -1 ||
          type.indexOf('pptx') !== -1 ||
          type.indexOf('ppt') !== -1
        ) {
          // this.dialogVisible = true
          // // this.url = encodeURIComponent(row.url)
          // this.url = params.row.url
          this.$http
          .get('${cqxy}/wps/oauth/olinePreview?_w_fileid=' + params.row.id)
          .then(res => {
            let url = window.context.cqxy + res.data.value
            window.open(url, '_blank')
          })
        } else {
          this.$message('当前文件暂不支持预览，请下载查看')
        }
      } else {
        let ids = params.row.id
        let bizPath = 'default'
        let formData = new FormData()
        formData.append('bizPath', bizPath)
        formData.append('ids', ids)
        req
          .downloadPostCopy(
            '${cqxy}/biz/universal/accessory/v1/minIoAllDownFile',
            formData
          )
          .then((res) => {
            console.log(res)
          })
      }
    },
        destroyed() {
      this.$bus.$off('fileAdded')
      this.$bus.$off('fileSuccess')
    },
    closeDialog() {
      this.dialogVisible = false
    },
  },
}
</script>
<style lang="scss" scoped>
@import '@/assets/css/common.scss';
</style>

