<template>
  <div class="app-content">
    <a-form :form="form" :model="searchForm" layout="inline" class="search_form">
      <a-form-item>
        <a-input placeholder="项目名称" v-model="searchForm.projName" :maxLength="100" style="width: 180px" />
      </a-form-item>
      <a-form-item>
        <a-input placeholder="项目编号" v-model="searchForm.projNo" :maxLength="100" style="width: 180px" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" icon="search" @click="search">搜索</a-button>
        <a-button icon="reload" style="margin-left: 10px" @click="reset" class="bt-normal">重置</a-button>
        <!-- <a-button type="primary" style="margin-left: 10px" @click="FileDownload" icon="download">项目合同书模板下载</a-button> -->
        <!-- <a-button icon="primary" style="margin-left: 10px" @click="FileView">查看</a-button> -->
      </a-form-item>
    </a-form>
    <div style="width:100%;margin-bottom: 8px;">
      <div style="display: inline-block;;width:50%">
        <btn-group :data="tabDate" :itemCount="itemCount" v-model="activekey" @change="callback" />
      </div>
      <div style="display: inline-block;;width:50%;text-align: right;">
        <!-- <a-button type="primary" @click="projectKPI" icon="download">绩效指标表</a-button> -->
        <!-- <a-button type="primary" @click="createMember">新建项目组成员</a-button> -->
        <a-button type="primary" style="margin-right:8px;" v-if="(display && isButten)" @click="createProject">新建项目</a-button>
        <a-button type="primary" @click="exportData" icon="download">Excel</a-button>
      </div>
    </div>
    <a-divider style="height: 1px; background-color: #e8e8e8;" />
    <span class="form-description"> ※填报时间：{{ description }}</span>
    <a-table :dataSource="tableData" :columns="columns" :scroll="{ x: 'max-content' }" rowKey="id" :pagination="false" :loading="loading">
      <!-- size="small" bordered -->
      <template slot="projName" slot-scope="record">
        <a @click="recordClick(record, 'view')">{{record.projName}}</a>
      </template>
      <template slot="state" slot-scope="record">
        <span>
          {{ record.projStateName }}
        </span>
      </template>
      <template slot="option" slot-scope="record">
        <a-button type="link" size="small" @click="recordClick(record, 'view')">查看</a-button>
        <a-button type="link" size="small" v-if="((record.projState == -10 || record.projState == 10 || record.projState == 30))" @click="recordClick(record, 'edit')">修改</a-button>
        <a-popconfirm title="确定要上报吗？" v-if="((record.projState == 10 || record.projState == 30))" ok-text="确定" cancel-text="取消" @confirm="recordClick(record,'report')">
          <a-button type="link" size="small">上报</a-button>
        </a-popconfirm>
        <a-button type="link" size="small" v-if="record.projState <= 10" @click="recordClick(record,'delete')">删除</a-button>
      </template>
    </a-table>
    <a-pagination v-if="pagination.total > 0" :total="pagination.total" show-size-changer show-quick-jumper v-model="pagination.pageIndex" :page-size="pagination.pageSize" :page-size-options="pagination.pageSizeOptions" @showSizeChange="showSizeChange" @change="change" :showTotal="() => `共 ${pagination.total} 条`" />
    <a-modal v-model="visibleEdit" v-if="visibleEdit" title="项目创建/修改" width="94%" :footer="null" :dialog-style="{ top: '8%' }" destroyOnClose :maskClosable="false">
      <project-create v-model="id" @close="closeWindow"></project-create>
    </a-modal>
    <a-modal v-model="visibleView" v-if="visibleView" title="项目详情" width="94%" :footer="null" :dialog-style="{ top: '8%' }" destroyOnClose :maskClosable="false">
      <project-view v-model="id" @close="closeWindow"></project-view>
    </a-modal>
    <a-modal v-model="visibleFileView" title="查看" width="1000px" :dialog-style="{ top: '10%' }" :footer="null" destroyOnClose :maskClosable="false">
      <preview-file v-model="realurl.downloadUrl" :fileName="realurl.fileName"></preview-file>
    </a-modal>
    <a-modal v-model="projectKPIView" title="查看项目KPI" width="80%" :dialog-style="{ top: '10%' }" :footer="null" destroyOnClose :maskClosable="false">
      <project-kpi></project-kpi>
    </a-modal>
    <a-modal v-model="visiblePersonEdit" v-if="visiblePersonEdit" title="请先完善个人信息！" width="700px" :dialog-style="{ top: '12%' }" :footer="null" destroyOnClose :maskClosable="false">
      <person-info-edit @sub="personSave" />
    </a-modal>
  </div>
</template>

<script>
import { getType, getComplete, setComplete } from '@/views/utils/auth'
import moment from 'moment'
import { isEmptyParams, filterExportExcelData, tableColumnsName, checkEmail, personGender, personBirthday, checkImageFileType, checkDocumentFileType } from "@/views/utils/common"
import projectCreate from '@/views/report/project/components/projectCreate'
import projectEdit from '@/views/report/project/components/projectEdit'
import projectEditKey from "@/views/report/project/components/keyProject/projectEdit"
import projectView from '@/views/report/project/components/projectView'
import previewFile from '@/views/components/common/previewFile'
import knowledgeSelect from '@/views/components/common/knowledgeSelect'
import personInfoEdit from '@/views/basicSetting/person/index'

import axios from 'axios'

export default {
  name: 'reportProject',
  components: {
    projectView, projectCreate, projectEdit,previewFile, projectEditKey, knowledgeSelect, personInfoEdit
  },
  data () {
    return {
      isComplete: getComplete(),
      size: 'small',
      form: this.$form.createForm(this, { name: 'advanced_search' }),
      activekey: '1',
      tabDate: [
        { key: "1", tab: "未上报" },
        { key: "2", tab: "返回修改" },
        { key: "3", tab: "已上报" },
        { key: "4", tab: "所有" },
      ],
      itemCount: [0, 0, 0, 0, 0],
      // 表单
      searchForm: { projName: null, projNo: null, projState: 1, reportYear: null, projType: getType(), knowledgeId: null },
      tableData: [],
      columns: [
        { title: "项目名称", scopedSlots: { customRender: 'projName' } },
        //{ title: '项目编号', dataIndex: 'projNo', align: 'center' },
        { title: '申请编号', dataIndex: 'appNo', align: 'center' },
        // { title: '版本号', dataIndex: 'versionNo', align: 'center' },
        // { title: '项目类别', dataIndex: 'projClassName', align: 'center' },
        { title: '开始时间', dataIndex: 'startDate', align: 'center' },
        { title: '结束时间', dataIndex: 'endDate', align: 'center' },
        { title: '申报年度', dataIndex: 'reportYear', align: 'center' },
        { title: '状态', scopedSlots: { customRender: 'state' }, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'option' }, align: 'center', fixed: 'right', width: 180, },
      ],
      pagination: { pageIndex: 1, pageSize: this.$defaultPageSize, total: 0, pageSizeOptions: this.$defaultPageSizeOptions, },
      loading: false,
      // 弹窗标志
      visibleView: false,
      visibleEdit: false,
      id: null,
      isButten: false,
      display: true,
      description: '',
      url1: {
        downloadUrl: '/downloadFile/202406261120.doc',
        fileName: '项目合同书模板.doc',
      },
      url2: {
        downloadUrl: '/downloadFile/202407050900.doc',
        fileName: '项目合同书模板.doc',
      },
      realurl: null,
      visibleFileView: false,
      projectKPIView: false,
      visiblePersonEdit: false,
    }
  },
  created () {
    this.getYear()
    if (getType() == "1")
      this.realurl = this.url1
    else
      this.realurl = this.url2
  },
  methods: {
    personSave (value) {
      setComplete(true)
      this.isComplete = true
      this.visiblePersonEdit = false
      this.id = null
      this.visibleEdit = true
    },
    getYear () {
      this.$api.batch.getCurrentYearBatch({ type: 1, projType: getType() }).then(({ data = {} }) => {
        if (data) {
          this.isButten = data.disabled
          this.description = data.description
          this.searchForm.reportYear = data.year
          this.getListByPage()
        }
      }).catch(() => { })
    },
    getListByPage () {
      this.getCount()
      this.loading = true
      let pars = isEmptyParams(this.searchForm)
      let par = { ...pars, pageIndex: this.pagination.pageIndex, pageSize: this.pagination.pageSize }
      this.$api.project.getListByPage(par).then(({ data = {} }) => {
        if (data) {
          const { dataList = [], total = 0 } = data
          this.tableData = dataList
          this.pagination.total = total
          this.loading = false
          this.tableData.forEach(e => {
            if (e.startDate) {
              e.startDate = moment(e.startDate).format('YYYY-MM-DD')
              e.endDate = moment(e.endDate).format('YYYY-MM-DD')
            }
          })
        } this.loading = false
      }).catch(() => { this.loading = false })
    },
    getCount () {
      this.$api.project.getCount({ reportYear: this.searchForm.reportYear, projType: getType() }).then(({ data = {} }) => {
        if (data) {
          this.itemCount = [data.count1, data.count2, data.count3, data.count4, data.count5]
        }
      }).catch(() => { })
    },
    search () {
      this.pagination.pageIndex = 1
      this.getListByPage()
    },
    reset () {
      this.searchForm.projName = null
      this.searchForm.projNo = null
      this.pagination.pageIndex = 1
      this.getListByPage()
    },
    change () {
      this.getListByPage()
    },
    showSizeChange (current, pageSize) {
      this.pagination.pageIndex = current
      this.pagination.pageSize = pageSize
      this.getListByPage()
    },
    createProject () {
      if (this.isComplete) {
        this.id = null
        this.visibleEdit = true
      }
      else {
        this.visiblePersonEdit = true
      }
    },
    closeWindow (value) {
      if (value === 'submit') {
        this.visibleEdit = false
        this.getListByPage()
      } else if (value === 'save') {
        this.getListByPage()
      }
      else if (value === 'error') {
        this.id = null
        this.visibleEdit = false
        this.visibleView = false
      }
    },
    onLoad (value) {
      this.loading = value
    },
    recordClick (record, type) {
      if (type === 'view') {
        this.id = record.id
        this.visibleView = true
      } else if (type === 'edit') {
        this.id = record.id
        this.visibleEdit = true
      } else if (type === 'delete') {
        let self = this
        this.$confirm({
          title: '',
          content: '确定要删除该项目？',
          okText: '确定',
          okType: 'danger',
          cancelText: '取消',
          onOk () {
            self.loading = true
            let par = { id: record.id }
            self.$api.project.delete(par).then(({ code, data }) => {
              if (data) {
                self.$message.success('删除成功！')
                self.getListByPage()
              }
              self.loading = false
            }).catch(() => { self.loading = false })
          },
          onCancel () {
          },
        })
      } else if (type === 'report') {
        this.loading = true
        this.$api.project.report({ auditObjectId: record.id, auditType: 1 }).then(({ data = {} }) => {
          if (data) {
            this.$message.success('上报成功！')
            this.getListByPage()
          }
          this.loading = false
        }).catch(() => { this.loading = false })
      }
    },
    callback (key) {
      if (key == '1')
        this.display = true
      else
        this.display = false
      this.searchForm.projState = key
      this.getListByPage()
    },
    exportData () {
      let pars = isEmptyParams(this.searchForm)
      let par = { ...pars, pageIndex: -1, pageSize: -1 }
      this.$api.project.getListByPage(par).then(({ data = {} }) => {
        if (data) {
          const { dataList = [], total = 0 } = data
          this.$ToDoExcel(`项目列表`, tableColumnsName(this.columns), filterExportExcelData(this.columns, dataList))
        }
      }).catch(() => { })
    },
    FileDownload () {
      axios({
        url: this.realurl.downloadUrl, // 替换为实际文件的URL
        method: 'GET',
        responseType: 'blob' // 告诉axios返回的数据类型为Blob
      }).then(response => {
        const url = window.URL.createObjectURL(new Blob([response.data]))
        const link = document.createElement('a')
        link.href = url
        link.setAttribute('download', this.realurl.fileName) // 下载文件的名称
        document.body.appendChild(link)
        link.click()
      })
    },
    FileView () {
      if (checkImageFileType(this.realurl.fileName)) {//文件为图片
        this.$viewerApi({ images: [this.realurl.downloadUrl] })
      } else {
        if (checkDocumentFileType(this.realurl.fileName))
          this.visibleFileView = true
      }
    },
    projectKPI () {
      this.projectKPIView = true
    }
  }
}
</script>
