<template>
  <div :class="[$style.wrapHeight, $style.dataclass]">
    <div :class="$style.rowHeight">
      <div :class="$style.rowLeft">
        <audit-class-tree
          ref="auditMaintainCatalogTree"
          show-line
          top-node-text="数据分类维护"
          :is-select-dep="true"
          :draggable="true"
          :cate="true"
          @treeSelect="treeSelect"
        ></audit-class-tree>
      </div>
      <div :class="$style.rightcard">
        <a-card>
          <!-- 高级搜索组件 -->
          <audit-advanced-query
            :expand="expand"
            :search-data="formData"
            :ref-name="searchform"
            :search-style="{ height: '150px', left: '20px', top: '57px' }"
            :search-fun="handleSearch"
            @searchedClick="searchedClick"
          >
            <template>
              <a-col :span="12">
                <a-form-model-item :label="'分类编号'" prop="categoryId">
                  <a-input v-model="formData.categoryId" allow-clear />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item :label="'分类名称'" prop="categoryName">
                  <a-input v-model="formData.categoryName" allow-clear />
                </a-form-model-item>
              </a-col>
            </template>
          </audit-advanced-query>
          <SdDataTableEx
            :key="key"
            ref="dataTable"
            :projectlist="true"
            :editnode="editnode"
            form-id="iamDataCategory"
            page-id="audit/maintain/iamDataCategory"
            :columns="columns"
            :actions="actions"
            :filter-expressions="expressions"
            :custom-delete-fun="deleteRows"
            :search-fields="['categoryId', 'categoryName']"
            :show-advance-query="true"
            :show-selection="(item) => item.editAndDeleteAuth !== 0"
            @recordSaved="onRecordSaved"
            @searchbtnClick="searchbtnClick"
          >
            <template slot="categoryName" slot-scope="text, record">
              <a
                v-if="record.editAndDeleteAuth !== 0"
                @click="
                  () => {
                    $refs.dataTable.showDetailModal(record.id, 'audit/maintain/iamDataCategory')
                  }
                "
                >{{ text }}</a
              >
              <span v-else>{{ text }}</span>
            </template>

            <!-- 详情表单 -->
            <template v-slot:form="{ model }" v-sd-watermark="waterMark" @saved="onSaved">
              <SdFormItem name="categoryId" />
              <SdFormItem name="categoryName" />
              <a-form-model-item label="移动端分类名称">
                <a-input v-model="model.reservestring4" />
              </a-form-model-item>
              <SdFormItem name="weightNum" />
              <SdFormItem name="sortNum" />
              <a-form-model-item label="分类级别">
                <a-input :value="parentId === 0 ? '一级' : className" disabled />
              </a-form-model-item>
              <SdFormItem name="categoryDesc">
                <a-textarea v-model="model.categoryDesc" :rows="3" />
              </SdFormItem>
              <SdFormItem
                :hidden="true"
                name="parentId"
                :input-props="{ defaultValue: parentId, disabled: true }"
              />
              <SdFormItem
                :hidden="true"
                name="auditOrgId"
                :input-props="{ defaultValue: auditOrgId, disabled: true }"
              />
              <SdFormItem
                :hidden="true"
                name="parentCategoryId"
                :input-props="{ defaultValue: parentCategoryId, disabled: true }"
              />
              <SdFormItem name="level" :hidden="true" :input-props="{ defaultValue: level }" />
            </template>
          </SdDataTableEx>
        </a-card>
      </div>
    </div>
  </div>
</template>

<script>
import { Modal, message } from 'ant-design-vue'
import axios from '@/common/services/axios-instance'
import auditClassTree from './audit-class-tree.vue'
import auditAdvancedQuery from '../../components/audit-advanced-query.vue'
import auditAdvancedQueryMixins from '../../components/audit-advanced-query-mixins'
import auditAdvancedGroupMixins from '../../components/audit-advanced-group-mixins'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import SdDataTableEx from '@/common/components/sd-data-table-ex.vue'
import SdFormItem from '@/common/components/sd-form-item.vue'
import dataService from './data-config'
import TableActionTypes from '@/common/services/table-action-types'

export default {
  name: 'AuditDataClass',
  metaInfo: {
    title: '数据分类维护',
  },
  components: {
    SdDataTableEx,
    SdFormItem,
    auditClassTree,
    auditAdvancedQuery,
  },
  mixins: [auditAdvancedQueryMixins, auditAdvancedGroupMixins],
  data() {
    return {
      editnode: true,
      key: 0,
      searchform: 'searchform',
      formData: {
        categoryName: '',
        categoryId: '',
      },
      auditOrgId: null,
      parentId: 0,
      parentCategoryId: '000000',
      level: 1,
      isEnd: null,
      columns: [
        {
          dataIndex: 'id',
          sdHidden: true,
        },
        {
          dataIndex: 'parentId',
          sdHidden: true,
        },
        {
          title: '排序号',
          dataIndex: 'sortNum',
          defaultSortOrder: 'ascend',
          width: '100px',
        },
        {
          title: '分类编号',
          dataIndex: 'categoryId',
        },
        {
          title: '分类名称',
          dataIndex: 'categoryName',
          scopedSlots: { customRender: 'categoryName' },
        },
        {
          tiltle: '移动端分类名称',
          dataIndex: 'reservestring4',
          sdHidden: true,
        },
        {
          title: '权重排序',
          dataIndex: 'weightNum',
          sdHidden: true,
        },
        {
          title: '分类描述',
          dataIndex: 'categoryDesc',
        },
      ],
      actions: [
        {
          label: '新建',
          permission: null,
          // id: 'new',
          type: TableActionTypes.primary,
          callback: this.create,
        },
        {
          label: '审搜索引',
          permission: null,
          id: 'auditMattersCatalog',
          callback: this.toIndexList,
        },
        {
          label: '数据授权',
          id: 'auditWarrant',
          permission: null,
          callback: this.Matterssq,
        },
        {
          label: '删除',
          // id: 'delete',
          permission: null,
          callback: this.deleteRows,
        },
      ],
      expressions: [
        {
          dataType: 'long',
          name: 'parentId',
          op: 'eq',
          longValue: 0,
        },
      ],
      isroot: true,
      className: '',
    }
  },
  methods: {
    toIndexList() {
      if (this.parentId === 0) {
        message.warning('请选择分类')
        return
      }
      const name = this.className === '' ? '全部分类' : this.className
      const url =
        '#/sd-frame/audit-class-index?classId=' + this.parentCategoryId + '&className=' + name
      window.open(url)
    },
    handleSearch() {
      this.expressions = []
      // 模型编号
      if (this.formData.categoryId) {
        this.expressions.push({
          dataType: 'str',
          name: 'categoryId',
          op: 'like',
          stringValue: `%${this.formData.categoryId}%`,
        })
      }
      // 模型名称
      if (this.formData.categoryName) {
        this.expressions.push({
          dataType: 'str',
          name: 'categoryName',
          op: 'like',
          stringValue: `%${this.formData.categoryName}%`,
        })
      }

      this.expressions.push({
        dataType: 'long',
        name: 'parentId',
        op: 'eq',
        longValue: this.parentId,
      })
    },

    // 删除回调
    onRecordsDeleted() {
      message.success('删除成功')
      this.$refs.auditMaintainCatalogTree.initTreeData()
    },
    // 新建保存回调刷新树
    onRecordSaved() {
      this.$refs.auditMaintainCatalogTree.initTreeData()
    },
    treeSelect(selectedKeys, info) {
      // 父级id
      if (info != null && info.selectedNodes[0]) {
        if (info.node.dataRef.props.isroot) {
          this.parentId = 0
          this.level = 1
          this.parentCategoryId = '000000'
        } else {
          this.parentCategoryId = info.selectedNodes[0].data.props.dataRef.props.categoryId
          this.parentId = Number(info.selectedNodes[0].data.props.id)
          this.className = info.selectedNodes[0].data.props.text // 父级名称
          this.level = 2
        }
        this.editnode = info.selectedNodes[0].data.props.edit
        if (info.selectedNodes[0].data.props.props) {
          this.isEnd = info.selectedNodes[0].data.props.props.isEnd
        }
      } else {
        if (selectedKeys.length > 0) {
          this.parentId = Number(selectedKeys)
          this.level = 2
        } else {
          this.parentId = 0
          this.level = 1
          this.parentCategoryId = '000000'
        }
      }

      // 如果是末级，则隐藏新建按钮
      this.expressions.forEach((item) => {
        if (item.name === 'parentId') {
          item.longValue = this.parentId
        }
      })
      this.expressions = [...this.expressions]
    },
    // 新增
    create() {
      this.$refs.dataTable.showDetailModal(null, 'audit/maintain/iamDataCategory')
    },
    // 删除
    deleteRows() {
      const ids = this.$refs.dataTable.getSelectedRowKeys()
      dataService.deleteCategory(ids).then(() => {
        this.onRecordsDeleted()
        this.refresh()
      })
    },
    refresh() {
      this.$refs.dataTable.refresh()
    },
    // 判断按钮显示
    showeditbutton(deptId) {
      // axios({
      // url: 'api/xcoa-mobile/v1/iamdatacategory/getRole?selectOrgId=0',
      //  method: 'get',
      // }).then((res) => {
      //  if (res.data === true) {
      //   if (this.actions.length === 0) {
      this.actionscsh.forEach((a) => {
        this.actions.push(a)
      })
      //   }
      //  } else {
      //    this.actions = []
      // }
      // })
    },
    Matterssq() {
      // window.open('#/audit-data-from', '_blank')
      const arrid = this.$refs.dataTable.getSelectedRowKeys()
      if (arrid.length > 0) {
        const url = '#/audit-data-from?selectid=' + arrid.join(',') // 新页面要打开的路由地址
        // 新页面要打开的路由地址
        window.open(url, '_blank')
      } else {
        Modal.warning({
          title: '提示',
          content: '请选择数据分类!',
        })
      }
    },
    createMatters() {
      const rootId = this.$refs.auditMattersCatalogTree.depvalue
      // if (this.catalogId != null && this.catalogId !== rootId) {
      if (this.catalogId !== null || this.parentId !== null) {
        const url =
          '/audit-data-from?catalogId=' +
          this.parentId +
          '&catalogName=' +
          this.catalogName +
          '&auditMattersPath=' +
          this.auditMattersPath +
          '&orgId=' +
          this.$refs.auditMattersCatalogTree.depvalue
        // 新页面要打开的路由地址
        crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
          if (refreshFlag) {
            // 这里写或者调刷新的方法
            this.refresh()
          }
        })
      } else {
        Modal.confirm({
          title: '请先选择审计模型分类',
          content: '请先选择审计模型分类。',
          okText: '确定',
          okType: 'danger',
        })
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.wrap-height {
  height: 100%;
  .row-left {
    width: 20%;
  }
  .row-height {
    display: flex;
    flex: auto;
    height: 100%;

    .rightcard {
      flex: 1;
      width: calc(100% - 20%);
      height: 100%;
      overflow-y: auto;
    }
  }
}

.dataclass {
  :global(.projectlist .ant-table-empty .ant-table-body) {
    overflow-x: hidden !important;
  }

  :global(span > .ant-btn:nth-child(2)) {
    color: #fff;
    background-color: #1890ff;
    border-color: #1890ff;
  }

  // :global(.ant-table-placeholder) {
  //   width: auto;
  // }
}
</style>
