<template>
  <div class="criterion-config">
    <el-tabs v-model="activeName">
      <!-- 访视阅片 -->
      <el-tab-pane :label="$t('dictionary:template:criterionConfig:tab:visitReview')" name="0">
        <div class="search-form" style="display:flex;justify-content: space-between;">
          <el-form :inline="true" size="mini" class="base-search-form">
            <!-- 问题名称 -->
            <el-form-item :label="$t('trials:readingUnit:qsList:title:qsName')">
              <el-input v-model="searchData.QuestionName" clearable style="width:120px;" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="handleSearch">
                {{ $t('common:button:search') }}
              </el-button>
              <el-button
                type="primary"
                icon="el-icon-refresh-left"
                @click="handleReset"
              >
                {{ $t('common:button:reset') }}
              </el-button>
            </el-form-item>
          </el-form>
          <div>
            <el-button
              :disabled="list.length === 0"
              size="mini"
              type="primary"
              @click="handlePreview"
            >
              {{ $t('common:button:preview') }}
            </el-button>
            <el-button
              v-if="Object.keys(data).length > 0 && !(data.IsCompleteConfig || data.IsBeUsed)"
              size="mini"
              type="primary"
              @click="handleAdd"
            >
              {{ $t('common:button:add') }}
            </el-button>
          </div>

        </div>
        <el-table
          ref="criterionQSList"
          v-loading="loading"
          v-adaptive="{bottomOffset:80}"
          height="100"
          :data="list"
          stripe
          @sort-change="handleSortByColumn"
        >
          <!-- 显示序号 -->
          <el-table-column
            prop="ShowOrder"
            label=""
            width="50"
          />
          <!-- 分组 -->
          <el-table-column
            prop="GroupName"
            :label="$t('trials:readingUnit:qsList:title:groupName')"
            show-overflow-tooltip
          />
          <!-- 分组(EN) -->
          <el-table-column
            prop="GroupEnName"
            :label="$t('dictionary:template:criterionConfig:title:groupNameEn')"
            show-overflow-tooltip
            width="140"
          />
          <!-- 名称 -->
          <el-table-column
            prop="QuestionName"
            :label="$t('trials:readingUnit:qsList:title:qsName')"
            show-overflow-tooltip
          />
          <!-- 名称(EN) -->
          <el-table-column
            prop="QuestionEnName"
            :label="$t('dictionary:template:criterionConfig:title:qsNameEn')"
            show-overflow-tooltip
            width="140"
          />
          <!-- 题型 -->
          <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="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="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="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="150"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              {{ $fd('YesOrNo', scope.row.IsShowInDicom) }}
            </template>
          </el-table-column>
          <!-- 注释 -->
          <!-- <el-table-column
            prop="Remark"
            :label="$t('trials:readingUnit:qsList:title:Remark')"
            width="140"
            show-overflow-tooltip
          >
          </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="ExportIdentification"
            :label="$t('trials:readingUnit:qsList:title:ExportIdentification')"
            width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              {{ $fd('ExportIdentification', scope.row.ExportIdentification) }}
            </template>
          </el-table-column> -->
          <!-- 问题标识 -->
          <el-table-column
            prop="QuestionType"
            :label="$t('dictionary:template:criterionConfig:table:questionType')"
            width="140"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              {{ $fd('QuestionType', scope.row.QuestionType) }}
            </template>
          </el-table-column>
          <!-- 导出目标表格 -->
          <el-table-column
            prop="ExportResult"
            :label="$t('trials:readingUnit:qsList:title:ExportResult')"
            width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              {{ getStringResult(scope.row.ExportResult, 'ExportResult') }}
            </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=""
            :label="$t('common:action:action')"
            width="250"
            show-overflow-tooltip
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                v-if="Object.keys(data).length > 0 && !(data.IsCompleteConfig || data.IsBeUsed)"
                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
                type="primary"
                size="mini"
                :disabled="!(scope.row.Type === 'table' || scope.row.Type === 'basicTable')"
                @click="handleConfig(scope.row)"
              >
                {{ $t('trials:readingUnit:qsList:title:tableQs') }}
              </el-button>
              <el-button
                v-if="Object.keys(data).length > 0 && !(data.IsCompleteConfig || data.IsBeUsed)"
                type="danger"
                size="mini"
                :disabled="scope.row.IsEnable"
                @click="handleDelete(scope.row)"
              >
                {{ $t('trials:readingUnit:qsList:title:delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination style="text-align: right;margin-top: 10px;" class="page" :total="total" :page.sync="searchData.PageIndex" :limit.sync="searchData.PageSize" @pagination="getList" />
      </el-tab-pane>
      <!-- 全局阅片 -->
      <el-tab-pane :label="$t('dictionary:template:criterionConfig:tab:globalReview')" name="Global">
        <EvaluationOfGlobalConfig v-if="activeName === 'Global'" :data="data" :criterion-id="criterionId" />
      </el-tab-pane>
      <!-- 肿瘤学阅片 -->
      <el-tab-pane :label="$t('dictionary:template:criterionConfig:tab:oncologyReview')" name="Oncology">
        <EvaluationOfOncologyConfig v-if="activeName === 'Oncology'" :data="data" :criterion-id="criterionId" />
      </el-tab-pane>
    </el-tabs>
    <el-dialog
      v-if="addOrEdit.visible"
      :visible.sync="addOrEdit.visible"
      :close-on-click-modal="false"
      :title="`${addOrEdit.title}`"
      width="650px"
      append-to-body
      custom-class="base-dialog-wrapper"
    >
      <CriterionQuestionForm
        ref="addOrEdit"
        :data="rowData"
        :type="type"
        :criterionType="data.CriterionType"
        :criterionId="data.Id"
        @close="addOrEdit.visible = false"
        @getList="getList"
      />
    </el-dialog>

    <!-- 预览 -->
    <el-dialog
      v-if="preview.visible"
      v-dialogDrag
      :visible.sync="preview.visible"
      :close-on-click-modal="false"
      :title="preview.title"
      fullscreen
      append-to-body
    >
      <div>
        <ECRF
          :criterion-id="criterionId"
        />
      </div>
    </el-dialog>

    <el-dialog
      v-if="config.visible"
      :visible.sync="config.visible"
      :close-on-click-modal="false"
      :title="config.title"
      width="90%"
      append-to-body
    >
      <TableQsList
        :criterion-id="criterionId"
        :criterionType="data.CriterionType"
        :reading-question-id="rowData.Id"
        :is-complete-config="data.IsCompleteConfig"
        :lesion-type="rowData.LesionType"
      />
    </el-dialog>
  </div>
</template>
<script>
import { getReadingQuestionSystemList, deleteReadingQuestionSystem } from '@/api/dictionary'
import Pagination from '@/components/Pagination'
import CriterionQuestionForm from './CriterionQuestionForm'
import EvaluationOfOncologyConfig from './EvaluationOfOncologyConfig'
import EvaluationOfGlobalConfig from './EvaluationOfGlobalConfig'
import ECRF from './ECRF'
import TableQsList from './TableQsList'
const searchDataDefault = () => {
  return {
    QuestionName: '',
    PageIndex: 1,
    PageSize: 20
  }
}
export default {
  name: 'QcQuestions',
  components: { Pagination, CriterionQuestionForm, ECRF, TableQsList, EvaluationOfOncologyConfig, EvaluationOfGlobalConfig },
  props: {
    criterionId: {
      type: String,
      required: true
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      type: null,
      loading: false,
      rowData: {},
      activeName: '0',
      addOrEdit: { visible: false, title: '' },
      preview: { visible: false, title: 'eCRF' + this.$t('common:button:preview') },
      config: { visible: false, title: '' }
    }
  },
  watch: {
    list() {
      this.$nextTick(() => {
        this.$refs.criterionQSList.doLayout()
      })
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handlePreview() {
      window.open(`/ecrfPreview?SystemReadingCriterionId=${this.criterionId}&lang=${this.$i18n.locale}`)
    },
    getList() {
      this.loading = true
      this.searchData.ReadingQuestionCriterionSystemId = this.criterionId
      getReadingQuestionSystemList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      }).catch(() => { this.loading = false })
    },
    handleAdd() {
      this.rowData = { ReadingQuestionCriterionSystemId: this.criterionId, Id: ''}
      this.type = 'add'
      this.addOrEdit.title = this.$t('common:button:new')
      this.addOrEdit.visible = true
    }, 
    handleLook(row) {
      this.rowData = { ...row }
      this.type = 'look'
      this.addOrEdit.title = this.$t('common:button:view')
      this.addOrEdit.visible = true
    },
    handleEdit(row) {
      this.rowData = { ...row }
      this.type = 'edit'
      this.addOrEdit.title = this.$t('common:button:edit')
      this.addOrEdit.visible = true
    },
    handleDelete(row) {
      this.$confirm(this.$t('trials:staffResearch:message:confirmDel'), {
        type: 'warning',
        distinguishCancelAndClose: true
      })
        .then(() => {
          this.loading = true
          deleteReadingQuestionSystem(row.Id)
            .then(res => {
              this.loading = false
              if (res.IsSuccess) {
                this.getList()
                this.$message.success(this.$t('common:message:deletedSuccessfully'))
              }
            }).catch(() => { this.loading = false })
        })
    },
    handleConfig(row) {
      this.rowData = { ...row }
      this.config.title = `${this.$fd('LesionType', row.LesionType)}：${this.$t('trials:readingUnit:qsList:title:tableQs')}`
      this.config.visible = true
    },
    getStringResult(arr, dictionaryCode) {
      let newArr = arr.map(i => this.$fd(dictionaryCode, i))
      let delimiter = this.$i18n.locale === 'zh' ? '，' : ', '
      return newArr.join(delimiter)
    },
    // 查询
    handleSearch() {
      this.getList()
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    // 排序
    handleSortByColumn(column) {
      if (column.order === 'ascending') {
        this.searchData.Asc = true
      } else {
        this.searchData.Asc = false
      }
      this.searchData.SortField = column.prop
      this.searchData.PageIndex = 1
      this.getList()
    }
  }
}
</script>
<style lang="scss" scoped>
</style>

