<template>
  <div>
    <a-card>
      <audit-advanced-query
        :expand="expand"
        :search-data="WPJGKFormData"
        :ref-name="searchform"
        :search-style="{ height: '170px', left: '20px', top: '60px' }"
        :search-fun="handleSearch"
        @searchedClick="searchedClick"
      >
        <template>
          <a-col :span="12">
            <a-form-model-item :label="'企业名称'" prop="enterName">
              <a-input v-model="WPJGKFormData.enterName" allow-clear />
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item :label="'类\u2002\u2002\u2002\u2002型'" prop="category">
              <sd-select v-model="WPJGKFormData.category" :allow-clear="true" :options="category" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="'住\u2002\u2002\u2002\u2002所'" prop="address">
              <a-input v-model="WPJGKFormData.address" allow-clear />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="'企业状态'" prop="enterStatus">
              <sd-select
                v-model="WPJGKFormData.enterStatus"
                :allow-clear="true"
                :options="enterStatus"
              />
            </a-form-model-item>
          </a-col>
        </template>
      </audit-advanced-query>
      <div :class="[$style.btns]">
        <audit-advanced-export
          ref="extrnalExport"
          :class-style="[$style.buttonSpacing]"
          :exclebxh="false"
          :expressions="expressions"
          :config-id="31"
          :v-if="false"
        />
      </div>
      <sd-oa-table
        ref="iamExternalBaseTable"
        form-id="iamExternalBase"
        page-id="audit/auditsource/extrnal/iamExternalBase"
        :filter-expressions="expressions"
        :columns="columns"
        :actions="actions"
        show-selection
        :search-fields="['name', 'code', 'address']"
        :show-advance-query="true"
        @searchbtnClick="searchbtnClick"
      >
        <div slot="islink" slot-scope="text, record">
          <a :title="text" @click="rowClick(record)">{{ text }}</a>
        </div>
        <div slot="isProject" slot-scope="text, e">
          <a :title="text" @click="handleOk(e)">{{ text }}</a>
        </div>
      </sd-oa-table>
    </a-card>
    <a-modal
      ref="projectList"
      title="项目信息"
      :visible="visible"
      :confirm-loading="confirmLoading"
      width="1000px"
      :footer="null"
      @cancel="handleCancel"
    >
      <sd-data-table-ex
        :data-url="joinProjectDataUrl"
        form-id="iamAuditProject"
        page-id="audit/project/iamAuditProject"
        :columns="projectColumns"
        :filter-expressions="projectTableExpressions"
        :show-selection="false"
      >
      </sd-data-table-ex>
    </a-modal>
  </div>
</template>

<script>
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import axios from '@/common/services/axios-instance'
import TableColumnTypes from '@/common/services/table-column-types'
import TableActionTypes from '@/common/services/table-action-types'
import auditAdvancedQuerybtn from '../../../components/audit-advanced-querybtn.vue'
import auditAdvancedQuery from '../../../components/audit-advanced-query.vue'
import auditAdvancedQueryMixins from '../../../components/audit-advanced-query-mixins'
import auditAdvancedExport from '../../../components/audit-advanced-export.vue'
import components from './_import-components/iam-audit-extrnal-list-import'
const columns = [
  {
    title: '序号',
    dataIndex: 'sortNumber',
    width: '80px',
    customRender: (text, record, index) => `${index + 1}`,
  },
  {
    dataIndex: 'id',
    sdHidden: true,
  },
  {
    title: '企业名称',
    dataIndex: 'name',
    // sdClickable: true, // 列内容是否可点击
    scopedSlots: { customRender: 'islink' },
    width: '26%',
  },
  {
    title: '类型',
    dataIndex: 'category',
  },
  {
    title: '统一社会信用代码',
    dataIndex: 'code',
    width: '220px',
  },
  {
    title: '住所',
    dataIndex: 'address',
  },
  {
    title: '企业状态',
    sorter: true,
    dataIndex: 'status',
  },
  {
    title: '当前所参与项目',
    dataIndex: 'joinProject',
    scopedSlots: { customRender: 'isProject' },
  },
  {
    title: '编制人员',
    dataIndex: 'authName',
    width: '120px',
  },
  {
    title: '编制日期',
    dataIndex: 'authTime',
    sorter: true,
    defaultSortOrder: 'descend',
    sdRender: TableColumnTypes.date,
  },
]
const projectColumns = [
  {
    title: '序号',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '项目名称',
    dataIndex: 'projectTitle',
  },
  {
    title: '项目组长',
    dataIndex: 'groupLeaderName',
  },
  {
    title: '项目状态',
    sorter: true,
    dataIndex: 'itemStatus',
  },
  {
    title: '项目开展日期',
    dataIndex: 'projectJoinDate',
  },
]
export default {
  name: 'IamAuditExtrnalList',
  metaInfo: {
    title: '外聘机构基本信息',
  },
  components: { ...components, auditAdvancedQuery, auditAdvancedExport },
  mixins: [auditAdvancedQueryMixins],
  data() {
    return {
      joinProjectDataUrl: 'api/xcoa-mobile/v1/iamexternalbase/project/list',
      columns,
      searchform: 'searchform',
      visible: false,
      projectColumns,
      confirmLoading: false,
      projectTableExpressions: [],
      category: [],
      enterStatus: [],
      WPJGKFormData: {
        enterName: null,
        category: '',
        address: null,
        enterStatus: '',
      },
      expressions: [],
      actions: [
        {
          label: '新建',
          id: 'new',
          permission: 'create',
          type: TableActionTypes.primary, // 新建按钮，不需要回调，自动处理
          callback: () => {
            const url = '/iam-audit-extrnal-form' // 新页面要打开的路由地址
            crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
              if (refreshFlag) {
                // 这里写或者调刷新的方法
                this.refresh()
              }
            })
          },
        },
        {
          label: '删除',
          id: 'delete',
          type: TableActionTypes.oa.delete, // 删除按钮，不需要回调，会自动处理
        },
        {
          label: '导出',
          permission: null, // 纯前端操作，不需要权限控制
          callback: this.export,
        },
      ],
    }
  },
  mounted() {
    setTimeout(() => {
      // 如果是展示的，那么可能去获取字典值去
      if (!this.strOrArr(this.WPJGKFormData.category)) {
        // 获取企业类型的字典值 'ENTERPRI-TYPE'
        if (!this.strOrArr(this.category)) {
          axios({
            url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=ENTERPRI-TYPE',
            method: 'get',
          }).then((res) => {
            if (res.status === 200) {
              this.category = res.data
            }
          })
        }
      }
      if (!this.strOrArr(this.WPJGKFormData.enterStatus)) {
        // 获取企业状态的字典值 'ENTERPRI-STATUS'
        if (!this.strOrArr(this.enterStatus)) {
          axios({
            url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=ENTERPRI-STATUS',
            method: 'get',
          }).then((res) => {
            if (res.status === 200) {
              this.enterStatus = res.data
            }
          })
        }
      }
    }, 500)
  },
  methods: {
    handleOk(e) {
      this.projectTableExpressions = []
      this.projectTableExpressions.push({
        dataType: 'str',
        name: 'code',
        op: 'eq',
        stringValue: e.code,
      })
      this.confirmLoading = true
      this.visible = true
      this.confirmLoading = false
    },
    export() {
      this.$refs.extrnalExport.exportdata()
    },
    handleCancel(e) {
      this.visible = false
    },
    rowClick(record) {
      const url = '/iam-audit-extrnal-form?record=' + record.id // 新页面要打开的路由地址
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.refresh()
        }
      })
    },
    refresh(toFirstPage) {
      return this.$refs.iamExternalBaseTable.refresh(toFirstPage)
    },
    handleSearch() {
      debugger
      this.expressions = []
      // 企业名称
      if (this.WPJGKFormData.enterName && this.WPJGKFormData.enterName.length > 0) {
        this.expressions.push({
          dataType: 'str',
          name: 'name',
          op: 'like',
          stringValue: `%${this.WPJGKFormData.enterName}%`,
        })
      }
      // 住所
      if (this.WPJGKFormData.address && this.WPJGKFormData.address.length > 0) {
        this.expressions.push({
          dataType: 'str',
          name: 'address',
          op: 'like',
          stringValue: `%${this.WPJGKFormData.address}%`,
        })
      }
      // 类型
      if (this.WPJGKFormData.category && this.WPJGKFormData.category.length > 0) {
        if (this.WPJGKFormData.category[0] === '') {
          this.WPJGKFormData.category = null
        } else {
          this.expressions.push({
            dataType: 'str',
            name: 'category',
            op: 'eq',
            stringValue: this.WPJGKFormData.category[0].id,
          })
        }
      }
      // 企业状态
      if (this.WPJGKFormData.enterStatus && this.WPJGKFormData.enterStatus.length > 0) {
        if (this.WPJGKFormData.enterStatus[0] === '') {
          this.WPJGKFormData.enterStatus = null
        } else {
          this.expressions.push({
            dataType: 'str',
            name: 'status',
            op: 'eq',
            stringValue: this.WPJGKFormData.enterStatus[0].id,
          })
        }
      }
    },
    strOrArr(strOrArr) {
      return strOrArr !== null && strOrArr.length > 0
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.btns {
  position: absolute;
  right: 99.5px;
  top: 24px;
  z-index: 100;
}
.buttonSpacing {
  margin-left: 5px;
}
</style>
