<template>
  <div :class="$style.rightcard">
    <a-card>
      <audit-advanced-query
        :expand="expand"
        :search-data="formData"
        :ref-name="searchform"
        :search-style="{ height: '205px', left: '20px', top: '64px' }"
        :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="publishUnit">
              <a-input v-model="formData.publishUnit" 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="'发布日期'" prop="publishTime">
              <a-range-picker v-model="formData.publishTime" style="width:100%;" />
            </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>
          <a-col :span="12">
            <a-form-model-item :label="'属\u2002\u2002\u2002\u2002性'" prop="attribute">
              <sd-select v-model="formData.attribute" :allow-clear="true" :options="attribute" />
            </a-form-model-item>
          </a-col>
        </template>
      </audit-advanced-query>
      <sd-data-table-ex
        ref="list"
        :columns="columns"
        form-id=""
        :show-advance-query="true"
        :process-req="processReq"
        :search-fields="['docTitle', 'publishUnit', 'dispatchWord']"
        data-url="api/xcoa-mobile/v1/law-institution/getList"
        :filter-expressions="filterExpressions"
        @searchbtnClick="searchbtnClick"
      >
        <div slot="islink" slot-scope="text, record">
          <a :title="text" @click="rowClick(record)">{{ text }}</a>
        </div>
      </sd-data-table-ex>
    </a-card>
  </div>
</template>

<script>
import TableColumnTypes from '@/common/services/table-column-types'
import axios from '@/common/services/axios-instance'
import { getUserInfo } from '@/common/store-mixin'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import auditAdvancedQuery from '../../components/audit-advanced-query.vue'
import auditAdvancedQueryMixins from '../../components/audit-advanced-query-mixins'
import auditAdvancedQuerybtn from '../../components/audit-advanced-querybtn.vue'
import components from './_import-components/iam-audit-auditsource-lawinstitutionquery-list-import'
const columns = [
  {
    dataIndex: 'id',
    sdHidden: true,
  },
  // {
  //   title: '序号',
  //   dataIndex: 'sortNumber',
  //   width: '70px',
  //   customRender: (text, record, index) => `${index + 1}`,
  // },
  {
    title: '序号',
    dataIndex: 'index',
    width: '7%',
  },
  {
    title: '标题',
    dataIndex: 'docTitle',
    scopedSlots: { customRender: 'islink' },
    width: '24%',
  },
  {
    title: '属性',
    dataIndex: 'attribute',
    width: '9%',
  },
  {
    title: '发布单位',
    dataIndex: 'publishUnit',
    width: '14%',
  },
  {
    title: '发文字号',
    dataIndex: 'dispatchWord',
    width: '14%',
  },
  {
    title: '发布日期',
    dataIndex: 'publishTime',
    sdRender: TableColumnTypes.date,
    sorter: true,
    defaultSortOrder: 'desc',
    width: '10%',
  },
  {
    title: '实施日期',
    dataIndex: 'implementTime',
    sdRender: TableColumnTypes.date,
    width: '10%',
  },
  {
    title: '时效性',
    dataIndex: 'period',
    width: '9%',
  },
]
export default {
  name: 'IamAuditAuditsourceLawinstitutionqueryList',
  metaInfo: {
    title: '法规制度查询',
  },
  components: {
    ...components,
    auditAdvancedQuery,
    // auditAdvancedQuerybtn,
  },
  mixins: [auditAdvancedQueryMixins],
  data() {
    return {
      // 列表展示用
      columns,
      // 列表展示过滤条件
      currentUser: getUserInfo().account,
      filterExpressions: [],
      // 高级搜索
      searchform: 'searchform',
      expand: false,
      formData: {
        docTitle: '',
        publishUnit: '',
        dispatchWord: '',
        publishTime: null,
        period: null,
        attribute: null,
      },
      period: [],
      attribute: [
        { name: '内部制度', id: '内部制度' },
        { name: '法律法规', id: '法律法规' },
      ],
      // 按钮
      // actions: [
      //   {
      //     label: '高级搜索',
      //     permission: null,
      //     callback: () => {
      //       this.seniorSearch()
      //     },
      //   },
      //   // {
      //   //   label: '',
      //   //   permission: null,
      //   //   callback: this.openSearchModal,
      //   // },
      // ],
    }
  },
  methods: {
    searchbtnClick() {
      this.expand = !this.expand
      if (this.expand === true) {
        // 如果是展示的，那么可能去获取字典值去
        if (!this.strOrArr(this.formData.period)) {
          // 获取时效性的字典值 'PERIOD'
          if (!this.strOrArr(this.period)) {
            axios({
              url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=PERIOD',
              method: 'get',
            }).then((res) => {
              if (res.status === 200) {
                this.period = res.data
              }
            })
          }
        }
      }
    },
    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.publishUnit)) {
          this.filterExpressions.push({
            dataType: 'str',
            name: 'publishUnit',
            op: 'like',
            stringValue: formData.publishUnit,
          })
        }
        // 发文字号
        if (this.strOrArr(formData.dispatchWord)) {
          this.filterExpressions.push({
            dataType: 'str',
            name: 'dispatchWord',
            op: 'like',
            stringValue: formData.dispatchWord,
          })
        }
        // 发布日期
        if (this.strOrArr(formData.publishTime)) {
          if (formData.publishTime[0] === null) {
            formData.publishTime = null
          } else {
            this.filterExpressions.push({
              dataType: 'long',
              name: 'publishTime',
              op: 'ge',
              longValue: new Date(
                formData.publishTime[0].format('YYYY-MM-DD') + ' 00:00:00'
              ).valueOf(),
            })
            this.filterExpressions.push({
              dataType: 'long',
              name: 'publishTime',
              op: 'le',
              longValue: new Date(
                formData.publishTime[1].format('YYYY-MM-DD') + ' 23:59:59'
              ).valueOf(),
            })
          }
        }
        // 时效性
        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,
            })
          }
        }
        // 属性
        if (this.strOrArr(formData.attribute)) {
          if (formData.attribute[0] === null) {
            formData.attribute = null
          } else {
            this.filterExpressions.push({
              dataType: 'str',
              name: 'attribute',
              op: 'eq',
              stringValue: formData.attribute[0].id,
            })
          }
        }
      }
    },
    // 判断数组或字符串为null或长度为0
    strOrArr(strOrArr) {
      return strOrArr !== null && strOrArr.length > 0
    },
    // 开启关闭
    searchedClick() {
      this.expand = false
    },
    // 标题链接点击
    rowClick(record) {
      if (record.attribute === '法律法规') {
        crossWindowWatcher.waitForChanged('/iam-audit-auditsource-law-form?record=' + record.id)
      } else {
        crossWindowWatcher.waitForChanged(
          '/iam-audit-auditsource-institution-form?record=' + record.id
        )
      }
    },
    processReq(req) {
      const url = req.url
      if (url === 'api/xcoa-mobile/v1/law-institution/getList') {
        const expre = req.data.expressions
        expre.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 = expre
      }
      return req
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
// .wrap-height {
//   height: 100%;
//   .row-height {
//     display: flex;
//     flex: auto;
//     height: 100%;
//     .rightcard {
//       flex: 1;
//       // width: calc(100% - 20%);
//       right: 10%;
//       height: 100%;
//     }
//   }
// }
.rightcard {
  // width: calc(100% - 20%);
  // height: 100%;
  right: 10%;
  bottom: 10%;
  // flex: 1;
  display: flex;
  flex: auto;
  // height: 100%;
}
</style>
