<template>
  <span :class="{ [$style.noBtnsTable]: !showbtns }">
    <audit-advanced-query
      :expand="expand"
      :search-data="formData"
      :search-style="{
        height: '135px',
        left: '0px',
        top: '40px !important',
        width: 'calc(100% - 5px) !important',
        margin: 'auto',
      }"
      :search-fun="handleSearch"
      @searchedClick="searchedClick"
    >
      <template>
        <a-col :span="12">
          <a-form-model-item label="分配状态" prop="itemStatus">
            <a-select v-model="formData.itemStatus" :allow-clear="true">
              <a-select-option value="已分配">
                已分配
              </a-select-option>
              <a-select-option value="未分配">
                未分配
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>

        <a-col :span="12">
          <a-form-model-item label="审计人员" prop="auditUser">
            <a-input v-model="formData.auditUser" />
          </a-form-model-item>
        </a-col>
      </template>
    </audit-advanced-query>

    <sd-data-table
      :key="key"
      ref="itemTable"
      :filter-expressions="expressions"
      :columns="columns"
      form-id="iamAuditFrameItem"
      :data-url="
        'api/xcoa-mobile/v1/iam-audit-frame-item/findIamAuditFrameItems?frameId=' + frameId
      "
      :process-req="processReq"
      :process-res="processRes"
      show-selection
      :modal-props="{ class: 'noBottom' }"
      :class="$style.itemTable"
      :search-fields="['itemName', 'itemCode', 'auditorName', 'itemStatus']"
      :show-advance-query="true"
      @searchbtnClick="searchbtnClick"
    >
      <div slot="islink" slot-scope="text, record">
        <a :title="text" @click="rowClick(record)">{{ text }}</a>
      </div>
    </sd-data-table>
  </span>
</template>

<script>
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import TableColumnTypes from '@/common/services/table-column-types'
import TableActionTypes from '@/common/services/table-action-types'
import auditAdvancedQuery from '../../../../../components/audit-advanced-query.vue'
import auditAdvancedQueryMixins from '../../../../../components/audit-advanced-query-mixins'
import components from './_import-components/iam-audit-frame-item-list-import'

export default {
  name: 'IamAuditFrameItemList',
  metaInfo: {
    title: 'IamAuditFrameItemList',
  },
  components: {
    ...components,
    auditAdvancedQuery,
  },
  mixins: [auditAdvancedQueryMixins],
  props: {
    frameId: {
      type: String,
      default: null,
    },
    showbtns: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      expressions: [
        {
          dataType: 'long',
          name: 'frameId',
          op: 'eq',
          longValue: this.frameId,
        },
      ],
      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '审计事项名称',
          dataIndex: 'itemName',
          scopedSlots: { customRender: 'islink' },
          width: '45%',
        },
        {
          title: '审计事项编号',
          dataIndex: 'itemCode',
        },
        {
          title: '审计人员',
          dataIndex: 'auditorName',
          sorter: true,
        },
        {
          title: '分配状态',
          dataIndex: 'itemStatus',
          sorter: true,
          customRender: (text, record, index) => {
            if (text) {
              return text
            } else {
              return '未分配'
            }
          },
          width: '120px',
        },
      ],
      formData: {
        itemStatus: null,
        auditUser: null,
      },
      key: 0,
    }
  },
  methods: {
    // 查询
    handleSearch() {
      this.key++
      // 默认查询条件
      this.expressions = [
        {
          dataType: 'long',
          name: 'frameId',
          op: 'eq',
          longValue: this.frameId,
        },
      ]

      // 分配状态
      if (this.formData.itemStatus) {
        this.expressions.push({
          dataType: 'str',
          name: 'itemStatus',
          op: 'eq',
          stringValue: `${this.formData.itemStatus}`,
        })
      }

      // 审计人员
      if (this.formData.auditUser) {
        this.expressions.push({
          dataType: 'str',
          name: 'auditorName',
          op: 'like',
          stringValue: `%${this.formData.auditUser}%`,
        })
      }
    },
    // 重新包装数据
    processRes(req) {
      const newdata = {
        data: [...req],
        totalSize: req.length,
      }
      return newdata
    },
    // post数据格式修改
    processReq(req) {
      req.infoQuery = req.data
      return req
    },
    // 获取复选的事项
    getSelectedRows() {
      return this.$refs.itemTable.getSelectedRows()
    },
    // 新建、详情打开新页面
    rowClick(record) {
      const url =
        '/iam-audit-frame-item?recordId=' + record.id + '&readonly=' + (this.showbtns ? '0' : '1') // 新页面要打开的路由地址
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.refresh()
        }
      })
    },
    // 刷新列表
    refresh() {
      this.$refs.itemTable.refresh()
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.item-table {
  margin-top: 10px;
  display: inline-block;
  :global(.header_sd-data-table_common) {
    position: absolute;
    top: -8px;
    right: 570px;
  }
}
.no-btns-table {
  .item-table {
    margin-top: 50px;
    display: inline-block;
    :global(.header_sd-data-table_common) {
      position: absolute;
      top: -8px;
      right: -12px;
    }
  }
}
</style>
