<template>
  <div class="app-content">
    <div v-if="loadState && !isComplete" style="margin-top:12px">
      <a-form-model ref="ruleForm" :model="formData" :rules="rules" class="from-table">
        <a-row>
          <a-col :span="24">
            <div class="tb-title">
              <span>补充人员基本信息</span>
            </div>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="4" class="bg-gray">
            <div class="required">民族</div>
          </a-col>
          <a-col :span="20">
            <a-form-model-item prop="nation">
              <para-select v-model="formData.nation" :width="150" :typeId="11" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="4" class="bg-gray">
            <div class="required">学历</div>
          </a-col>
          <a-col :span="20">
            <a-form-model-item prop="education">
              <para-select v-model="formData.education" :width="150" :typeId="8" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="4" class="bg-gray">
            <div class="required">职称</div>
          </a-col>
          <a-col :span="20">
            <a-form-model-item prop="title">
              <para-multi-select :width="150" v-model="formData.title" :typeId="7" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="4" class="bg-gray">
            <div class="required">专业</div>
          </a-col>
          <a-col :span="20">
            <a-form-model-item prop="spec">
              <para-multi-select :width="150" v-model="formData.spec" :typeId="42" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="4" class="bg-gray">
            <div class="required">邮箱</div>
          </a-col>
          <a-col :span="20">
            <a-form-model-item prop="email">
              <a-input v-model="formData.email" :maxLength="30" style="width:250px;" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24" style="text-align: center;">
            <a-button type="primary" @click="submit">提交</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
    <div v-if="loadState && isComplete">
      <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} 条`" />
    </div>
    <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>
      <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>
      <project-kpi></project-kpi>
    </a-modal>
  </div>
</template>

<script>
import { getType } from '@/views/utils/auth'
import moment from 'moment'
import { isEmptyParams, filterExportExcelData, tableColumnsName, checkEmail, personGender, personBirthday, checkImageFileType, checkDocumentFileType } from "@/views/utils/common"
import paraMultiSelect from '@/views/components/common/paraMultiSelect'
import paraSelect from '@/views/components/common/paraSelect'
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 axios from 'axios'

export default {
  name: 'reportProject',
  components: {
    projectView, projectCreate, projectEdit, paraMultiSelect, paraSelect, previewFile, projectEditKey
  },
  data () {
    return {
      projType: getType() == "1",
      loadState: false,  //判断是否查询信息完整状态
      isComplete: false,
      formData: { id: null, nation: null, title: null, education: null, spec: null, email: null, sex: null, birthday: null },
      rules: {
        nation: [{ required: true, message: '请选择民族', trigger: 'change' }],
        title: [{ required: true, message: '请选择职称', trigger: 'change' }],
        education: [{ required: true, message: '请选择学历', trigger: 'change' }],
        spec: [{ required: true, message: '请选择专业', trigger: 'change' }],
        // mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { required: true, validator: checkEmail, trigger: 'blur' }
        ],
      },
      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() },
      tableData: [],
      columns: [
        { title: "项目名称", scopedSlots: { customRender: 'projName' } },
        { title: '项目编号', dataIndex: 'projNo', 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: '',
      // downloadUrl: '/downloadFile/202306151700.docx',
      // fileName: '项目申报流程.docx',
      // downloadUrl: '/downloadFile/ExpertInfo.xlsx',
      // fileName: '专家导入模板.xlsx',
      // downloadUrl: '/downloadFile/202406261130.pdf',
      // fileName: '系统用户手册.pdf',
      // downloadUrl: '/downloadFile/1637631492.jpg',
      // fileName: '1637631492.jpg',
      url1: {
        downloadUrl: '/downloadFile/202406261120.doc',
        fileName: '项目合同书模板.doc',
      },
      url2: {
        downloadUrl: '/downloadFile/202407050900.doc',
        fileName: '项目合同书模板.doc',
      },
      realurl: null,
      visibleFileView: false,
      projectKPIView: false,
    }
  },
  created () {
    this.getUserState()
    if (getType() == 1)
      this.realurl = this.url1
    else
      this.realurl = this.url2
  },
  methods: {
    getUserState () {
      this.isComplete = this.$store.state.app.isComplete
      if (this.isComplete) {
        this.loadState = true
        this.getYear()
      }
      else {
        this.getAppPersonInfo()
      }
    },
    getAppPersonInfo () {
      this.$api.person.getAppPersonInfo().then(({ data = {} }) => {
        if (data) {
          this.formData = data.person
          this.loadState = true
          this.isComplete = data.isComplete
          this.$store.commit('app/setComplete', data.isComplete)
          this.getYear()
        }
      }).catch(() => { })
    },
    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()
    },
    submit () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          let pars = isEmptyParams(this.formData)
          let par = { ...pars }
          this.$api.person.updatePerson(par).then(({ data = {} }) => {
            if (data) {
              this.$message.success('提交成功!')
              this.$store.commit('app/setComplete', true)
              this.isComplete = true
            }
          }).catch(() => { })
        } else {
          return false
        }
      });
    },
    createProject () {
      this.id = null
      this.visibleEdit = true
      //this.$store.commit('app/addCard', { title: '项目创建', key: '1299', code: 'projectCreate', keepAlive: 1, router: '/project/create', closable: 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>