<template>
  <div class="page">
    <el-form :inline="true" class="query-form m-b-10" v-if="searchVisible" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
      <!-- 搜索框-->
      <el-form-item label="项目名称" prop="cwProjectRecordsDTO.projectName">
        <el-input v-model="searchForm.cwProjectRecordsDTO.projectName" placeholder="请输入项目名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="项目经理" prop="cwProjectRecordsDTO.projectMasterId">
        <SelectUserTree
          ref="companyTree"
          :props="{
                  value: 'id',             // ID字段名
                  label: 'name',         // 显示名称
                  children: 'children'    // 子级字段名
                }"
          :url="`/system-server/sys/user/treeUserDataAllOffice?type=2`"
          :value="searchForm.cwProjectRecordsDTO.projectMasterId"
          :clearable="true"
          :accordion="true"
		  size="default"
          @getValue="(value) => {searchForm.cwProjectRecordsDTO.projectMasterId=value}"/>
      </el-form-item>
      <el-form-item v-if="showHideItem" label="案卷号" prop="number">
        <el-input v-model="searchForm.number" placeholder="请输入案卷号" clearable></el-input>
      </el-form-item>

      <el-form-item v-if="showHideItem" label="报告文号" prop="reportNo">
        <el-input v-model="searchForm.reportNo" placeholder="请输入报告文号" clearable></el-input>
      </el-form-item>

      <el-form-item v-if="showHideItem" label="报告时间" prop="reportDates">
        <el-date-picker
          placement="bottom-start"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
          v-model="searchForm.reportDates"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          clearable>
        </el-date-picker>
      </el-form-item>

      <el-form-item v-if="showHideItem" label="归档时间" prop="auditDates">
        <el-date-picker
          placement="bottom-start"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
          v-model="searchForm.auditDates"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          clearable>
        </el-date-picker>
      </el-form-item>


      <el-form-item v-if="showHideItem" label="归档状态" prop="status">
        <el-select v-model="searchForm.status" placeholder="请选择归档状态" clearable style="width: 100%">
          <el-option-group
            v-for="(type,index) in typeDictList"
            :key="type.key"
            :label="type.label">
            <el-option
              v-for="item in typeDictList[index].childrenList"
              :key="item.key"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-option-group>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="default" @click="showHide" :icon="showHideIcon">{{showHideName}}</el-button>
        <el-button type="primary" @click="refreshList()" icon="el-icon-search">查询</el-button>
        <el-button @click="resetSearch()" icon="el-icon-refresh-right">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="jp-table top" style="">
      <vxe-toolbar :refresh="{query: refreshList}" ref="toolbarRef" export print custom>
		  <template #tools>
			  <vxe-button
				  text
				  type="primary"
				  :title="searchVisible ? '收起检索' : '展开检索'"
				  icon="vxe-icon-search"
				  class="tool-btn"
				  @click="searchVisible = !searchVisible"
			  ></vxe-button>
		  </template>
      </vxe-toolbar>
      <div style="height: calc(100% - 90px)">
        <vxe-table
          border="inner"
          auto-resize
          resizable
          height="auto"
          :loading="loading"
          ref="projectTable"
          show-header-overflow
          show-overflow
          highlight-hover-row
          :print-config="{}"
          :export-config="{
                    remote: true,
                    filename: `归档信息${moment(new Date()).format('YYYY-MM-DD')}`,
                    sheetName: '归档信息',
                    exportMethod: exportMethod,
                    types: ['xlsx'],
                    modes: ['current', 'selected', 'all']
                  }"
          :menu-config="{}"
          @sort-change="sortChangeHandle"
          :sort-config="{remote:true}"
          :data="dataList"
          :checkbox-config="{}">
          <vxe-column type="seq" width="60" title="序号"></vxe-column>
          <vxe-column min-width="230" align="center" title="项目名称" field="cwProjectRecordsDTO.projectName">
            <template #default="scope">
              <el-link  type="primary" :underline="false" v-if="hasPermission('cwProjectRecords:view')" @click="viewProject(scope.row.projectId)">{{commonJS.isNotEmpty(scope.row.cwProjectRecordsDTO)?scope.row.cwProjectRecordsDTO.projectName:''}}</el-link>
              <span v-else>{{commonJS.isNotEmpty(scope.row.cwProjectRecordsDTO)?scope.row.cwProjectRecordsDTO.projectName:''}}</span>
            </template>
          </vxe-column>
          <vxe-column min-width="150" align="center" title="项目经理" field="cwProjectRecordsDTO.projectMasterName"></vxe-column>
          <vxe-column min-width="230" align="center" title="报告文号" field="reportNo"></vxe-column>
          <vxe-column min-width="150" align="center" title="报告日期" field="reportDate"></vxe-column>
          <vxe-column min-width="150" align="center" title="归档日期" field="auditDate"></vxe-column>
          <vxe-column min-width="150" align="center" title="归档状态" field="status">
            <template #default="scope">
              <span style="color:#5bc0de; font-weight: bold;" v-if="scope.row.status === '暂时未归档'">暂时未归档</span>
              <span style="color:#de6764; font-weight: bold;" v-if="scope.row.status === '超期未归档'">超期未归档</span>
              <span style="color:#5cb85c; font-weight: bold;" v-if="scope.row.status === '已按时归档'">已按时归档</span>
              <span style="color:#f0ad4e; font-weight: bold;" v-if="scope.row.status === '已超期归档'">已超期归档</span>
            </template>
          </vxe-column>
          <vxe-column min-width="150" align="center" title="超期（天）" field="overdueDay">
            <template #default="scope">
              <span style="color:#5bc0de; font-weight: bold;" v-if="scope.row.status === '暂时未归档'">{{scope.row.overdueDay}}</span>
              <span style="color:#de6764; font-weight: bold;" v-if="scope.row.status === '超期未归档'">{{scope.row.overdueDay}}</span>
              <span style="color:#5cb85c; font-weight: bold;" v-if="scope.row.status === '已按时归档'">{{scope.row.overdueDay}}</span>
              <span style="color:#f0ad4e; font-weight: bold;" v-if="scope.row.status === '已超期归档'">{{scope.row.overdueDay}}</span>
            </template></vxe-column>
          <vxe-column min-width="150" align="center" title="罚款金额（元）" field="fineMoney"></vxe-column>
          <vxe-column min-width="150" align="center" title="签字注师1" field="signatureAnnotatorName1"></vxe-column>
          <vxe-column min-width="150" align="center" title="签字注师2" field="signatureAnnotatorName2"></vxe-column>
          <vxe-column min-width="230" align="center" title="案卷号" field="number"></vxe-column>
        </vxe-table>
        <vxe-pager
          background
          :current-page="tablePage.currentPage"
          :page-size="tablePage.pageSize"
          :total="tablePage.total"
          :page-sizes="[10, 20, 100, 1000, {label: '全量数据', value: 1000000}]"
          :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
          @page-change="currentChangeHandle">
        </vxe-pager>
      </div>
      <ProjectRecordsForm ref="projectRecordsForm"></ProjectRecordsForm>
      <ProjectReportArchiveForm ref="projectReportArchiveForm"></ProjectReportArchiveForm>
    </div>
  </div>
</template>

<script>
  import ProjectReportArchiveDetailService from '@/api/cw/projectRecords/ProjectReportArchiveDetailService'
  import userService from '@/api/sys/UserService'
  import ProjectRecordsForm from '../projectRecords/ProjectRecordsForm'
  import ProjectReportArchiveForm from './ProjectReportArchiveForm'
  import SelectUserTree from '@/views/utils/treeUserSelect'
  import XEUtils from 'xe-utils'
  export default {
    data () {
      return {
		searchVisible: true,
        showHideItem: false,
        showHideIcon: 'el-icon-arrow-down',
        showHideName: '展示',
        searchForm: {
          name: '',
          cwProjectRecordsDTO: {
            id: '',
            projectName: '',
            projectMasterId: '',
            projectMasterName: ''
          },
          year: '',
          number: '',
          auditDates: [],
          status: '',
          reportDates: [],
          reportNo: '',
          fileNumber: ''
        },
        dataList: [],
        tablePage: {
          total: 0,
          currentPage: 1,
          pageSize: 10,
          orders: []
        },
        tableKey: '',
        loading: false,
        processDefinitionAuditId: '',
        procDefAuditKey: '',
        isAdmin: false,
        typeDictList: [
          {
            key: '已归档',
            label: '已归档',
            childrenList: [
              {
                key: '已按时归档',
                value: '已按时归档',
                label: '已按时归档'
              },
              {
                key: '已超期归档',
                value: '已超期归档',
                label: '已超期归档'
              }
            ]
          }, {
            key: '未归档',
            label: '未归档',
            childrenList: [
              {
                key: '暂时未归档',
                value: '暂时未归档',
                label: '暂时未归档'
              },
              {
                key: '超期未归档',
                value: '超期未归档',
                label: '超期未归档'
              }
            ]
          }
        ]
      }
    },
    projectReportArchiveDetailService: null,
    created () {
      this.projectReportArchiveDetailService = new ProjectReportArchiveDetailService()
    },
    components: {
      ProjectRecordsForm,
      ProjectReportArchiveForm,
      SelectUserTree
    },
    computed: {
      userName () {
        return JSON.parse(localStorage.getItem('user')).name
      },
      user () {
        this.createName = JSON.parse(localStorage.getItem('user')).name
        return JSON.parse(localStorage.getItem('user'))
      }
    },
    mounted () {
		this.$nextTick(() => {
			//将表格和工具栏进行关联
			const $table = this.$refs.projectTable;
			const $toolbar = this.$refs.toolbarRef;
			$table.connect($toolbar);
		})
      this.refreshList()
    },
    activated () {
      this.refreshList()
    },
    methods: {
      showHide () {
        if (this.showHideItem === false) {
          this.showHideItem = true
          this.showHideIcon = 'el-icon-arrow-up'
          this.showHideName = '隐藏'
        } else {
          this.showHideItem = false
          this.showHideIcon = 'el-icon-arrow-down'
          this.showHideName = '展示'
        }
      },
      formatDate ({ cellValue }) {
        return XEUtils.toDateString(cellValue, 'YYYY-MM-DD')
      },
      // 查看
      view (id) {
        this.$refs.projectReportArchiveForm.init('view', id)
      },
      viewProject (id) {
        this.$refs.projectRecordsForm.init('view', id)
      },
      // 查询当前用户是否是管理员用户
      checkIsAdmin () {
        userService.is().then((data) => {
          this.isAdmin = data
        })
      },
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.projectReportArchiveDetailService.list({
          'current': this.tablePage.currentPage,
          'size': this.tablePage.pageSize,
          'orders': this.tablePage.orders,
          ...this.searchForm
        }).then((data) => {
          this.dataList = data.records
          this.tablePage.total = data.total
          this.tableKey = Math.random()
          this.loading = false
        })
      },
      // 当前页
      currentChangeHandle ({ currentPage, pageSize }) {
        this.tablePage.currentPage = currentPage
        this.tablePage.pageSize = pageSize
        this.refreshList()
      },
      // 排序
      sortChangeHandle (column) {
        this.tablePage.orders = []
        if (column.order != null) {
          this.tablePage.orders.push({column: this.$utils.toLine(column.property), asc: column.order === 'asc'})
        }
        this.refreshList()
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      },
      // 自定义服务端导出
      exportMethod ({ options }) {
        // 传给服务端的参数
        const params = {
          'current': this.tablePage.currentPage,
          'size': this.tablePage.pageSize,
          'orders': this.tablePage.orders,
          ...this.searchForm,
          filename: options.filename,
          sheetName: options.sheetName,
          isHeader: options.isHeader,
          original: options.original,
          mode: options.mode,
          selectIds: options.mode === 'selected' ? options.map(item => item.id) : [],
          exportFields: options.columns.map(column => column.property)
        }
        return this.projectReportArchiveDetailService.exportExcel(params).then((res) => {
          // 将二进制流文件写入excel表，以下为重要步骤
          this.$utils.downloadExcel(res, options.filename)
        }).catch(function (err) {
          if (err.response) {
            console.log(err.response)
          }
        })
      }
    }
  }
</script>
