<template>
  <a-modal
    ref="auditMattersSelectModal"
    :body-style="bodyStyle"
    :title="title"
    :destroy-on-close="true"
    :visible="visible"
    :width="modalWidth"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <audit-advanced-query
      :expand="expand"
      :search-data="formData"
      :ref-name="searchform"
      :search-style="{ height: '175px', top: '105px !important' }"
      :search-fun="handleSearch"
      @searchedClick="searchedClick"
    >
      <template>
        <a-col :span="12">
          <a-form-model-item :label="'标\u2002\u2002\u2002\u2002题'" prop="docTitle">
            <a-input v-model="formData.docTitle" allow-clear />
          </a-form-model-item>
        </a-col>

        <a-col :span="12">
          <a-form-model-item :label="'发文字号'" prop="dispatchWord">
            <a-input v-model="formData.dispatchWord" allow-clear />
          </a-form-model-item>
        </a-col>

        <a-col :span="12">
          <a-form-model-item :label="'时\u2002效\u2002性'" prop="period">
            <sd-select v-model="formData.period" :allow-clear="true" :options="period" />
          </a-form-model-item>
        </a-col>
      </template>
    </audit-advanced-query>
    <!-- 列表 -->
    <sd-data-table
      ref="auditMattersSelectModalTable"
      style="width:98%;margin:auto"
      :show-advance-btn="true"
      data-url="api/xcoa-mobile/v1/law-institution/getList"
      :pagination="{ pageSize: 10 }"
      form-id=""
      :filter-expressions="filterExpressions"
      :check-type="'checkbox'"
      :columns="columns"
      :show-advance-query="true"
      :search-fields="['docTitle', 'dispatchWord']"
      :process-res="processRes"
      :process-req="processReq"
      :show-selection="showSelection"
      @searchbtnClick="searchbtnClick"
      @onChange="onChange"
    >
      <div slot="islink" slot-scope="text, record">
        <a :title="text" @click="rowClick(record)">{{ text }}</a>
      </div>
    </sd-data-table>
  </a-modal>
</template>

<script>
import axios from '@/common/services/axios-instance'
import { getUserInfo } from '@/common/store-mixin'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import auditAdvancedQueryMixins from '../../components/audit-advanced-query-mixins'
import auditAdvancedQuery from '../../components/audit-advanced-query.vue'
import components from './_import-components/audit-matters-select-modal-import'
const columns = [
  {
    dataIndex: 'id',
    sdHidden: true,
  },
  {
    title: '序号',
    dataIndex: 'sortNumber',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '标题',
    dataIndex: 'docTitle',
    width: '45%',
    scopedSlots: { customRender: 'islink' },
  },
  {
    title: '属性',
    dataIndex: 'attribute',
    width: '100px',
  },
  {
    title: '发文字号',
    dataIndex: 'dispatchWord',
  },
  {
    title: '时效性',
    dataIndex: 'period',
  },
  {
    title: '编制日期',
    dataIndex: 'creationTime',
    defaultSortOrder: 'desc',
    sdHidden: true,
    width: '120px',
  },
]
export default {
  name: 'AuditMattersSelectModal',
  metaInfo: {
    title: 'AuditMattersSelectModal',
  },
  components: {
    ...components,
    auditAdvancedQuery,
  },
  mixins: [auditAdvancedQueryMixins],
  props: {
    // 弹出窗标题
    title: {
      type: String,
      default: '请选择',
    },
    // 弹出窗宽度
    modalWidth: {
      type: String,
      default: '1200px',
    },
    // 弹出窗显示参数
    visible: {
      type: Boolean,
      default: false,
    },
    orgId: {
      type: String,
      default: '0',
    },
    showSelection: {
      type: Function,
      default: () => true,
    },
  },
  data() {
    return {
      bodyStyle: {
        padding: 0,
      },
      // 列表展示用
      columns,

      // 高级搜索
      searchform: 'searchform',
      expand: false,
      // 列表展示过滤条件
      filterExpressions: [
        {
          dataType: 'long',
          name: 'auditOrgId',
          op: 'eq',
          longValue: Number(this.orgId),
        },
        {
          dataType: 'exps',
          op: 'or',
          expressionsValue: [
            {
              dataType: 'int',
              name: 'whether',
              op: 'eq',
              intValue: 0,
            },
            {
              dataType: 'str',
              name: 'creatorAccount',
              op: 'eq',
              stringValue: this.currentUser,
            },
          ],
        },
      ],
      formData: {
        docTitle: '',
        dispatchWord: '',
        period: null,
      },
      period: [],
      // 列表展示过滤条件
      currentUser: getUserInfo().account,
    }
  },
  watch: {
    orgId() {
      this.filterExpressions.forEach((item) => {
        if (item.name === 'auditOrgId') {
          item.longValue = this.orgId + ''
        }
      })

      this.filterExpressions.push({
        dataType: 'exps',
        op: 'or',
        expressionsValue: [
          {
            dataType: 'int',
            name: 'whether',
            op: 'eq',
            intValue: 0,
          },
          {
            dataType: 'str',
            name: 'creatorAccount',
            op: 'eq',
            stringValue: this.currentUser,
          },
        ],
      })
    },
  },
  created() {
    if (this.orgId === null) {
      this.filterExpressions = []
    }
    this.getDict()
  },
  methods: {
    // 标题链接点击
    rowClick(record) {
      if (record.attribute === '法律法规') {
        crossWindowWatcher.waitForChanged(
          '/iam-audit-auditsource-law-form?record=' + record.recordId
        )
      } else {
        crossWindowWatcher.waitForChanged(
          '/iam-audit-auditsource-institution-form?record=' + record.recordId
        )
      }
    },
    processReq(req) {
      const url = req.url
      if (url === 'api/xcoa-mobile/v1/law-institution/getList') {
        const expre = req.data.expressions
        if (expre.length <= 0) {
          if (this.orgId !== null) {
            this.filterExpressions.push({
              dataType: 'long',
              name: 'auditOrgId',
              op: 'eq',
              longValue: Number(this.orgId),
            })
          }
          this.filterExpressions.push({
            dataType: 'exps',
            op: 'or',
            expressionsValue: [
              {
                dataType: 'int',
                name: 'whether',
                op: 'eq',
                intValue: 0,
              },
              // {
              //   dataType: 'str',
              //   name: 'creatorAccount',
              //   op: 'eq',
              //   stringValue: this.currentUser,
              // },
            ],
          })
          req.data.expressions = this.filterExpressions
        }
      }
      return req
    },
    processRes(data) {
      let index = 0
      if (data.data !== null) {
        data.data.forEach((item) => {
          item.recordId = item.id
          item.id = this.$refs.auditMattersSelectModalTable.localPagination.current + '-' + index
          index++
        })
      }
      return data
    },
    getDict() {
      // 如果是展示的，那么可能去获取字典值去
      if (!this.strOrArr(this.formData.period)) {
        // 获取时效性的字典值 'PERIOD'
        if (this.period.length === 0) {
          axios({
            url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=PERIOD',
            method: 'get',
          }).then((res) => {
            if (res.status === 200) {
              this.period = res.data
            }
          })
        }
      }
    },
    // 判断数组或字符串为null或长度为0
    strOrArr(strOrArr) {
      return strOrArr !== null && strOrArr.length > 0
    },
    handleOk(e) {
      this.$parent.visible = !this.$parent.visible
      // 列表选择事件，返回选择的数据
      this.$emit(
        'listSelected',
        this.$refs.auditMattersSelectModalTable.getSelectedRowKeys(),
        this.$refs.auditMattersSelectModalTable.getSelectedRows()
      )
    },
    handleCancel(e) {
      this.$parent.visible = !this.$parent.visible
    },
    // 翻页操作
    onChange(pagination, filters, sorter) {
      this.pageSize = pagination.pageSize
      this.startPosition = (pagination.current - 1) * pagination.pageSize
    },
    // 开启关闭
    searchedClick() {
      this.expand = false
    },
    handleSearch() {
      const formData = this.formData
      // 获取当前表格过滤条件
      this.filterExpressions = []
      // 获取高级搜索的数据
      // 开始修改
      if (formData !== null) {
        // 标题
        if (this.strOrArr(formData.docTitle)) {
          this.filterExpressions.push({
            dataType: 'str',
            name: 'docTitle',
            op: 'like',
            stringValue: formData.docTitle,
          })
        }

        // 发文字号
        if (this.strOrArr(formData.dispatchWord)) {
          this.filterExpressions.push({
            dataType: 'str',
            name: 'dispatchWord',
            op: 'like',
            stringValue: formData.dispatchWord,
          })
        }

        // 时效性
        if (this.strOrArr(formData.period)) {
          if (formData.period[0] === null) {
            formData.period = null
          } else {
            this.filterExpressions.push({
              dataType: 'str',
              name: 'period',
              op: 'eq',
              stringValue: formData.period[0].id,
            })
          }
        }
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
