<template>
  <div :class="$style.personalStatistics">
    <a-card :class="$style.conditions">
      <a-form-model
        ref="advancedSearchForm"
        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="timeRange">
              <AuditRangePicker :time-range.sync="form.timeRange" />
            </a-form-model-item>
          </a-col>
          <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">
            <div :class="$style.buttonContent">
              <a-button @click="handleReset">重置</a-button>
              <a-button type="primary" @click="handleAction('serach')">查询</a-button>
              <a-button :loading="exportLoading" type="primary" @click="handleAction('export')"
                >导出</a-button
              >
            </div>
          </a-col>
        </a-row>
      </a-form-model>
    </a-card>

    <p :class="$style.tableTitle"> 机构基本情况统计 </p>
    <a-card class="reporttablecardxm">
      <sd-data-table
        ref="dataTable"
        :key="dataKey"
        :columns="columns"
        :process-req="processReq"
        :defultpagination-pagesize="50"
        data-url="api/xcoa-mobile/v1/iam-statistics/getOrgStatusList"
        @dataLoaded="dataLoaded"
        @rowClick="rowClick"
      >
      </sd-data-table>
    </a-card>
  </div>
</template>

<script>
import AuditRangePicker from '../../components/picker/audit-range-picker.vue'
import AuditGroupPicker from '../../components/picker/audit-group-picker.vue'
import components from './_import-components/audit-personnel-statistics-import'
import StatisticsService from './statistics-service'
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: 'AuditPersonnelStatistics',
  metaInfo: {
    title: '机构人员统计',
  },
  components: {
    ...components,
    AuditRangePicker,
    AuditGroupPicker,
  },
  data() {
    return {
      exportLoading: false,
      dataKey: 0,
      reqData: {
        dateStart: '',
        dateEnd: '',
        unitNames: '',
      },
      data: [],
      columns: [
        {
          title: '序号',
          dataIndex: 'id',
          width: '66px',
        },
        {
          title: '审计机构',
          dataIndex: 'ORG_NAME',
          width: '400px',
        },
        {
          title: '机构类型',
          dataIndex: 'ORG_TYPE',
          width: '100px',
        },
        {
          title: '编制人数',
          dataIndex: 'PERSON_NUMBER',
          width: '100px',
        },
        {
          title: '专职人数',
          dataIndex: 'SPEC',
          width: '100px',
        },
        {
          title: 'CIA资格人数',
          dataIndex: 'CIA',
          width: '130px',
        },
        {
          title: '学历',
          children: [
            {
              title: '硕士及以上',
              dataIndex: 'MASTER',
              width: '115px',
            },
            {
              title: '大学本科',
              dataIndex: 'BACHELOR',
              width: '100px',
            },
            {
              title: '大学专科及以下',
              dataIndex: 'COLLEGE',
              width: '150px',
            },
          ],
        },
        {
          title: '职称',
          children: [
            {
              title: '高级',
              dataIndex: 'SENIOR',
              width: '100px',
            },
            {
              title: '中级',
              dataIndex: 'INTERMEDIATE',
              width: '100px',
            },
            {
              title: '正高级',
              dataIndex: 'ELEMENTARY',
              width: '100px',
            },
          ],
        },
        {
          title: '知识结构',
          children: [
            {
              title: '审计',
              dataIndex: 'AUDIT',
              width: '100px',
            },
            {
              title: '会计',
              dataIndex: 'ACCOUNTINT',
              width: '100px',
            },
            {
              title: '经济',
              dataIndex: 'ECONOMY',
              width: '100px',
            },
            {
              title: '法律',
              dataIndex: 'LAW',
              width: '100px',
            },
            {
              title: '管理',
              dataIndex: 'MANAGE',
              width: '100px',
            },
            {
              title: '信息技术',
              dataIndex: 'IT',
              width: '100px',
            },
            {
              title: '工程',
              dataIndex: 'ENGINEERING',
              width: '100px',
            },
            {
              title: '其他',
              dataIndex: 'OTHER',
              width: '100px',
            },
          ],
        },
        {
          title: '实际项目数',
          dataIndex: 'REAL_NUM',
          width: '120px',
          sdClickable: true,
        },
        {
          title: '境外项目数',
          dataIndex: 'OUTSIDE_NUM',
          width: '120px',
          sdClickable: true,
        },
        {
          title: '发现问题数',
          dataIndex: 'FIND_NUM',
          width: '120px',
          sdClickable: true,
        },
        {
          title: '需整改问题数',
          dataIndex: 'RECT_NUM',
          width: '135px',
          sdClickable: true,
        },
        {
          title: '已整改问题数',
          dataIndex: 'DONE_NUM',
          width: '135px',
          sdClickable: true,
        },
        {
          title: '整改完成率',
          dataIndex: 'PERCENT',
          width: '120px',
        },
        {
          title: '审计资产金额（万元）',
          dataIndex: 'AUDIT_ASSETS',
          width: '200px',
        },
        // {
        //   title:'整改率',
        //   dataIndex: 'RECT_RATE',
        //   width: '120px'
        // }
      ],
      form: {
        unitNames: [],
        timeRange: [],
      },
      rules: {
        timeRange: [{ required: true, message: '请选择统计时间', trigger: 'change' }],
        unitNames: [{ 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 }
        // const deptCode = userInfo.id.toString()
        // const deptName = res.data.orgName
        // this.rootNode = { code: deptCode, name: deptName, id: this.id }
      })
    })
  },
  mounted() {
    this.tableResize()
  },
  methods: {
    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}%`
    },
    processReq(req) {
      req.data = {
        maxResults: req.data.maxResults,
        startPosition: req.data.startPosition,
        dateStart: this.reqData.dateStart,
        dateEnd: this.reqData.dateEnd,
        unitIds: this.reqData.unitNames,
      }
      return req
    },

    dataLoaded(res) {
      if (res.data.length) {
        res.data.forEach((item, index) => {
          item.id = index + 1
        })
      } else {
        this.tableResize()
      }
      return res
    },
    handleReset() {
      this.$refs.advancedSearchForm.resetFields()
    },
    handleAction(type) {
      this.$refs.advancedSearchForm.validate((valid, values) => {
        if (valid) {
          // 处理时间
          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(',')
          // 调用方法
          switch (type) {
            case 'serach':
              this.handleSearch()
              break
            case 'export':
              this.handleExport()
              break
          }
        }
      })
    },
    handleSearch() {
      this.reqData.startPosition = 0
      this.dataKey++
    },
    handleExport() {
      if (this.$refs.dataTable.data.length === 0) {
        message.warning('未查询出可导出数据', 1)
        return
      }
      this.exportLoading = true
      StatisticsService.exportPersonalStatisticsList(this.reqData).then((data) => {
        const url = URL.createObjectURL(data)
        download(url, '机构人员统计表.xls')
        this.exportLoading = false
      })
    },
    rowClick(record, { rowIndex, column }) {
      const param = {
        dateStart: this.reqData.dateStart,
        dateEnd: this.reqData.dateEnd,
        unitIds: "'" + record.ORG_ID + "'",
      }
      let url = ''
      if (column.dataIndex === 'REAL_NUM' || column.dataIndex === 'OUTSIDE_NUM') {
        if (column.dataIndex === 'REAL_NUM') {
          param.columnValue = 'SJXMS'
        } else {
          param.columnValue = 'JWXMS'
        }
        // 项目列表 实际 和境外
        url = '#/audit-annualplancompletion-project-list?params='
      } else if (column.dataIndex === 'FIND_NUM') {
        // 发现列表
        url = '#/audit-problemamount-find-list?params='
      } else {
        // 整改列表
        if (column.dataIndex === 'DONE_NUM') {
          param.rectStatus = '03'
        }
        param.rectUnitFlag = 'true'
        url = '#/audit-statistics-find-list?params='
      }
      url = url + encodeURIComponent(JSON.stringify(param)) + '&type=personnelstatistics'
      window.open(url)
    },
  },
}
</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 {
    margin-bottom: 20px;
  }
  .buttonContent {
    padding-top: 6px;
    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;
    text-align: center;
    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>
