<template>
  <div v-loading="loading">
    <div
      class="search-form"
      style="text-align: left;"
    >
      <!-- 新增 -->
      <el-button
        v-if="!isConfirm && hasPermi(['trials:trials-panel:setting:reading-unit:edit']) && !isFromSystem"
        size="mini"
        type="primary"
        @click="handleAdd"
      >
        {{ $t('common:button:add') }}
      </el-button>
      <!-- 预览 -->
      <el-button
        v-if="isPreview"
        :disabled="tblList.length===0"
        size="mini"
        type="primary"
        @click="handlePreview"
      >
        {{ $t('common:button:preview') }}
      </el-button>
    </div>

    <el-table
      :data="tblList"
      size="small"
    >
      <el-table-column
        prop="ShowOrder"
        label=""
        width="50"
      />
      <!-- 分组 -->
      <el-table-column
        prop="GroupName"
        v-if="$i18n.locale === 'zh'"
        :label="$t('trials:readingUnit:qsList:title:groupNameEn')"
        show-overflow-tooltip
      />
      <!-- 分组(EN) -->
      <el-table-column
        prop="GroupEnName"
        v-if="$i18n.locale === 'en'"
        :label="$t('trials:readingUnit:qsList:title:groupNameEn')"
        show-overflow-tooltip
      />
      <!-- 名称 -->
      <el-table-column
        prop="QuestionName"
        v-if="$i18n.locale === 'zh'"
        :label="$t('trials:readingUnit:qsList:title:qsNameEn')"
        show-overflow-tooltip
      />
      <!-- 名称(EN) -->
      <el-table-column
        prop="QuestionEnName"
        v-if="$i18n.locale === 'en'"
        :label="$t('trials:readingUnit:qsList:title:qsNameEn')"
        show-overflow-tooltip
      />
      <!-- 题型 -->
      <el-table-column
        prop="Type"
        :label="$t('trials:readingUnit:qsList:title:type')"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ $fd('Criterion_Question_Type',scope.row.Type) }}
        </template>
      </el-table-column>
      <!-- 是否必填 -->
      <el-table-column
        prop="IsRequired"
        :label="$t('trials:readingUnit:qsList:title:isRequired')"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ $fd('QuestionRequired',scope.row.IsRequired) }}
        </template>
      </el-table-column>
      <!-- 是否显示 -->
      <el-table-column
        prop="ShowQuestion"
        :label="$t('trials:readingUnit:qsList:title:isShow')"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ $fd('ShowQuestion',scope.row.ShowQuestion) }}
        </template>
      </el-table-column>
      <!-- 是否裁判问题 -->
      <el-table-column
        prop="IsJudgeQuestion"
        :label="$t('trials:readingUnit:qsList:title:isJudgeQuestion')"
        width="120"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ $fd('YesOrNo', scope.row.IsJudgeQuestion) }}
        </template>
      </el-table-column>
      <!-- 是否在阅片页面显示 -->
      <el-table-column
        prop="IsShowInDicom"
        :label="$t('trials:readingUnit:qsList:title:isShowInDicom')"
        width="140"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ $fd('YesOrNo', scope.row.IsShowInDicom) }}
        </template>
      </el-table-column>
      <!-- 是否在全局阅片显示 -->
      <el-table-column
        prop="GlobalReadingShowType"
        :label="$t('trials:readingUnit:qsList:title:globalReadingShowType')"
        width="160"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ $fd('GlobalReadingShowType', scope.row.GlobalReadingShowType) }}
        </template>
      </el-table-column>
      <!-- 限制编辑 -->
      <el-table-column
        prop="LimitEdit"
        :label="$t('trials:readingUnit:qsList:title:limitEdit')"
        width="160"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ $fd('LimitEdit', scope.row.LimitEdit) }}
        </template>
      </el-table-column>
      <!-- 注释 -->
      <el-table-column
        prop="Remark"
        :label="$t('trials:readingUnit:qsList:title:Remark')"
        width="140"
        show-overflow-tooltip
      />
      <el-table-column
        prop=""
        :label="$t('common:action:action')"
        width="280"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <!-- 编辑 -->
          <el-button
            v-if="!isConfirm && !isFromSystem && hasPermi(['trials:trials-panel:setting:reading-unit:edit'])"
            type="primary"
            size="mini"
            @click="handleEdit(scope.row)"
          >
            {{ $t('trials:readingUnit:qsList:title:edit') }}
          </el-button>
          <!-- 查看 -->
          <el-button
            v-else
            type="primary"
            size="mini"
            @click="handlelook(scope.row)"
          >
            {{ $t('trials:readingUnit:qsList:title:view') }}
          </el-button>
          <!-- 表格问题 -->
          <el-button
            v-if="scope.row.Type === 'table'"
            type="primary"
            size="mini"
            @click="handleConfig(scope.row)"
          >
            {{ $t('trials:readingUnit:qsList:title:tableQs') }}
          </el-button>
          <!-- 删除 -->
          <el-button
            v-if="!isConfirm && !isFromSystem && hasPermi(['trials:trials-panel:setting:reading-unit:edit'])"
            type="danger"
            size="mini"
            @click="handleDelete(scope.row)"
          >
            {{ $t('trials:readingUnit:qsList:title:delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      v-if="addOrEdit.visible"
      :visible.sync="addOrEdit.visible"
      :close-on-click-modal="false"
      :title="addOrEdit.title"
      width="800px"
      append-to-body
      custom-class="base-dialog-wrapper"
    >
      <QuestionsForm
        ref="addOrEdit"
        :data="rowData"
        :trial-criterion-id="trialCriterionId"
        :is-from-system="isFromSystem"
        :digit-places="digitPlaces"
        :list="tblList"
        :is-look="isLook"
        :is-system-criterion="isSystemCriterion"
        @close="addOrEdit.visible = false"
        @getList="getList"
        @reloadArbitrationRules="reloadArbitrationRules"
      />
    </el-dialog>
    <el-dialog
      v-if="preview.visible"
      :visible.sync="preview.visible"
      :close-on-click-modal="false"
      :title="preview.title"
      :fullscreen="true"
    >
      <QuestionsPreview :criterion-id="trialCriterionId" :is-system-criterion="isSystemCriterion" :form-type="1" />
    </el-dialog>

    <el-dialog
      v-if="config.visible"
      :visible.sync="config.visible"
      :close-on-click-modal="false"
      :title="config.title"
      :fullscreen="true"
    >
      <TableQsList
        :digit-places="digitPlaces"
        :reading-question-id="rowData.Id"
        :is-from-system="isFromSystem"
        :is-confirm="isConfirm"
        :criterion-id="trialCriterionId"
        @close="config.visible = false"
      />
    </el-dialog>
  </div>
</template>
<script>
import { getReadingQuestionTrialList, deleteReadingQuestionTrial } from '@/api/trials'
import QuestionsForm from './QuestionsForm'
import QuestionsPreview from './QuestionsPreview'
import TableQsList from './TableQsList'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
const searchDataDefault = () => {
  return {
    QuestionName: ''
  }
}
export default {
  name: 'QcQuestions',
  components: { QuestionsForm, QuestionsPreview, TableQsList },
  props: {
    isSystemCriterion: {
      type: Boolean,
      default() { return false }
    },
    digitPlaces: {
      type: Number,
      default() { return 0 }
    },
    list: {
      type: Array,
      default() { return [] }
    },
    trialCriterionId: {
      type: String,
      required: true
    },
    readingCriterionPageId: {
      type: String,
      default: ''
    },
    isConfirm: {
      type: Boolean,
      default: true
    },
    isPreview: {
      type: Boolean,
      default: true
    },
    isFromSystem: {
      type: Boolean,
      default: true
    }

  },
  data() {
    return {
      searchData: searchDataDefault(),
      // list: [],
      isLook: null,
      tblList: [],
      total: 0,
      loading: false,
      rowData: {},
      addOrEdit: { visible: false, title: '' },
      preview: { visible: false, title: this.$t('trials:readingUnit:qsList:title:ecrfPreview') }, // 'eCRF预览'
      config: { visible: false, title: '' }
    }
  },
  computed: {
    ...mapGetters(['language'])
  },
  mounted() {
    this.tblList = this.list
    console.log('ql', this.isSystemCriterion)
  },
  methods: {
    getList() {
      this.loading = true
      this.searchData.ReadingQuestionCriterionTrialId = this.trialCriterionId
      this.searchData.ReadingCriterionPageId = this.readingCriterionPageId
      getReadingQuestionTrialList(this.searchData).then(res => {
        this.loading = false
        this.tblList = res.Result
      }).catch(() => { this.loading = false })
    },
    handleAdd() {
      this.rowData = {
        ReadingQuestionCriterionTrialId: this.trialCriterionId,
        ReadingCriterionPageId: this.readingCriterionPageId,
        Id: '' }
      this.isLook = false
      this.addOrEdit.title = this.$t('trials:readingUnit:qsList:title:add')// '添加'
      this.addOrEdit.visible = true
    },
    handlelook(row) {
      this.rowData = { ...row }
      this.isLook = true
      this.rowData.ReadingQuestionCriterionTrialId = this.trialCriterionId
      this.rowData.ReadingCriterionPageId = this.readingCriterionPageId
      this.addOrEdit.title = this.$t('trials:readingUnit:qsList:title:view')// '查看'
      this.addOrEdit.visible = true
    },
    handleEdit(row) {
      this.rowData = { ...row }
      this.isLook = false
      this.rowData.ReadingQuestionCriterionTrialId = this.trialCriterionId
      this.rowData.ReadingCriterionPageId = this.readingCriterionPageId
      this.addOrEdit.title = this.$t('trials:readingUnit:qsList:title:edit')// '编辑'
      this.addOrEdit.visible = true
    },
    handleDelete(row) {
      // '是否确认删除？'
      this.$confirm(this.$t('trials:readingUnit:qsList:message:deleteConfirm'), {
        type: 'warning',
        distinguishCancelAndClose: true
      })
        .then(() => {
          this.loading = true
          deleteReadingQuestionTrial(row.Id)
            .then(res => {
              this.loading = false
              if (res.IsSuccess) {
                this.getList()
                // 删除成功！
                this.$message.success(this.$t('trials:readingUnit:qsList:message:deleteSuccessfully'))
              }
            }).catch(() => { this.loading = false })
        })
    },
    handlePreview() {
      // this.preview.visible = true
      window.localStorage.setItem('TrialReadingCriterionId', this.TrialReadingCriterionId)
      var token = getToken()
     var path = `/criterionquestions?TrialReadingCriterionId=${this.trialCriterionId}&isSystemCriterion=${this.isSystemCriterion}&TokenKey=${token}`
      
      var routeData = this.$router.resolve({ path })
      this.openWindow = window.open(routeData.href, '_blank')
    },
    handleConfig(row) {
      this.rowData = { ...row }
      this.config.title = `${this.language === 'en' && row.QuestionEnName ? row.QuestionEnName : row.QuestionName}：${this.$t('trials:readingUnit:qsList:title:tableQs')}`// 表格问题
      this.config.visible = true
    },
    // 查询
    handleSearch() {
      this.getList()
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    reloadArbitrationRules() {
      this.$emit('reloadArbitrationRules')
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
