<template>
  <div class="app-content">
    <a-form layout="inline" :form="form" :model="searchForm">
        <a-form-item>
            <a-input placeholder="项目名称" v-model="searchForm.projName" :maxLength="100" style="width: 160px" />
        </a-form-item>
        <a-form-item>
            <a-input placeholder="项目编号" v-model="searchForm.projNo" :maxLength="100" style="width: 160px" />
        </a-form-item>
        <a-form-item>
            <base-select v-model="searchForm.reportYear" :title="'年度'" :type="7" :isAll="true" :width="160" />
        </a-form-item>
        <a-form-item>
            <a-button type="primary" icon="search" @click="searchList">搜索</a-button>
            <a-button icon="reload" style="margin-left: 10px" @click="searchFormReset" class="bt-normal">重置</a-button>
            <a-button type="primary" style="margin-left: 10px" @click="FileDownload" icon="download">项目任务书模板下载</a-button>
        </a-form-item>
        <div style="width:100%;margin-bottom: 8px;">
            <a-button type="primary" @click="exportData">导出Excel</a-button>
        </div>
    </a-form>
    <a-table :dataSource="tableData" :columns="columns" rowKey="projId" :pagination="false" :loading="loading"> <!-- :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }" -->
      <template slot="projName" slot-scope="record">
        <a @click="recordClick(record, 'viewProject')">{{record.projName}}</a>
      </template>
      <template slot="testStateName" slot-scope="record">
        <span v-if="record">{{record}}</span>
        <span v-else>未填写</span>
      </template>
      <template slot="option" slot-scope="record">
        <a-button type="primary" size="small" v-if="((!record.testState||record.testState==1))" @click="recordClick(record,'UploadProjTask')">上传项目任务书</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="taskFileEditView" title="上传项目任务书" :width="'60%'" :maskClosable="false" :footer="null" destroyOnClose class="sc_modal">
      <task-file-edit v-model="selectProjId" @close="closeTaskFileEditWindow" />
    </a-modal>
    <s-modal title="项目详情" v-model="visibleView" v-if="visibleView">
      <div slot="content">
        <project-view v-model="selectProjId" @close="() => this.visibleView = false"></project-view>
      </div>
    </s-modal>
  </div>
</template>

<script>
import { isEmptyParams, filterExportExcelData, tableColumnsName, } from "@/views/utils/common";
import { getType } from '@/views/utils/auth'
import baseSelect from '@/views/components/common/baseSelect'
import taskFileEdit from '@/views/report/task/components/taskFileEdit'
import projectView from '@/views/report/project/components/projectView'
import moment from 'moment'
import axios from 'axios'

const columns = [
  { title: "项目名称", scopedSlots: { customRender: 'projName' } },
  { title: "项目编号", dataIndex: "projNo" },
  { title: "项目开始时间", dataIndex: "projStart", tabKey: "2" },
  { title: "项目结束时间", dataIndex: "projEnd", tabKey: "2" },
  { title: "申报年度", dataIndex: "reportYear" },
  //{ title: "状态", dataIndex: "testStateName", scopedSlots: { customRender: "testStateName" }, },
  { title: "操作", fixed: "right", width: "200px", scopedSlots: { customRender: "option" }, align: 'center' },
];

export default {
  name: "projectTask",
  components: { baseSelect, taskFileEdit, projectView, },
  data() {
    return {
      form: this.$form.createForm(this, { name: 'advanced_search' }),
      searchForm: { projName: '', projNo: '', testState: '5', reportYear: null },
      tableData: [],
      columns: columns,
      pagination: {
        pageIndex: 1,
        pageSize: this.$defaultPageSize,
        total: 0,
        pageSizeOptions: this.$defaultPageSizeOptions,
      },
      selectedRowKeys: [],
      loading: false,
      taskFileEditView: false,
      selectProjId: null,
      //selectTaskId: null,
      //selectTestState: null,
      visibleView: false,
      id: null,
      url1: {
        downloadUrl: '/downloadFile/202408091430.doc',
        fileName: '项目任务书模板.doc',
      },
      url2: {
        downloadUrl: '/downloadFile/202408091430.doc',
        fileName: '公立医院高水平临床专科发展科技项目任务书.doc',
      },
      realurl: null,
    };
  },
  created () {
    this.getYear()
    if (getType() == 1)
      this.realurl = this.url1
    else
      this.realurl = this.url2
  },
  methods: {
    getYear () {
      this.$api.batch.getCurrentYearBatch({ type: 1, projType: getType() }).then(({ data = {} }) => {
        if (data) {
          this.searchForm.reportYear = data.year
          this.getListByPage()
        }
      }).catch(() => { })
    },
    getListByPage () {
      this.loading = true
      let pars = isEmptyParams(this.searchForm)
      let par = {
        ...pars,
        pageIndex: this.pagination.pageIndex,
        pageSize: this.pagination.pageSize
      }

      this.$api.taskReport.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.projStart) {
              e.projStart = moment(e.projStart).format('YYYY-MM-DD')
              e.projEnd = moment(e.projEnd).format('YYYY-MM-DD')
            }
          })
        }
      }).catch(() => {
        this.loading = false
      })
    },
    searchList () {
      this.getListByPage()
    },
    change () {
      this.getListByPage()
    },
    showSizeChange (current, pageSize) {
      this.pagination.pageIndex = current
      this.pagination.pageSize = pageSize
      this.getListByPage()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectProjId = selectedRows[0].projId
    //   this.selectTaskId = selectedRows[0].id
    //   this.selectTestState = selectedRows[0].testState
    },
    searchFormReset () {
      this.searchForm = {
        projName: '',
        projNo: '',
        testState: '5',
        reportYear: moment().year(),
      }
    },
    recordClick (record, type) {
      if (type === 'UploadProjTask') {
        this.selectProjId = record.projId
        this.taskFileEditView = true
      }
      if (type === 'viewProject') {
        this.selectProjId = record.projId
        this.visibleView = true
      }
    },
    closeTaskFileEditWindow() {
      this.taskFileEditView = false
    },
    exportData () {
      this.$ToDoExcel(`任务书列表`, tableColumnsName(this.columns), filterExportExcelData(this.columns, this.tableData))
    },
    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()
      })
    },
  }
};
</script>