<template>
  <a-modal
    :visible="rulevisible"
    :body-style="{
      padding: 0,
      minHeight: '500px',
    }"
    title="选择"
    width="1200px"
    :destroy-on-close="true"
    @ok="handleruleOk"
    @cancel="handleruleCancel"
  >
    <div :class="$style.wrapHeight">
      <div :class="$style.rowHeight">
        <audit-tree
          :checkable="false"
          :draggable="false"
          :is-search="false"
          :show-line="true"
          :top-node-text="'法律法规分类'"
          manager-type="view"
          :treeparams="{
            configId: 51,
          }"
          :default-expanded-keys="['0']"
          :is-select-dep="true"
          :dep-options="categorys"
          @treeSelect="treeSelect"
        >
        </audit-tree>
        <div :class="$style.rightcard">
          <a-card>
            <audit-advanced-query
              :expand="expand"
              :search-data="formData"
              :ref-name="searchform"
              :search-style="{ height: 'auto', left: '2%', top: '10%' }"
              :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="'效力级别'" prop="devoteLevel">
                    <sd-select
                      v-model="formData.devoteLevel"
                      :allow-clear="true"
                      :options="devoteLevel"
                    />
                  </a-form-model-item>
                </a-col>

                <a-col :span="12">
                  <a-form-model-item :label="'实施日期'" prop="implementTime">
                    <a-range-picker v-model="formData.implementTime" style="width: 100%" />
                  </a-form-model-item>
                </a-col>
              </template>
            </audit-advanced-query>
            <sd-data-table-ex
              ref="rulelist"
              form-id="iamLaw"
              page-id="core/law/iamLaw"
              :columns="columns"
              :actions="actions"
              show-selection
              :show-advance-query="true"
              :search-fields="['docTitle', 'publishUnit', 'dispatchWord']"
              :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>
      </div>
    </div>
  </a-modal>
</template>

<script>
import FlowService from '@/webflow/flow-service'
import axios from '@/common/services/axios-instance'
import TableActionTypes from '@/common/services/table-action-types'
import TableColumnTypes from '@/common/services/table-column-types'
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 auditTree from '../../../../components/audit-tree.vue'
import components from './_import-components/iam-operate-find-rule-modal-import'
const columns = [
  {
    title: '序号',
    dataIndex: 'sortNumber',
    width: '70px',
    customRender: (text, record, index) => `${index + 1}`,
  },
  {
    dataIndex: 'id',
    sdHidden: true,
  },
  {
    dataIndex: 'parentId',
    sdHidden: true,
  },
  {
    title: '标题',
    dataIndex: 'docTitle',
    // sdClickable: true, // 列内容是否可点击
    scopedSlots: { customRender: 'islink' },
  },
  {
    title: '发布单位',
    dataIndex: 'publishUnit',
  },
  {
    title: '发文字号',
    dataIndex: 'dispatchWord',
  },
  {
    title: '发布日期',
    dataIndex: 'publishTime',
    sdRender: TableColumnTypes.date,
    sorter: true,
    defaultSortOrder: 'desc',
  },
  {
    title: '实施日期',
    dataIndex: 'implementTime',
    sdRender: TableColumnTypes.date,
  },
  {
    title: '时效性',
    dataIndex: 'period',
  },
  {
    title: '效力级别',
    dataIndex: 'devoteLevel',
  },
  {
    title: '创建时间',
    dataIndex: 'creationTime',
    sdHidden: true,
  },
  {
    title: '内容',
    dataIndex: 'label',
    sdHidden: true,
  },
]
export default {
  name: 'IamOperateFindRuleModal',
  metaInfo: {
    title: '法律法规库',
  },
  components: {
    ...components,
    auditTree,
    auditAdvancedQuery,
  },
  mixins: [auditAdvancedQueryMixins],
  props: {
    rulevisible: {
      type: Boolean,
      default: false,
    },
    showrule: {
      type: Function,
      default: null,
    },
  },
  data() {
    return {
      // 列表展示用
      columns,
      // 导入用
      groupid: '',
      visible: false,
      title: '法律法规导入',
      // 左侧树用，用于创建对应分类数据
      parentId: 0,
      categorys: [],
      // 高级搜索用
      expand: false,
      searchform: 'searchform',
      formData: {
        docTitle: '',
        publishUnit: '',
        dispatchWord: '',
        publishTime: null,
        period: null,
        devoteLevel: null,
        implementTime: null,
      },
      period: [],
      devoteLevel: [],
      // 列表数据过滤公式
      filterExpressions: [
        {
          dataType: 'long',
          name: 'parentId',
          op: 'eq',
          longValue: 1,
        },
      ],
      // 按钮
      actions: [],
    }
  },
  methods: {
    // 弹出窗确认
    handleruleOk() {
      // 获取别选择的行信息
      const selectInfo = this.$refs.rulelist.getSelectedRows()
      let i = 1
      selectInfo.forEach((item) => {
        item.ruleName = item.docTitle
        item.ruleNumber = item.dispatchWord
        item.ruleContent = item.label
        item.sortNum = i
        i = i + 1
      })
      this.showrule(selectInfo)
    },
    // 弹出窗取消
    handleruleCancel() {
      this.showrule('')
    },
    flush() {
      return this.$refs.list?.refresh(true)
    },
    treeSelect(selectedKeys, info) {
      this.filterExpressions = []
      this.filterExpressions.push({
        dataType: 'long',
        name: 'parentId',
        op: 'eq',
        longValue: this.parentId,
      })
      if (selectedKeys && selectedKeys.length > 0) {
        this.filterExpressions[0].longValue = selectedKeys[0] + ''
        this.parentId = selectedKeys[0]
      }
    },
    // 高级搜索
    seniorSearch() {
      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
              }
            })
          }
        }
        if (!this.strOrArr(this.formData.devoteLevel)) {
          // 获取效力级别的字典值 'DEVOTE_LEVEL'
          if (!this.strOrArr(this.devoteLevel)) {
            axios({
              url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=DEVOTE_LEVEL',
              method: 'get',
            }).then((res) => {
              if (res.status === 200) {
                this.devoteLevel = res.data
              }
            })
          }
        }
      }
    },
    handleSearch() {
      const formData = this.formData
      // 修改表格过滤条件进行，再刷新进行数据过滤
      // 获取当前表格过滤条件
      this.filterExpressions = []
      this.filterExpressions.push({
        dataType: 'long',
        name: 'parentId',
        op: 'eq',
        longValue: this.parentId,
      })
      // 获取高级搜索的数据
      // 开始修改
      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.publishUnit,
          })
        }
        // 发布日期
        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]).valueOf(),
            })
            this.filterExpressions.push({
              dataType: 'long',
              name: 'publishTime',
              op: 'le',
              longValue: new Date(formData.publishTime[1]).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.devoteLevel)) {
          if (formData.devoteLevel[0] === null) {
            formData.devoteLevel = null
          } else {
            this.filterExpressions.push({
              dataType: 'str',
              name: 'devoteLevel',
              op: 'eq',
              stringValue: formData.devoteLevel[0].id,
            })
          }
        }
        // 实施日期
        if (this.strOrArr(formData.implementTime)) {
          if (formData.implementTime[0] === null) {
            formData.implementTime = null
          } else {
            this.filterExpressions.push({
              dataType: 'long',
              name: 'implementTime',
              op: 'ge',
              longValue: new Date(formData.implementTime[0]).valueOf(),
            })
            this.filterExpressions.push({
              dataType: 'long',
              name: 'implementTime',
              op: 'le',
              longValue: new Date(formData.implementTime[1]).valueOf(),
            })
          }
        }
      }
    },
    searchedClick() {
      this.expand = false
    },
    // 判断数组或字符串为null或长度为0
    strOrArr(strOrArr) {
      return strOrArr !== null && strOrArr.length > 0
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.btns {
  position: absolute;
  top: 24px;
  right: 490px;
  z-index: 100;
}
.wrap-height {
  height: 100%;
  :global(.ant-card-body) {
    height: 100%;
  }
  :global(.collapse_audit-tree_product) {
    :global(.fold_audit-tree_product) {
      left: 0;
    }
  }
  .row-height {
    display: flex;
    flex: auto;
    min-height: 500px;
    .rightcard {
      flex: 1;
      width: calc(100% - 20%);
      height: 100%;
      :global(.ant-card-body) {
        width: 98%;
        padding: 0;
      }
    }
  }
}
</style>
