<template>
  <div :class="$style.wrapHeight">
    <div :class="$style.rowHeight">
      <AduitAreaTree ref="auditAreaTree" @treeSelect="treeSelect"></AduitAreaTree>

      <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="modelCode">
                  <a-input v-model="formData.modelCode" allow-clear />
                </a-form-model-item>
              </a-col> -->
              <a-col :span="12">
                <a-form-model-item :label="'模型名称'" prop="modelName">
                  <a-input v-model="formData.modelName" allow-clear />
                </a-form-model-item>
              </a-col>
            </template>
          </audit-advanced-query>

          <sd-data-table
            ref="SJMXDataTable"
            :filter-expressions="expressions"
            :columns="columns"
            :actions="actions"
            form-id="iamModelMaintain"
            data-url="api/xcoa-mobile/v1/iammodelmaintain/iamModelMaintainQueryList"
            :search-fields="['modelName']"
            show-selection
            :show-advance-query="true"
            @searchbtnClick="searchbtnClick"
          >
            <div slot="islink" slot-scope="text, record">
              <a v-if="rowNameStatus(record)" :title="text" @click="rowClick(record)">{{ text }}</a>
              <span v-else>{{ text }}</span>
            </div>
            <div slot="action" slot-scope="text, record">
              <!-- 如果 -->
              <a-button
                v-if="sharedSettingStatus(record)"
                type="link"
                @click="onActionShare(record)"
                >申请共享</a-button
              >
              <span v-else :class="$style.noAction">申请共享</span>
            </div>
          </sd-data-table>
        </a-card>
      </div>
    </div>
  </div>
</template>

<script>
import axios from '@/common/services/axios-instance'
import { Modal, message } from 'ant-design-vue'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import TableActionTypes from '@/common/services/table-action-types'
import { getUserInfo } from '@/common/store-mixin'
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 auditMaintainClassifyTree from './audit-maintain-classify-tree'
import auditMaintainService from './audit-maintain-service'
import AduitAreaTree from './aduit-area-tree.vue'
import components from './_import-components/audit-view-list-import'

export default {
  name: 'AuditViewList',
  metaInfo: {
    title: '模型查询',
  },
  components: {
    ...components,
    auditAdvancedQuery,
    // auditMaintainClassifyTree,
    AduitAreaTree,
  },
  mixins: [auditAdvancedQueryMixins, auditAdvancedGroupMixins],

  data() {
    return {
      key: 0,
      treeData: [],
      searchform: 'searchform',
      formData: {
        modelCode: '',
        modelName: '',
      },
      formId: 'iamModelMaintain',
      columns: [
        {
          title: '序号',
          dataIndex: 'sortNumber',
          width: '70px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        {
          title: '模型名称',
          dataIndex: 'modelName',
          scopedSlots: { customRender: 'islink' },
        },
        {
          title: '模型编号',
          dataIndex: 'id',
        },
        {
          title: '模型描述',
          dataIndex: 'modelDesc',
        },
        {
          title: '操作',
          dataIndex: 'sharedSetting',
          // 按钮
          scopedSlots: { customRender: 'action' },
        },
      ],
      actions: [
        {
          label: '授权申请',
          id: 'sqapply',
          permission: null,
          type: TableActionTypes.primary,
          callback: () => {
            window.open('#/sd-flow-guide?code=PRODUCT_IAM_MLSQSQ')
          },
        },
      ],
      expressions: [
        {
          dataType: 'str',
          name: 'catalogId',
          op: 'eq',
          stringValue: 0,
        },
      ],
      catalogId: null,
      catalogName: '',
      isroot: true,
      modelDomain: '',
    }
  },
  computed: {
    userInfoData() {
      return getUserInfo()
    },
  },
  methods: {
    treeSelect(selectedKeys, info) {
      this.modelDomain = selectedKeys[0] || ''
      this.handleSearch()
    },

    refresh() {
      return this.$refs.SJMBKDataTable.refresh(true)
    },
    rowClick(record) {
      const url = '/audit-maintain-from?record=' + record.id + '&type=readonly' // 新页面要打开的路由地址
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.refresh()
        }
      })
    },
    // 共享按钮状态
    sharedSettingStatus(record) {
      // 0 没共享过， 1 有共享记录
      // 如果是创建人，已经申请过，
      // 开启共享状态
      const flag =
        record.reservelong1 !== 1 &&
        record.creatorId !== this.userInfoData.id &&
        record.sharedSetting === 0 &&
        record.reservestring1 !== 'true'

      // if (record.creatorId !== this.userInfoData.id) {
      //   return true
      // }
      return flag
    },
    rowNameStatus(record) {
      return (
        record.reservelong1 === 1 ||
        record.creatorId === this.userInfoData.id ||
        record.reservestring1 === 'true'
      )
    },
    // 模板名称是否能点击
    islink(record) {
      // 如果是创建人，已经申请过，且审核状态是通过
      // 可以点击
      if (record.creatorId === this.userInfoData.id) {
        return true
      }
      return false
    },
    // 查询
    handleSearch() {
      this.expressions = []
      // 模型编号
      if (this.formData.modelCode) {
        this.expressions.push({
          dataType: 'str',
          name: 'id',
          op: 'like',
          stringValue: `%${this.formData.modelCode}%`,
        })
      }
      // 模型名称
      if (this.formData.modelName) {
        this.expressions.push({
          dataType: 'str',
          name: 'modelName',
          op: 'like',
          stringValue: `%${this.formData.modelName}%`,
        })
      }
      // 模型领域
      if (this.modelDomain) {
        this.expressions.push({
          dataType: 'str',
          name: 'modelDomain',
          op: 'eq',
          stringValue: this.modelDomain,
        })
      }
    },
    // 点击申请共享
    onActionShare(val) {
      const userInfo = getUserInfo()
      const dataList = []
      const paramsList = ['modelId', 'applyForId', 'applyStatus']
      paramsList.forEach((item) => {
        const obj = {}
        obj.key = item
        obj.oldVal = null
        if (item === 'modelId') {
          obj.newVal = val.id
        }
        if (item === 'applyForId') {
          obj.newVal = userInfo.id
        }
        if (item === 'applyStatus') {
          obj.newVal = '0'
        }
        dataList.push(obj)
      })
      const jsonStr = JSON.stringify(dataList)
      const paramsLog = {
        beanId: null,
        formId: 'iamModelShare',
        pageId: 'audit/maintain/iamModelShare',
        jsonStr,
      }
      const params = {
        eventId: 'save',
        inputs: [
          {
            name: 'id',
          },
          {
            name: 'modelId',
            value: val.id,
          },

          {
            name: 'applyForId',
            value: userInfo.id,
          },
          {
            name: 'applyStatus',
            value: '0',
          },
          {
            name: 'belongedOrgId',
            value: -1,
          },
          {
            name: 'modelCreatorId',
            value: val.creatorId,
          },
        ],
        pageFlowId: null,
        pagePath: null,
      }
      auditMaintainService.getApplyShareModel().then((res) => {
        params.pageFlowId = res.data.attrs.pageflowId
        params.pagePath = res.data.attrs.pagePath
        auditMaintainService.applyShareLog(paramsLog).then((resLog) => {
          if (res.data) {
            auditMaintainService.applyShare(params).then((resPar) => {
              // 提示
              if (resPar.data) message.warning('您的申请已发送创建人请稍等，申请通过后即可查看')
            })
          }
        })
      })
      // id val.id
      // 根据id向创建人发送通知
    },
  },
}
</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%);
      height: 100%;
    }
  }
}
.no-action {
  padding: 0 15px;
  color: $text-color-secondary;
}
</style>
