<template>
  <div v-loading="loading">
    <div
      class="search-form"
      style="text-align: right;"
    >
      <el-button
        v-if="!isFromSystemData"
        size="mini"
        type="primary"
        @click="handleAdd"
      >
        {{ $t('common:button:new') }}
      </el-button>

    </div>

    <el-table
      :data="list"
      size="small"
      height="500"
    >
      <el-table-column
        prop="ShowOrder"
        :label="$t('trials:trials-config:clinicalDataQuestionCfg:table:orderNum')"
        min-width="70"
      />
      <el-table-column
        prop="QuestionName"
        :label="$t('trials:trials-config:clinicalDataQuestionCfg:table:qsName')"
        show-overflow-tooltip
      />
      <el-table-column
        prop="QuestionEnName"
        :label="$t('trials:trials-config:clinicalDataQuestionCfg:table:qsNameEn')"
        show-overflow-tooltip
      />
      <el-table-column
        prop="Type"
        :label="$t('trials:trials-config:clinicalDataQuestionCfg:table:type')"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ $fd('ClinicalTableQuestionType',scope.row.ClinicalTableQuestionType) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="TypeValue"
        :label="$t('trials:trials-config:clinicalDataQuestionCfg:table:options')"
        show-overflow-tooltip
        min-width="110"
      />
      <el-table-column
        prop="IsRequired"
        :label="$t('trials:trials-config:clinicalDataQuestionCfg:table:isRequired')"
        min-width="90"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ $fd('QuestionRequired',scope.row.IsRequired) }}
        </template>
      </el-table-column>
      <el-table-column
        prop=""
        :label="$t('common:action:action')"
        width="150"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-button
            v-if="isFromSystemData"
            type="primary"
            size="mini"
            @click="handleLook(scope.row)"
          >
            {{ $t('trials:trials-config:clinicalDataQuestionCfg:button:view') }}
          </el-button>
          <el-button
            v-if="!isFromSystemData && !isCompleteConfig"
            type="primary"
            size="mini"
            @click="handleEdit(scope.row)"
          >
            {{ $t('common:button:edit') }}
          </el-button>
          <el-button
            v-if="!isFromSystemData && !isCompleteConfig"
            type="danger"
            size="mini"
            @click="handleDelete(scope.row)"
          >
            {{ $t('common:button: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="600px"
      append-to-body
      custom-class="base-dialog-wrapper"
    >
      <ClinicalQuestionTableForm
        ref="addOrEdit"
        :clinical-id="clinicalId"
        :clinical-info="clinicalInfo"
        :list="list"
        :data="rowData"
        :type="type"
        :is-complete-config="isCompleteConfig"
        :reading-question-id="readingQuestionId"
        @close="addOrEdit.visible = false"
        @getList="getList"
      />
    </el-dialog>
  </div>
</template>
<script>
import { getTrialClinicalTableQuestionList, deleteTrialClinicalTableQuestion } from '@/api/dictionary'
import ClinicalQuestionTableForm from './ClinicalQuestionTableForm'
const searchDataDefault = () => {
  return {
    QuestionName: ''
  }
}
export default {
  name: 'TableQsList',
  components: { ClinicalQuestionTableForm },
  props: {
    isFromSystemData: {
      type: Boolean,
      required: true
    },
    readingQuestionId: {
      type: String,
      required: true
    },
    clinicalInfo: {
      type: Object,
      default() { return {} }
    },
    isCompleteConfig: {
      type: Boolean,
      required: true
    },
    clinicalId: {
      type: String,
      required: true
    }

  },
  data() {
    return {
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      rowData: {},
      addOrEdit: { visible: false, title: '' },
      type: null
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleLook(row) {
      this.type = 'look'
      this.rowData = { ...row }
      this.addOrEdit.title = this.$t('trials:trials-config:clinicalDataQuestionCfg:title:edit')
      this.addOrEdit.visible = true
    },
    getList() {
      this.loading = true
      getTrialClinicalTableQuestionList({ questionId: this.readingQuestionId, PageIndex: 1, PageSize: 999 }).then(res => {
        this.loading = false
        this.list = res.Result.CurrentPageData
        console.log(this.list)
      }).catch(() => { this.loading = false })
    },
    handleAdd() {
      this.type = 'add'
      this.rowData = {}
      this.addOrEdit.title = this.$t('trials:trials-config:clinicalDataQuestionCfg:title:add')
      this.addOrEdit.visible = true
    },
    handleEdit(row) {
      this.type = 'edit'
      this.rowData = { ...row }
      this.addOrEdit.title = this.$t('trials:trials-config:clinicalDataQuestionCfg:title:edit')
      this.addOrEdit.visible = true
    },
    handleDelete(row) {
      this.$confirm(this.$t('trials:staffResearch:message:confirmDel'), {
        type: 'warning',
        distinguishCancelAndClose: true
      })
        .then(() => {
          this.loading = true
          deleteTrialClinicalTableQuestion(row.Id)
            .then(res => {
              this.loading = false
              if (res.IsSuccess) {
                this.getList()
                this.$message.success(this.$t('trials:trials-config:clinicalDataQuestionCfg:msg:delSuccessfully'))
              }
            }).catch(() => { this.loading = false })
        })
    },
    // 查询
    handleSearch() {
      this.getList()
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    }
  }
}
</script>

