<template>
  <div>
    <a-card :class="$style.surveyCard">
      <audit-advanced-query
        :expand="expand"
        :search-data="surveyFormData"
        :ref-name="searchform"
        :search-style="{ height: '170px', left: '20px', top: '40px !important' }"
        :search-fun="handleSearch"
        @searchedClick="searchedClick"
        @resetForm="resetForm"
      >
        <template>
          <a-col :span="12">
            <a-form-model-item :label="'控制点名称'" prop="measureName">
              <a-input v-model="surveyFormData.measureName" allow-clear />
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="缺陷等级" prop="defectLevel">
              <sd-select v-model="surveyFormData.defectLevel" :options="defectLevel" allow-clear />
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item :label="'\u2002\u2002缺陷类型:'" prop="defectType">
              <sd-select v-model="surveyFormData.defectType" :options="defectType" allow-clear />
            </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="201"
          :v-if="false"
        />
      </div>
      <sd-data-table-ex
        ref="icConfirmList"
        form-id="icEvaluationInventory"
        data-url="api/xcoa-mobile/v1/icevaluationconfirm/getMeasureList"
        :columns="Columns"
        :filter-expressions="Expressions"
        :actions="actions"
        show-selection
        :show-advance-query="true"
        :search-fields="['prcName', 'measureName', 'testResult']"
        @searchbtnClick="searchbtnClick"
      >
        <div slot="islink" slot-scope="text, record">
          <a @click="rowClick(record)">{{ text }}</a>
        </div>
      </sd-data-table-ex>
    </a-card>
  </div>
</template>

<script>
import moment from 'moment'
import { Modal } from 'ant-design-vue'
import axios from '@/common/services/axios-instance'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import auditAdvancedQuery from '../../../components/audit-advanced-query.vue'
// import icMeasureSelect from '../../../components/ic-measure-select.vue'
import auditAdvancedQueryMixins from '../../../components/audit-advanced-query-mixins'
import auditAdvancedExport from '../../../components/audit-advanced-export.vue'
import MtxProcessService from '../../mtxprocess/mtx-process-service'
import components from './_import-components/iam-work-confirm-list-import'

const Columns = [
  {
    title: '序号',
    dataIndex: 'sortNumber',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    dataIndex: 'id',
    sdHidden: true,
  },
  {
    title: '内控流程',
    dataIndex: 'prcName',
  },
  {
    title: '控制措施名称',
    dataIndex: 'measureName',
    width: '20%',
  },
  {
    title: '控制措施编号',
    width: '10%',
    dataIndex: 'measureCode',
    // sdClickable: true, // 列内容是否可点击
    // scopedSlots: { customRender: 'islink' },
  },
  {
    title: '测试结果',
    dataIndex: 'testResult',
    width: '10%',
  },
  {
    title: '缺陷类型',
    dataIndex: 'defectType',
    width: '10%',
  },
  {
    title: '缺陷等级',
    dataIndex: 'defectLevel',
    width: '10%',
  },
  {
    title: '缺陷描述',
    dataIndex: 'defectDesc',
    width: '10%',
  },
  {
    title: '测试人',
    dataIndex: 'testUserName',
    width: '120px',
  },
  // { title: '测试类型', dataIndex: 'currentUser' },
]
export default {
  name: 'IamWorkConfirmList',
  metaInfo: {
    title: '缺陷汇总清单',
  },
  components: { ...components, auditAdvancedQuery, auditAdvancedExport },
  mixins: [auditAdvancedQueryMixins],
  data() {
    return {
      formId: 'icEvaluationInventory',
      planId: this.$root.$route.query.planId,
      Columns,
      searchform: 'searchform',
      surveyFormData: {
        measureName: '',
        defectLevel: '',
        defectType: '',
      },
      Expressions: [
        {
          dataType: 'long',
          name: 'planId',
          op: 'eq',
          longValue: this.$root.$route.query.planId,
        },
      ],
      actions: [
        {
          label: '导出',
          id: 'export',
          permission: null,
          callback: () => {
            this.exportdata()
          },
        },
        {
          label: '缺陷确认',
          id: 'new',
          permission: null,
          callback: this.createconfirm,
          index: 1,
        },
      ],
      versionId: '',
      orgId: '',
      orgName: '',
      defectType: [],
      defectLevel: [],
    }
  },
  mounted() {
    const planId = this.$route.query.planId
    if (planId) {
      this.planId = planId
    }
    axios({
      url: `api/framework/v1/page/wp/ic/evaluation/plan/icEvaluationPlan?id=` + planId,
    }).then((res) => {
      const unitname = res.data.pageFormData.pageFieldInfos.findIndex(
        (i) => i.name === 'evaluationUnitName'
      )
      const unitid = res.data.pageFormData.pageFieldInfos.findIndex(
        (i) => i.name === 'evaluationUnitId'
      )
      const creatorName = res.data.pageFormData.pageFieldInfos.findIndex(
        (i) => i.name === 'creatorName'
      )

      const params = {
        orgId: res.data.pageFormData.pageFieldInfos[unitid].value,
        moduleId: 'icMtxVersion',
      }
      this.orgId = res.data.pageFormData.pageFieldInfos[unitid].value
      this.orgName = res.data.pageFormData.pageFieldInfos[unitname].value.toString()
      axios({
        url: 'api/xcoa-mobile/v1/iamorg/findIamOrgId',
        method: 'post',
        params,
      }).then((res) => {
        if (res.data) {
          this.orgId = res.data.toString()
          MtxProcessService.getversion(this.orgId).then((res) => {
            if (res.data.id) {
              this.versionId = res.data.id.toString()
            }
          })
        }
      })
    })
    this.initDictionaryInfo()
    // this.fngetlist()
  },
  methods: {
    createconfirm() {
      const crids = []
      const cids = []
      const selectRows = this.$refs.icConfirmList.getSelectedRows()
      if (selectRows.length === 0) {
        Modal.error({
          title: '请选择需要确认的缺陷',
        })
        return false
      }
      selectRows.forEach((r) => {
        if (r.testType === '控制测试') {
          cids.push(r.id)
        } else {
          crids.push(r.id)
        }
      })
      const url =
        '/sd-flow-guide?code=PRODUCT_IC_CONFIRM&planId=' +
        this.planId +
        '&cids=' +
        cids.join(',') +
        '&crossid=' +
        crids.join(',') // 新页面要打开的路由地址
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          // 这里写或者调刷新的方法
          this.refresh()
        }
      })
    },
    initDictionaryInfo() {
      axios({
        url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=DEFECT_TYPE',
        method: 'get',
      }).then((res) => {
        if (res.status === 200) {
          this.defectType = res.data
        }
      })
      axios({
        url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=DEFECT_LEVEL',
        method: 'get',
      }).then((res) => {
        if (res.status === 200) {
          this.defectLevel = res.data
        }
      })
    },
    exportdata() {
      this.$refs.extrnalExport.exportdata()
    },
    moment,
    rowClick(record) {
      let url = ''
      if ((record.flowState === '起草') | (record.flowState === '开始')) {
        url = `/sd-webflow/pages/draft/` + record.instId
      } else {
        url = `/sd-webflow/done-pages/` + record.instId
      }
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) this.refresh()
      })
    },
    refresh() {
      if (this.$refs.icConfirmList) {
        this.$refs.icConfirmList.clearSelection()
        this.$refs.icConfirmList.refresh()
      }
    },
    handleSearch() {
      this.Expressions = []
      this.Expressions.push({
        dataType: 'long',
        name: 'planId',
        op: 'eq',
        longValue: this.$root.$route.query.planId,
      })
      if (this.surveyFormData.measureName) {
        this.Expressions.push({
          dataType: 'str',
          name: 'measureName',
          op: 'like',
          stringValue: `%${this.surveyFormData.measureName}%`,
        })
      }
      // 缺陷
      if (this.surveyFormData.defectType) {
        this.Expressions.push({
          dataType: 'str',
          name: 'defectType',
          op: 'eq',
          stringValue: this.surveyFormData.defectType[0].id,
        })
      }
      if (this.surveyFormData.defectLevel) {
        this.Expressions.push({
          dataType: 'str',
          name: 'defectLevel',
          op: 'eq',
          stringValue: this.surveyFormData.defectLevel[0].id,
        })
      }
      // this.expand = false
    },
    // 重置
    resetForm() {
      this.surveyFormData.defectLevel = null
      this.surveyFormData.defectType = null
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.sjfxTable {
  :global .ant-table-column-title {
    white-space: normal;
  }
  :global(.ant-table-body) {
    overflow-x: scroll !important;
  }

  :global(.ant-table-empty) {
    :global(.ant-table-body) {
      overflow-x: scroll !important;
    }
  }
}
.survey-card {
  margin-top: -7px;
}
.btns {
  position: absolute;
  top: 24px;
  right: 99.5px;
  z-index: 100;
}
.buttonSpacing {
  margin-left: 5px;
}
</style>
