<template>
  <div>
    <a-card :class="$style.surveyCard">
      <a-tabs v-model="activeKey" type="line" @tabClick="getTabsKey($event)">
        <a-tab-pane key="1" tab="审计调查">
          <audit-advanced-query
            :expand="expand"
            :search-data="surveyFormData"
            :ref-name="searchform"
            :search-style="{ height: '130px', left: '20px', top: '40px' }"
            :search-fun="handleSearch"
            @searchedClick="searchedClick"
          >
            <template>
              <a-col :span="12">
                <a-form-model-item :label="'文件标题'" prop="title">
                  <a-input v-model="surveyFormData.title" allow-clear />
                </a-form-model-item>
              </a-col>

              <a-col :span="12">
                <a-form-model-item :label="'编制人员'" prop="drawUpName">
                  <a-input v-model="surveyFormData.drawUpName" allow-clear />
                </a-form-model-item>
              </a-col>
            </template>
          </audit-advanced-query>
          <sd-data-table-ex
            v-if="tabi === '1'"
            ref="iamAuditSurveyTable"
            form-id="iamAuditSurvey"
            page-id="audit/survey/iamAuditSurvey"
            data-url="api/xcoa-mobile/v1/iamauditsurvey/survey-list"
            :columns="surveyColumns"
            :filter-expressions="surveyExpressions"
            :actions="actions"
            show-selection
            :show-advance-query="true"
            :search-fields="['title', 'code', 'drawUpName']"
            @searchbtnClick="searchbtnClick"
          >
            <div slot="islink" slot-scope="text, record">
              <a @click="rowClick(record)">{{ text }}</a>
            </div>
          </sd-data-table-ex>
        </a-tab-pane>
        <a-tab-pane key="2" tab="查询历史审计发现" force-render>
          <audit-advanced-query
            :expand="expand"
            :search-data="historyFindFormData"
            :ref-name="searchform"
            :search-style="{ height: '170px', left: '40px', top: '60px' }"
            :search-fun="historyFindSearch"
            @searchedClick="searchedClick"
            @resetForm="findResetForm"
          >
            <template>
              <a-col :span="12">
                <a-form-model-item :label="'查询\u2003区间'">
                  <a-range-picker
                    :mode="['month', 'month']"
                    :value="findValue"
                    format="YYYY-MM"
                    @panelChange="findPanelChange"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item :label="'审计发现标题'" prop="findTitle">
                  <a-input v-model="historyFindFormData.findTitle" allow-clear />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item :label="'被审计单位'" prop="auditedUnitNames">
                  <a-input v-model="historyFindFormData.auditedUnitNames" allow-clear />
                </a-form-model-item>
              </a-col>
            </template>
          </audit-advanced-query>
          <sd-data-table-ex
            v-if="tabi === '2'"
            ref="historyAuditFindTable"
            :columns="historyAuditFindColumns"
            data-url="api/xcoa-mobile/v1/iamauditsurvey/history-audit-find"
            :filter-expressions="historyAuditFindExpressions"
            form-id=""
            page-id=""
            :projectlist="true"
            :actions="actions"
            :show-advance-query="true"
            :search-fields="['findTitle']"
            :class="$style.sjfxTable"
            @searchbtnClick="searchbtnClick"
          >
            <template slot="findType" slot-scope="text">
              {{ JSON.parse(text).text }}
            </template>
          </sd-data-table-ex>
        </a-tab-pane>
        <a-tab-pane key="3" tab="查询历史审计报告" force-render>
          <audit-advanced-query
            :expand="expand"
            :search-data="historyReportFormData"
            :ref-name="searchform"
            :search-style="{ height: '170px', left: '40px', top: '60px' }"
            :search-fun="historyReportSearch"
            @searchedClick="searchedClick"
            @resetForm="reportResetForm"
          >
            <template>
              <a-col :span="12">
                <a-form-model-item :label="'查询\u2003区间'">
                  <a-range-picker
                    :mode="['month', 'month']"
                    :value="reportValue"
                    format="YYYY-MM"
                    @panelChange="reportPanelChange"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item :label="'报告标题'" prop="fileTitle">
                  <a-input v-model="historyReportFormData.fileTitle" allow-clear />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item :label="'被审计单位'" prop="auditedUnitNames">
                  <a-input v-model="historyReportFormData.auditedUnitNames" allow-clear />
                </a-form-model-item>
              </a-col>
            </template>
          </audit-advanced-query>
          <sd-data-table-ex
            v-if="tabi === '3'"
            ref="historyAuditReportTable"
            :columns="historyAuditReportColumns"
            :filter-expressions="historyAuditReportExpressions"
            data-url="api/xcoa-mobile/v1/iamauditsurvey/history-audit-report"
            form-id=""
            page-id=""
            :actions="actions"
            :show-advance-query="true"
            :search-fields="['fileTitle']"
            @searchbtnClick="searchbtnClick"
          >
            <div slot="islinktoAuditReport" slot-scope="text, record">
              <a :title="text" @click="rowClicktoAuditReport(record)">{{ text }}</a>
            </div>
          </sd-data-table-ex>
        </a-tab-pane>
      </a-tabs>
    </a-card>
  </div>
</template>

<script>
import { getUserInfo } from '@/common/store-mixin'
import moment from 'moment'
import axios from '@/common/services/axios-instance'
import TableColumnTypes from '@/common/services/table-column-types'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import TableActionTypes from '@/common/services/table-action-types'
import auditAdvancedQuery from '../../components/audit-advanced-query.vue'
import auditAdvancedQueryMixins from '../../components/audit-advanced-query-mixins'
import components from './_import-components/iam-work-survey-list-import'

const surveyColumns = [
  {
    title: '序号',
    dataIndex: 'sortNumber',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    dataIndex: 'id',
    sdHidden: true,
  },
  {
    title: '文件标题',
    dataIndex: 'title',
    // sdClickable: true, // 列内容是否可点击
    scopedSlots: { customRender: 'islink' },
  },
  {
    title: '文件编号',
    dataIndex: 'code',
    width: '20%',
  },
  {
    title: '编制人员',
    width: '10%',
    dataIndex: 'drawUpName',
  },
  {
    title: '编制日期',
    dataIndex: 'drawUpTime',
    width: '10%',
    sorter: true,
    defaultSortOrder: 'descend',
    sdRender: TableColumnTypes.date,
  },
  {
    title: '文档状态',
    dataIndex: 'flileStatus',
    width: '10%',
    sorter: true,
  },
]
const historyAuditFindColumns = [
  {
    title: '序号',
    dataIndex: 'sortNumber',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '审计机构',
    dataIndex: 'unitName',
    width: '100px',
  },
  {
    title: '项目名称',
    dataIndex: 'projectTitle',
    width: '100px',
  },
  {
    title: '项目编号',
    dataIndex: 'projectCode',
    width: '100px',
  },
  {
    title: '项目组长',
    dataIndex: 'groupLeaderName',
    width: '100px',
  },
  {
    title: '被审计单位',
    dataIndex: 'auditedUnitNames',
    width: '115px',
  },
  {
    title: '审计发现提出人',
    dataIndex: 'findProposer',
    width: '144px',
  },
  {
    title: '审计发现标题',
    dataIndex: 'findTitle',
    width: '128px',
    // sdClickable: true, // 列内容是否可点击
    scopedSlots: { customRender: 'islink' },
  },
  {
    title: '审计发现编号',
    dataIndex: 'findCode',
    width: '128px',
  },
  {
    title: '审计发现类别',
    dataIndex: 'findType',
    scopedSlots: { customRender: 'findType' },
    width: '128px',
  },
  {
    title: '审计建议类型',
    dataIndex: 'suggestionType',
    width: '128px',
  },
  {
    title: '问题类型',
    dataIndex: 'problemType',
    width: '100px',
  },
  {
    title: '问题涉及金额(万元)',
    dataIndex: 'amount',
    width: '180px',
  },
  {
    title: '风险评级',
    dataIndex: 'riskLevel',
    width: '100px',
  },
  {
    title: '问题所属领域',
    dataIndex: 'businessProcess',
    width: '128px',
  },
  {
    title: '是否问责',
    dataIndex: 'whetherOrderDuty',
    width: '120px',
  },
]
const historyAuditReportColumns = [
  {
    title: '序号',
    dataIndex: 'sortNumber',
    width: '80px',
    customRender: (text, record, index) => `${index + 1}`,
  },
  {
    title: '报告标题',
    dataIndex: 'fileTitle',
    scopedSlots: { customRender: 'islinktoAuditReport' },
  },
  {
    title: '审计机构',
    dataIndex: 'unitName',
  },
  {
    title: '被审计单位',
    dataIndex: 'auditedUnitNames',
  },
  {
    title: '审计类型',
    width: '10%',
    dataIndex: 'auditType',
  },

  {
    title: '项目名称',
    dataIndex: 'projectTitle',
  },

  {
    title: '项目编号',
    dataIndex: 'projectCode',
  },
  {
    title: '报告文号',
    dataIndex: 'reportNumber',
  },
]
export default {
  name: 'IamWorkSurveyList',
  metaInfo: {
    title: '审计调查',
  },
  components: { ...components, auditAdvancedQuery },
  mixins: [auditAdvancedQueryMixins],
  data() {
    return {
      activeKey: '1',
      findValue: [],
      reportValue: [],
      projectId: this.$root.$route.query.projectId,
      surveyColumns,
      historyAuditFindColumns,
      historyAuditReportColumns,
      // expand: false,
      searchform: 'searchform',
      surveyFormData: {
        title: '',
        drawUpName: '',
      },
      historyFindFormData: {
        findTitle: '',
        auditedUnitNames: '',
        startTime: null,
        endTime: null,
      },
      historyReportFormData: {
        fileTitle: '',
        auditedUnitNames: '',
        startTime: null,
        endTime: null,
      },
      surveyExpressions: [
        {
          dataType: 'str',
          name: 'projectId',
          op: 'eq',
          stringValue: this.$root.$route.query.projectId,
        },
      ],
      historyAuditFindExpressions: [
        {
          dataType: 'str',
          name: 'projectId',
          op: 'eq',
          stringValue: this.$root.$route.query.projectId,
        },
      ],
      historyAuditReportExpressions: [
        {
          dataType: 'str',
          name: 'projectId',
          op: 'eq',
          stringValue: this.$root.$route.query.projectId,
        },
      ],
      // actions: [
      //   {
      //     label: '新建',
      //     id: 'new',
      //     permission: 'create',
      //     type: TableActionTypes.primary, // 新建按钮，不需要回调，自动处理
      //     callback: () => {
      //       const url = '/iam-work-survey-form?projectId=' + this.$root.$route.query.projectId // 新页面要打开的路由地址
      //       crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
      //         if (refreshFlag) {
      //           // 这里写或者调刷新的方法
      //           this.refresh()
      //         }
      //       })
      //     },
      //   },
      //   {
      //     label: '删除',
      //     id: 'delete',
      //     type: TableActionTypes.oa.delete, // 删除按钮，不需要回调，会自动处理
      //   },
      // ],
      actions: [],
      tabi: '1',
    }
  },
  mounted() {
    this.isShowBtn()
  },
  methods: {
    // 查看当前登录人是否项目成员，如果同时是项目阅读人员，取最大权限（项目成员权限）
    inProjectUser(userList) {
      var isProjectUser = false
      userList.map((item) => {
        if (item.userAccount === getUserInfo().account) {
          isProjectUser = true
          return isProjectUser
        }
      })
      return isProjectUser
    },
    // 是否显示按钮 禅道bug21826需要，关闭项目后各阶段都不能再新建（包括其他操作按钮）
    isShowBtn() {
      const projectId = this.$root.$route.query.projectId
      axios({
        url: `api/xcoa-mobile/v1/iamauditproject/getProjectInfoById?id=` + projectId,
        method: 'get',
      }).then((res) => {
        if (res.data) {
          const itemStatus = res.data.itemStatus
          const readAuthorityCodes = res.data.readAuthorityCodes
          const userList = res.data.iamProjectUserList
          var isProjectUser = this.inProjectUser(userList)
          if (['05', '06', '07'].includes(itemStatus)) {
            this.actions = []
          } else {
            // this.actions = [
            //   {
            //     label: '新建',
            //     id: 'new',
            //     permission: 'create',
            //     type: TableActionTypes.primary, // 新建按钮，不需要回调，自动处理
            //     callback: () => {
            //       const url = '/iam-work-survey-form?projectId=' + this.$root.$route.query.projectId // 新页面要打开的路由地址
            //       crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
            //         if (refreshFlag) {
            //           // 这里写或者调刷新的方法
            //           this.refresh()
            //         }
            //       })
            //     },
            //   },
            //   {
            //     label: '删除',
            //     id: 'delete',
            //     type: TableActionTypes.oa.delete, // 删除按钮，不需要回调，会自动处理
            //   },
            // ]
            if (
              readAuthorityCodes != null &&
              readAuthorityCodes.indexOf(getUserInfo().account) > -1 &&
              !isProjectUser
            ) {
              this.actions = []
            } else {
              this.actions = [
                {
                  label: '新建',
                  id: 'new',
                  permission: 'create',
                  type: TableActionTypes.primary, // 新建按钮，不需要回调，自动处理
                  callback: () => {
                    const url =
                      '/iam-work-survey-form?projectId=' + this.$root.$route.query.projectId // 新页面要打开的路由地址
                    crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
                      if (refreshFlag) {
                        // 这里写或者调刷新的方法
                        this.refresh()
                      }
                    })
                  },
                },
                {
                  label: '删除',
                  id: 'delete',
                  type: TableActionTypes.oa.delete, // 删除按钮，不需要回调，会自动处理
                },
              ]
            }
          }
        }
      })
    },
    getTabsKey(event) {
      if (event != null) {
        this.tabi = event
      }
    },
    moment,
    reportPanelChange(value, mode) {
      this.reportValue = value
      this.historyReportFormData.startTime = value[0].valueOf()
      this.historyReportFormData.endTime = value[1].valueOf()
      this.mode2 = [mode[0] === 'date' ? 'month' : mode[0], mode[1] === 'date' ? 'month' : mode[1]]
    },
    findPanelChange(value, mode) {
      this.findValue = value
      this.historyFindFormData.startTime = value[0].valueOf()
      this.historyFindFormData.endTime = value[1].valueOf()
      this.mode2 = [mode[0] === 'date' ? 'month' : mode[0], mode[1] === 'date' ? 'month' : mode[1]]
    },
    rowClick(record) {
      const url = '/iam-work-survey-form?record=' + record.id + '&projectId=' + this.projectId // 新页面要打开的路由地址
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.refresh()
        }
      })
    },
    // 打开新页面查看历史审计报告
    rowClicktoAuditReport(record) {
      const url = '/sd-webflow/done-pages/' + record.instId // 新页面要打开的路由地址
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.refresh()
        }
      })
    },
    refresh(toFirstPage) {
      return this.$refs.iamAuditSurveyTable.refresh(toFirstPage)
    },
    reportResetForm() {
      this.reportValue = []
      this.historyReportFormData.startTime = null
      this.historyReportFormData.endTime = null
    },

    findResetForm() {
      this.findValue = []
      this.historyFindFormData.startTime = null
      this.historyFindFormData.endTime = null
    },
    // searchbtnClick() {
    //   this.expand = !this.expand
    // },
    handleSearch() {
      this.surveyExpressions = []
      this.surveyExpressions.push({
        dataType: 'str',
        name: 'projectId',
        op: 'eq',
        stringValue: this.$root.$route.query.projectId,
      })
      if (this.surveyFormData.title) {
        this.surveyExpressions.push({
          dataType: 'str',
          name: 'title',
          op: 'like',
          stringValue: `%${this.surveyFormData.title}%`,
        })
      }
      // 住所
      if (this.surveyFormData.drawUpName) {
        this.surveyExpressions.push({
          dataType: 'str',
          name: 'drawUpName',
          op: 'like',
          stringValue: `%${this.surveyFormData.drawUpName}%`,
        })
      }
      // this.expand = false
    },
    historyFindSearch() {
      this.historyAuditFindExpressions = []
      this.historyAuditFindExpressions.push({
        dataType: 'str',
        name: 'projectId',
        op: 'eq',
        stringValue: this.$root.$route.query.projectId,
      })
      if (this.historyFindFormData.startTime) {
        this.historyAuditFindExpressions.push({
          dataType: 'long',
          name: 'startTime',
          op: 'ge',
          longValue: this.historyFindFormData.startTime,
        })
      }
      if (this.historyFindFormData.endTime) {
        this.historyAuditFindExpressions.push({
          dataType: 'long',
          name: 'endTime',
          op: 'le',
          longValue: this.historyFindFormData.endTime,
        })
      }
      // 审计发现标题
      if (this.historyFindFormData.findTitle) {
        this.historyAuditFindExpressions.push({
          dataType: 'str',
          name: 'findTitle',
          op: 'like',
          stringValue: `%${this.historyFindFormData.findTitle}%`,
        })
      }
      // 被审计单位
      if (this.historyFindFormData.auditedUnitNames) {
        this.historyAuditFindExpressions.push({
          dataType: 'str',
          name: 'auditedUnitNames',
          op: 'like',
          stringValue: `%${this.historyFindFormData.auditedUnitNames}%`,
        })
      }
      //  this.expand = false
    },
    historyReportSearch() {
      this.historyAuditReportExpressions = []
      this.historyAuditReportExpressions.push({
        dataType: 'str',
        name: 'projectId',
        op: 'eq',
        stringValue: this.$root.$route.query.projectId,
      })

      if (this.historyReportFormData.startTime) {
        this.historyAuditReportExpressions.push({
          dataType: 'long',
          name: 'startTime',
          op: 'ge',
          longValue: this.historyReportFormData.startTime,
        })
      }
      if (this.historyReportFormData.endTime) {
        this.historyAuditReportExpressions.push({
          dataType: 'long',
          name: 'endTime',
          op: 'le',
          longValue: this.historyReportFormData.endTime,
        })
      }
      // 审计发现标题
      if (this.historyReportFormData.fileTitle) {
        this.historyAuditReportExpressions.push({
          dataType: 'str',
          name: 'fileTitle',
          op: 'like',
          stringValue: `%${this.historyReportFormData.fileTitle}%`,
        })
      }
      // 被审计单位
      if (this.historyReportFormData.auditedUnitNames) {
        this.historyAuditReportExpressions.push({
          dataType: 'str',
          name: 'auditedUnitNames',
          op: 'like',
          stringValue: `%${this.historyReportFormData.auditedUnitNames}%`,
        })
      }
      //   this.expand = false
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.sjfxTable {
  :global .ant-table-column-title {
    white-space: normal;
  }
  :global(.ant-table-body) {
    overflow-x: scroll !important;
  }

  :global(.ant-table-empty) {
    :global(.ant-table-body) {
      overflow-x: scroll !important;
    }
  }
}
.survey-card {
  margin-top: -7px;
}
</style>
