<template>
  <div class="criterion-config">
    <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:qsNameEn')">
          <el-input v-model="searchData.QuestionName" clearable style="width:120px;" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleReset">{{ $t('common:button:reset') }}</el-button>
          <el-button type="primary" @click="handleSearch">{{ $t('common:button:search') }}</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="!data.IsFromSystemData && !data.IsCompleteConfig" size="mini" type="primary"
          @click="handleAdd">
          {{ $t('common:button:add') }}
        </el-button>

        <el-button v-if="!data.IsFromSystemData && !data.IsCompleteConfig" size="mini" type="primary"
          @click="handleApply">
          {{ $t('trials:readingUnit:qsList:button:apply') }}
        </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="QuestionName" :label="$t('trials:readingUnit:qsList:title:qName')" show-overflow-tooltip />
      <el-table-column prop="QuestionEnName" :label="$t('trials:readingUnit:qsList:title:qNameEn')"
        show-overflow-tooltip />
      <el-table-column prop="Type" :label="$t('trials:readingUnit:qsList:title:type')" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ $fd('ClinicalQuestionType', scope.row.ClinicalQuestionType) }}
        </template>
      </el-table-column>
      <el-table-column prop="TypeValue" :label="$t('trials:readingUnit:qsList:title:type')" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="GroupName" :label="$t('trials:readingUnit:qsList:title:groupName')" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="ClinicalQuestionShowEnum" :label="$t('trials:readingUnit:qsList:title:isShow')"
        show-overflow-tooltip>
        <template slot-scope="scope">
          {{ $fd('ShowQuestion', scope.row.ClinicalQuestionShowEnum) }}
        </template>
      </el-table-column>
      <el-table-column prop="IsRequired" :label="$t('trials:qcCfg:table:isRequired')" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ $fd('YesOrNo', scope.row.IsRequired) }}
        </template>
      </el-table-column>
      <el-table-column prop="" :label="$t('common:action:action')" min-width="250" show-overflow-tooltip fixed="right">
        <template slot-scope="scope">
          <el-button v-if="!data.IsFromSystemData && !data.IsCompleteConfig" 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:enrolledReviews:button:view') }}
          </el-button>
          <el-button type="primary" size="mini" :disabled="scope.row.ClinicalQuestionType !== 'table'"
            @click="handleConfig(scope.row)">
            {{ $t('trials:readingUnit:qsList:title:tableQs') }}
          </el-button>
          <el-button v-if="!data.IsFromSystemData && !data.IsCompleteConfig" type="danger" size="mini"
            :disabled="scope.row.IsEnable" @click="handleDelete(scope.row)">
            {{ $t('common:button: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-dialog v-if="addOrEdit.visible" :visible.sync="addOrEdit.visible" :close-on-click-modal="false"
      :title="addOrEdit.title" width="600px" append-to-body custom-class="base-dialog-wrapper">
      <ClinicalQuestionForm ref="addOrEdit" :clinical-id="clinicalId" :list="list" :clinical-info="data" :data="rowData"
        :type="type" @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>
      </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>
      <ClinicalQuestionTableQsList :clinical-id="clinicalId" :clinical-info="data" :data="rowData"
        :is-from-system-data="data.IsFromSystemData" :reading-question-id="rowData.Id"
        :is-complete-config="data.IsCompleteConfig" :lesion-type="rowData.LesionType" />
    </el-dialog>
  </div>
</template>
<script>
import { getTrialClinicalQuestionList, deleteTrialClinicalQuestion, applyTrialClinical } from '@/api/dictionary'
import Pagination from '@/components/Pagination'
import ClinicalQuestionTableQsList from './ClinicalQuestionTableQsList'
import ClinicalQuestionForm from "./ClinicalQuestionForm";
const searchDataDefault = () => {
  return {
    TrialClinicalId: null,
    QuestionName: '',
    PageIndex: 1,
    PageSize: 20
  }
}
export default {
  name: 'QcQuestions',
  components: { Pagination, ClinicalQuestionForm, ClinicalQuestionTableQsList },
  props: {
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      clinicalId: null,
      type: null,
      loading: false,
      rowData: {},
      activeName: '0',
      addOrEdit: { visible: false, title: '' },
      preview: { visible: false, title: this.$t('common:button:preview') },
      config: { visible: false, title: '' }
    }
  },
  watch: {
    list() {
      this.$nextTick(() => {
        this.$refs.criterionQSList.doLayout()
      })
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handlePreview() {
      this.$emit('handlePreview', this.data)
    },
    handleApply() {
      applyTrialClinical({
        TrialClinicalId: this.clinicalId
      }).then(res => {
        this.$message.success(this.$t('trials:adRules:message:msg9'))
        this.$emit('close')
      })
    },
    getList() {
      this.loading = true
      this.searchData.TrialClinicalId = this.data.Id
      this.clinicalId = this.data.Id
      getTrialClinicalQuestionList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      }).catch(() => { this.loading = false })
    },
    handleAdd() {
      this.rowData = { TrialClinicalId: this.data.Id, Id: '' }
      this.type = 'add'
      this.addOrEdit.title = this.$t('common:button:add')
      this.addOrEdit.visible = true
    },
    handleLook(row) {
      this.rowData = { ...row }
      this.type = 'look'
      this.addOrEdit.title = this.$t('trials:enrolledReviews: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
          deleteTrialClinicalQuestion(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.$t('trials:readingUnit:qsList:title:tableQs')
      this.config.visible = true
    },
    // 查询
    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>
