<template>
  <div :class="$style.personalStatistics">
    <a-card :class="$style.conditions">
      <a-form-model
        ref="projectSearchForm"
        class="ant-advanced-search-form"
        :model="form"
        :rules="rules"
        v-bind="formItemLayout"
      >
        <a-row :gutter="24" :class="$style.antformitem">
          <a-col :span="8">
            <a-form-model-item label="审计机构" prop="unitNames">
              <AuditGroupPicker
                ref="unitNames"
                v-model="form.unitNames"
                :single="false"
                :read-only="false"
                :root-node="rootNode"
              />
            </a-form-model-item>
          </a-col>

          <a-col :span="8">
            <a-form-model-item label="年度" prop="planYear">
              <a-date-picker
                v-model="form.planYear"
                mode="year"
                picker="YYYY"
                format="YYYY"
                :allow-clear="false"
                placeholder="选择年度"
                :input-read-only="true"
                :value="year"
                :open="endOpen"
                @panelChange="yearChange"
                @openChange="handleEndOpenChange"
              ></a-date-picker>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-card>
    <a-card :class="$style.conditions2">
      <div>
        <p :class="$style.tableTitle"> 审计署 </p>
        <a-row :gutter="24" :class="$style.antformitem">
          <a-col :span="8">
            <div :class="$style.buttonContent">
              <a-button :loading="exportLoading" type="primary" @click="exportAuditGroup"
                >生成《单位及内部审计机构基本情况表》</a-button
              >
            </div>
          </a-col>
        </a-row>
        <a-row :gutter="24" :class="$style.antformitem">
          <a-col :span="8">
            <div :class="$style.buttonContent">
              <a-button :loading="exportLoading" type="primary" @click="exportAuditWork"
                >生成《单位内部审计业务情况表》</a-button
              >
            </div>
          </a-col>
        </a-row>
      </div>
    </a-card>

    <a-card :class="$style.conditions3">
      <p :class="$style.tableTitle"> 国资委 </p>
      <a-row :gutter="24" :class="$style.antformitem">
        <a-col :span="8">
          <div :class="$style.buttonContent">
            <a-button
              :loading="exportLoading"
              type="primary"
              @change="processReq"
              @click="handelExport"
              >生成《国资委报表》</a-button
            >
          </div>
        </a-col>
      </a-row>
    </a-card>
  </div>
</template>

<script>
import moment from 'moment'
import components from './_import-components/audit-enterprise-reports-import'
import AuditGroupPicker from '@product/iam/components/picker/audit-group-picker.vue'
import { message } from '@/common/one-ui'
import download from '@/common/services/download'
import axios from '@/common/services/axios-instance'
import { getUserInfo } from '@/common/store-mixin'

export default {
  name: 'AuditEnterpriseReports',
  metaInfo: {
    title: '企业上报报表',
  },
  components: {
    ...components,
    AuditGroupPicker,
  },

  data() {
    return {
      endOpen: false,
      year: null,
      exportLoading: false,
      reqData: {
        unitNames: '',
        planYear: '',
      },
      data: [],
      form: {
        unitNames: [],
        planYear: '',
      },
      rules: {
        unitNames: [{ required: true, message: '请选择审计机构', trigger: 'change' }],
        planYear: [{ required: true, message: '请选择统计时间', trigger: 'change' }],
      },
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 14 },
      },
      rootNode: {},
    }
  },
  created() {
    let userInfo = getUserInfo()
    const params = {
      orgId: userInfo.deptId,
    }

    axios({
      url: 'api/xcoa-mobile/v1/iamorg/getCurrentUserGroup',
      method: 'get',
    }).then((res) => {
      userInfo = res.data
      params.orgId = res.data.id
      axios({
        url: 'api/xcoa-mobile/v1/iamorg/findIamOrg',
        method: 'post',
        params,
      }).then((res) => {
        this.id = res.data.id
        const deptCode = res.data.orgId + ''
        const deptName = res.data.orgName
        this.rootNode = { code: deptCode, name: deptName, id: this.id }
      })
    })
    // this.initGroupPicker()
    // this.tableResize()
  },
  mounted() {
    // this.tableResize()
    this.initGroupPicker()
    this.initDatePicker()
    // 初始化审计类型数据字典信息
    // this.initDictionaryInfo()
  },
  methods: {
    yearChange(value) {
      debugger
      this.year = value
      this.form.planYear = value.format('YYYY')
      if (document.getElementsByClassName('ant-calendar-picker-container').length > 0) {
        document.getElementsByClassName('ant-calendar-picker-container')[0].style.display = 'none'
      }
    },
    handleEndOpenChange(open) {
      this.endOpen = open
    },
    // initGroupPicker() {
    //   debugger
    //   const userInfo = getUserInfo()
    //   const userDeptValue = []
    //   const userDept = {
    //     code: userInfo.deptId + '',
    //     name: JSON.parse(userInfo.attrs.deptLevMap).COMPANY,
    //     text: JSON.parse(userInfo.attrs.deptLevMap).COMPANY,
    //     id: userInfo.deptId,
    //     type: 'Group',
    //   }
    //   userDeptValue.push(userDept)
    //   this.form.unitNames = userDeptValue
    // },
    initGroupPicker() {
      let userInfo = getUserInfo()
      const params = {
        orgId: userInfo.deptId,
      }
      axios({
        url: 'api/xcoa-mobile/v1/iamorg/getCurrentUserGroup',
        method: 'get',
      }).then((res) => {
        userInfo = res.data
        params.orgId = res.data.id
        axios({
          url: 'api/xcoa-mobile/v1/iamorg/findIamOrg',
          method: 'post',
          params,
        }).then((res) => {
          const userDeptValue = []
          const userDept = {
            code: res.data.orgId + '',
            name: res.data.orgName,
            text: res.data.orgName,
            id: res.data.orgId,
            type: 'Group',
          }
          userDeptValue.push(userDept)
          this.form.unitNames = userDeptValue
          this.$refs.projectSearchForm.validate()
        })
      })
    },
    initDatePicker() {
      debugger
      // const datePick = []
      const time = moment(new Date())
      // datePick.push(time)
      this.form.planYear = time.format('YYYY')
    },
    initDictionaryInfo() {
      axios({
        url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=PRODUCT_IAM_AUDIT_TYPE',
        method: 'get',
      }).then((res) => {
        if (res.status === 200) {
          this.auditTypeOptions = res.data
        }
      })
    },
    // tableResize() {
    //   document
    //     .getElementsByClassName('ant-table-empty')[0]
    //     .getElementsByClassName('ant-table-tbody')[0]
    //     .appendChild(document.getElementsByClassName('ant-table-placeholder')[0])
    //   var width = document.getElementsByClassName('ant-table-tbody')[0].offsetWidth
    //   document.getElementsByClassName('ant-table-placeholder')[0].style.width = `${width}%`
    // },
    // dataLoaded(res) {
    //   if (res.data.length) {
    //     res.data.forEach((item, index) => {
    //       item.id = index + 1
    //     })
    //   } else {
    //     this.tableResize()
    //   }
    //   return res
    // },
    processReq(req) {
      req.data = {
        maxResults: req.data.maxResults,
        startPosition: req.data.startPosition,
        unitIds: this.reqData.unitNames,
        planYear: this.reqData.planYear,
      }
      return req
    },
    handelExport() {
      debugger
      if (this.form.unitNames.length === 0 && this.form.planYear.length === 0) {
        this.exportGZW()
      } else {
        // 处理部门
        const arr = []
        this.form.unitNames.forEach((item) => {
          arr.push(`'${item.id}'`)
        })
        this.reqData.unitNames = arr.join(',')
        this.reqData.unitIds = arr.join(',')

        // 处理时间
        this.reqData.planYear = this.form.planYear + ''
        // this.reqData.dateEnd = this.form.timeRange[1].year() + ''
        this.exportGZW()
      }
    },
    exportAuditGroup() {
      if (this.form.unitNames.length === 0 && this.form.planYear.length === 0) {
        this.exportIamOrgExcel()
      } else {
        // 处理时间
        // this.reqData.dateStart = this.form.timeRange[0].year() + ''
        // this.reqData.dateEnd = this.form.timeRange[1].year() + ''
        // 处理部门
        const arr = []
        this.form.unitNames.forEach((item) => {
          arr.push(`'${item.id}'`)
        })
        this.reqData.unitNames = arr.join(',')
        this.reqData.unitIds = arr.join(',')
        // 处理时间
        this.reqData.planYear = this.form.planYear + ''
        this.exportIamOrgExcel()
      }
    },
    exportAuditWork() {
      if (this.form.unitNames.length === 0 && this.form.planYear.length === 0) {
        this.exportIamDwywExcel()
      } else {
        // 处理时间
        // this.reqData.dateStart = this.form.timeRange[0].year() + ''
        // this.reqData.dateEnd = this.form.timeRange[1].year() + ''
        // 处理部门
        const arr = []
        this.form.unitNames.forEach((item) => {
          arr.push(`'${item.id}'`)
        })
        this.reqData.unitNames = arr.join(',')
        this.reqData.unitIds = arr.join(',')
        // 处理时间
        this.reqData.planYear = this.form.planYear + ''
        this.exportIamDwywExcel()
      }
    },
    exportGZW() {
      axios({
        method: 'post',
        url: 'api/xcoa-mobile/v1/iam-auditWork-statistics/exportGZWExcel',
        responseType: 'blob',
        data: {
          unitIds: this.reqData.unitIds,
          planYear: this.reqData.planYear,
        },
      }).then((res) => {
        const url = URL.createObjectURL(res.data)
        download(url, '国资委报表.xls')
      })
    },
    exportIamOrgExcel() {
      axios({
        method: 'post',
        url: 'api/xcoa-mobile/v1/iam-audits-statistics/exportIamOrgExcel',
        responseType: 'blob',
        data: {
          unitIds: this.reqData.unitIds,
          planYear: this.reqData.planYear,
        },
      }).then((res) => {
        const url = URL.createObjectURL(res.data)
        download(url, '单位及内部审计机构基本情况表-审计署.xls')
      })
    },
    exportIamDwywExcel() {
      axios({
        method: 'post',
        url: 'api/xcoa-mobile/v1/iam-audits-statistics/exportIamDwywExcel',
        responseType: 'blob',
        data: {
          unitIds: this.reqData.unitIds,
          planYear: this.reqData.planYear,
        },
      }).then((res) => {
        const url = URL.createObjectURL(res.data)
        download(url, '单位内部审计业务情况表-审计署.xls')
      })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.personalStatistics {
  :global(.ant-table-body) {
    overflow-x: scroll !important;
  }
  :global(.ant-form-item) {
    margin: 0;
  }

  :global(.ant-table-empty) {
    :global(.ant-table-body) {
      overflow-x: scroll !important;
    }
  }
  .conditions {
    height: 100px;
    margin-bottom: 20px;
  }
  .conditions2 {
    height: 300px;
    margin-bottom: 20px;
  }
  .conditions3 {
    height: 200px;
    margin-bottom: 20px;
  }
  .buttonContent {
    margin-right: 500px;
    padding-top: 50px;
    text-align: right;
    button {
      margin: 0 4px;
    }
  }
  :global(.anticon-setting) {
    display: none;
  }
  .tableTitle {
    padding-top: 16px;
    margin: 0;
    font-size: 22px;
    font-weight: bold;
    color: #404040;
    background: white;
  }
  :global(.reporttablecardxm) {
    :global(.ant-table-body) {
      height: auto !important;
      min-height: auto !important;
      overflow: auto;
    }
  }
  :global(.ant-table-placeholder) {
    height: auto !important;
  }
}
</style>
