<template>
  <div :class="$style.searchdiv">
    <a-card :class="$style.searchWrap">
      <a-form-model
        ref="advancedSearchForm"
        :class="$style.antAdvancedSearchForm"
        :model="form"
        v-bind="formItemLayout"
        layout="horizontal"
      >
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-model-item
              :label="'年度'"
              prop="year"
              :rules="[{ required: true, message: '请选择年度' }]"
            >
              <!-- <a-space direction="vertical" :size="12"> -->
              <AudiitRangePicker :time-range.sync="form.year"></AudiitRangePicker>
              <!-- </a-space> -->
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item
              :label="'审计机构'"
              prop="units"
              :rules="[{ required: true, message: '请选择审计机构' }]"
            >
              <AuditGroupPicker
                v-model="form.units"
                :single="false"
                :read-only="false"
                :root-node="rootNode"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="项目名称">
              <a-input v-model="form.projectName"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-model-item label="被审计单位">
              <sd-group-picker v-model="form.auditedUnits" :single="false" :read-only="false" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="是否问责">
              <a-select v-model="form.whetherOrderDuty" :allow-clear="true" :placeholder="'请选择'">
                <a-select-option value="0">
                  是
                </a-select-option>
                <a-select-option value="1">
                  否
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="审计发现标题">
              <a-input v-model="form.findTitle"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-model-item label="审计发现类别">
              <a-input v-model="form.findType"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="问题类型">
              <a-select v-model="form.problemType" :allow-clear="true" :placeholder="'请选择'">
                <a-select-option
                  v-for="(item, index) in problemOptions"
                  :key="index"
                  :value="item.id"
                >
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="风险评级">
              <a-select v-model="form.riskLevel" :allow-clear="true" :placeholder="'请选择'">
                <a-select-option
                  v-for="(risk, index) in riskOptions"
                  :key="index"
                  :value="risk.id"
                  >{{ risk.name }}</a-select-option
                >
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="16"> </a-col>
          <a-col :class="$style.searchbutton" :span="8">
            <div class="reportbuttonContent" style="margin-right:15%">
              <a-button @click="handleReset">
                重置
              </a-button>
              <a-button type="primary" @click="handleSearch">
                查询
              </a-button>
              <a-button type="primary" @click="exportForm">知识图谱</a-button>
              <audit-advanced-export
                ref="export"
                :class-style="[$style.buttonSpacing]"
                :v-if="false"
                :config-id="91"
                :exclebxh="false"
                :expressions="expressions"
              ></audit-advanced-export>
            </div>
          </a-col>
        </a-row>
      </a-form-model>
      <!-- <findReportSearch :search="search" :expressions="expressions"> </findReportSearch> -->
    </a-card>
    <a-card class="reporttablecardxm">
      <span class="header_sd-header_common"
        ><span :class="['toptitle', $style.toptitle]">审计问题查询</span></span
      >
      <sd-data-table
        ref="sjzlTable"
        :row-key="(record, index) => index"
        :filter-expressions="expressions"
        :projectlist="true"
        :columns="columns"
        :data-url="'api/xcoa-mobile/v1/audit-report-find/find-report'"
        :show-advance-query="false"
        :scroll="{ x: 1500, y: 300 }"
        :defultpagination-pagesize="50"
        @searchbtnClick="searchbtnClick"
      >
        <template slot="findType" slot-scope="text, record">
          {{ JSON.parse(text).text }}
        </template>
        <template slot="whetherOrderDuty" slot-scope="text, record">
          {{ text === 0 ? '是' : '否' }}
        </template>
      </sd-data-table>
    </a-card>
  </div>
</template>

<script>
import { message } from '@/common/one-ui'
import axios from '@/common/services/axios-instance'
import { getUserInfo } from '@/common/store-mixin'
import auditAdvancedExport from '@product/iam/components/audit-advanced-export'
import AudiitRangePicker from '@product/iam/components/picker/audit-range-picker'
import AuditGroupPicker from '@product/iam/components/picker/audit-group-picker.vue'
import auditAdvancedQueryMixins from '@product/iam/components/audit-advanced-query-mixins'
import components from './_import-components/cube-audit-find-report-import'
export default {
  name: 'CubeAuditFindReport',
  components: {
    ...components,
    AudiitRangePicker,
    auditAdvancedExport,
    AuditGroupPicker,
  },
  metaInfo: {
    title: '审计问题',
  },
  mixins: [auditAdvancedQueryMixins],
  data() {
    return {
      expressions: [],
      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '审计机构',
          dataIndex: 'unitName',
          width: '120px',
        },
        {
          title: '项目名称',
          dataIndex: 'projectTitle',
          width: '170px',
        },
        {
          title: '项目编号',
          dataIndex: 'projectCode',
          width: '170px',
        },
        {
          title: '项目组长',
          dataIndex: 'groupLeaderName',
          width: '170px',
        },
        {
          title: '被审计单位',
          dataIndex: 'auditedUnitName',
          width: '170px',
        },
        {
          title: '审计问题发现提出人',
          dataIndex: 'creatFindOr',
          width: '180px',
        },
        {
          title: '审计发现标题',
          dataIndex: 'findTitle',
          width: '170px',
        },
        {
          title: '审计发现类别',
          dataIndex: 'findType',
          scopedSlots: { customRender: 'findType' },
          width: '170px',
        },
        {
          title: '审计发现描述',
          dataIndex: 'findDesc',
          width: '170px',
        },

        {
          title: '审计建议类型',
          dataIndex: 'suggestionType',
          scopedSlots: { customRender: 'suggestionType' },
          width: '170px',
        },
        {
          title: '问题类型',
          dataIndex: 'problemType',
          scopedSlots: { customRender: 'problemType' },
          width: '170px',
        },
        {
          title: '问题涉及金额（万元）',
          dataIndex: 'allMoney',
          width: '200px',
        },
        {
          title: '风险评级',
          dataIndex: 'riskLevel',
          width: '170px',
        },
        {
          title: '问题所属领域',
          dataIndex: 'businessProcess',
          width: '170px',
        },
        {
          title: '是否问责',
          dataIndex: 'whetherOrderDuty',
          scopedSlots: { customRender: 'whetherOrderDuty' },
          width: '170px',
        },
      ],

      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 14 },
      },
      form: {},
      riskOptions: [],
      problemOptions: [],
      auditModeOptions: [],
      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/findIamOrgId',
        method: 'post',
        params,
      }).then((res) => {
        this.id = res.data
        const deptCode = userInfo.id.toString()
        const deptName = userInfo.name
        this.rootNode = { code: deptCode, name: deptName, id: this.id }
      })
    })
  },
  mounted() {
    // 初始化数据字典信息
    this.initDictionaryInfo()
  },
  methods: {
    handleReset() {
      this.$refs.advancedSearchForm.resetFields()
      this.form = {}
    },
    initDictionaryInfo() {
      // 风险等级
      axios({
        url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=RISK_RATE',
        method: 'get',
      }).then((res) => {
        if (res.status === 200) {
          this.riskOptions = res.data
        }
      })
      // 问题类型
      axios({
        url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=PROBLEM_TYPE',
        method: 'get',
      }).then((res) => {
        if (res.status === 200) {
          this.problemOptions = res.data
        }
      })
    },
    exportForm() {
      if (!this.$refs.sjzlTable.data || this.$refs.sjzlTable.data.length === 0) {
        message.warning('未查询出可展示图谱的数据', 1)
        return
      }
      this.$refs.advancedSearchForm.validate((valid, values) => {
        if (valid) {
          // 此处直接打开知识图谱地址
          window.open('http://kg.smartdot.com:3001/schema/37/knowledge-atlas')
        }
      })
    },
    handleSearch() {
      this.$refs.advancedSearchForm.validate((valid, values) => {
        if (valid) {
          const units = this.form.units
          if (units && units.length > 0) {
            var unitsIds = []
            for (var i = 0; i < units.length; i++) {
              unitsIds.push(units[i].id.toString())
            }
            this.form.unitIds = unitsIds
          }
          const auditedUnits = this.form.auditedUnits
          if (auditedUnits && auditedUnits.length > 0) {
            var auditedUnitIds = []
            for (var j = 0; j < auditedUnits.length; j++) {
              auditedUnitIds.push(auditedUnits[j].id.toString())
            }
            this.form.auditedUnitIds = auditedUnitIds
          }
          this.search(this.form)
        }
      })
    },
    // 查询
    search(form) {
      // 默认查询条件
      this.expressions = []
      // 审计发现标题

      if (form.year[0]) {
        var startYear = new Date(form.year[0])
        this.expressions.push({
          dataType: 'str',
          name: 'planYear',
          op: 'ge',
          stringValue: startYear.getFullYear(),
        })
      }
      if (form.year[1]) {
        var endYear = new Date(form.year[1])
        this.expressions.push({
          dataType: 'str',
          name: 'planYear',
          op: 'le',
          stringValue: endYear.getFullYear(),
        })
      }
      if (form.unitIds) {
        this.expressions.push({
          dataType: 'str',
          name: 'unitIds',
          op: 'in',
          stringValue: form.unitIds
            .map((item) => {
              return "'" + item + "'"
            })
            .join(','),
        })
      }
      if (form.projectName) {
        this.expressions.push({
          dataType: 'str',
          name: 'projectName',
          op: 'like',
          stringValue: form.projectName,
        })
      }
      if (form.auditedUnitIds) {
        this.expressions.push({
          dataType: 'str',
          name: 'auditedUnitIds',
          op: 'in',
          stringValue: form.auditedUnitIds
            .map((item) => {
              return "'" + item + "'"
            })
            .join(','),
        })
      }
      if (form.whetherOrderDuty) {
        this.expressions.push({
          dataType: 'str',
          name: 'whetherOrderDuty',
          op: 'ne',
          stringValue: form.whetherOrderDuty,
        })
      }
      if (form.findTitle) {
        this.expressions.push({
          dataType: 'str',
          name: 'findTitle',
          op: 'like',
          stringValue: form.findTitle,
        })
      }
      if (form.findType) {
        this.expressions.push({
          dataType: 'str',
          name: 'findType',
          op: 'like',
          stringValue: form.findType,
        })
      }
      if (form.problemType) {
        this.expressions.push({
          dataType: 'str',
          name: 'problemType',
          op: 'ne',
          stringValue: form.problemType,
        })
      }
      if (form.riskLevel) {
        this.expressions.push({
          dataType: 'str',
          name: 'riskLevel',
          op: 'ne',
          stringValue: form.riskLevel,
        })
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
// .statistics {
//   :global(.ant-table-body) {
//     overflow-x: auto;
//   }
// }
.toptitle {
  color: #404040;
  font-weight: bold;
  font-size: 22px;
  margin-left: calc(50% - 150px);
}

.search-wrap {
  margin-bottom: $padding-lg;
  :global(.ant-advanced-search-form) {
    padding: 0;
    background: transparent;
    border: none;
  }
  :global .ant-divider-horizontal {
    margin: 10px 0;
  }
}
.searchdiv {
  height: 100%;
  :global(.sd-has-table.ant-card) {
    min-height: 50%;
  }
  :global(.reporttablecardxm) {
    :global(.ant-table-body) {
      height: auto !important;
      overflow: auto;
      min-height: auto !important;
    }
  }
  :global(.ant-table-placeholder) {
    height: auto !important;
  }
}
</style>
