<template>
  <el-form
    ref="globalReadingForm"
    v-loading="loading"
    :model="form"
    size="small"
    :rules="rules"
    label-width="110px"
  >
    <!-- 评估更新类型 -->
    <el-form-item :label="$t('trials:readingUnit:globalReading:title:globalAssessTypes')">
      <div style="width: 700px;display: flex">
        <el-table
          ref="multipleTable"
          min-height="100"
          style="width: 600px;"
          :data="form.GlobalAssessTypes"
          stripe
        >
          <!-- 中文值 -->
          <el-table-column
            prop="ValueCN"
            :label="$t('trials:readingUnit:globalReading:title:valueCN')"
            show-overflow-tooltip
          />
          <!-- 英文值 -->
          <el-table-column
            prop="Value"
            :label="$t('trials:readingUnit:globalReading:title:valueEN')"
            show-overflow-tooltip
          />
          <!-- 是否基线评估 -->
          <el-table-column
            prop="IsBaseLineUse"
            :label="$t('trials:readingUnit:globalReading:title:isBaseLineUse')"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.IsBaseLineUse"
                :disabled="form.IsSystemCriterion || isSign || !hasPermi(['trials:trials-panel:setting:reading-unit:edit'])"
              />
              <span>{{ $fd('YesOrNo', scope.row.IsBaseLineUse) }}</span>
            </template>
          </el-table-column>
          <!-- 是否随访评估 -->
          <el-table-column
            prop="IsFollowVisitUse"
            :label="$t('trials:readingUnit:globalReading:title:isFollowVisitUse')"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.IsFollowVisitUse"
                :disabled="form.IsSystemCriterion || isSign || !hasPermi(['trials:trials-panel:setting:reading-unit:edit'])"
              />
              <span>{{ $fd('YesOrNo', scope.row.IsFollowVisitUse) }}</span>
            </template>
          </el-table-column>
        </el-table>
        <div style="display: flex;align-items: center;padding-left: 10px">
          <el-button
            :disabled="form.IsSystemCriterion || isSign || !hasPermi(['trials:trials-panel:setting:reading-unit:edit'])"
            icon="el-icon-plus"
            circle
            @click="handleSetCriterion"
          />
        </div>
      </div>
    </el-form-item>
    <el-form-item v-if="!isSign && hasPermi(['trials:trials-panel:setting:reading-unit:edit'])">
      <!-- 保存 -->
      <el-button
        size="small"
        type="primary"
        @click="handleSave(true)"
      >
        {{ $t('common:button:save') }}
      </el-button>
    </el-form-item>
    <el-dialog
      v-if="config.visible"
      :visible.sync="config.visible"
      :close-on-click-modal="false"
      :title="config.title"
      width="90%"
      append-to-body
    >
      <div style="text-align: right">
        <!-- 保存 -->
        <el-button
          v-loading="loading"
          size="mini"
          type="primary"
          @click="handleTypeSave"
        >
          {{ $t('common:button:save') }}
        </el-button>
      </div>
      <el-table
        ref="multipleTable"
        v-loading="loading"
        v-adaptive="{bottomOffset:80}"
        height="100"
        :data="$d.GlobalAssessType"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
        />
        <!-- 中文值 -->
        <el-table-column
          prop="raw.ValueCN"
          :label="$t('trials:readingUnit:globalReading:title:valueCN')"
          show-overflow-tooltip
        />
        <!-- 英文值 -->
        <el-table-column
          prop="raw.Value"
          :label="$t('trials:readingUnit:globalReading:title:valueEN')"
          show-overflow-tooltip
        />
      </el-table>
    </el-dialog>
  </el-form>
</template>

<script>
import { getTrialConfigGlobalReadingInfo, setGlobalReadingInfo } from '@/api/trials'
// import { getAssessType } from '@/api/dictionary'
export default {
  name: 'GlobalReading',
  props: {
    trialReadingCriterionId: {
      type: String,
      default() {
        return ''
      }
    }
  },
  data() {
    return {
      form: {
        TrialId: '',
        GlobalAssessTypes: [],
        IsSystemCriterion: true
      },
      rules: {
        GlobalAssessTypes: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur'] }]
      },
      loading: false,
      config: { visible: false, title: this.$t('trials:readingUnit:GlobalReading') }, // '全局阅片'
      isSign: true,
      selectedList: []
    }
  },
  mounted() {
    this.initForm()
  },
  methods: {
    handleSelectionChange(val) {
      this.selectedList = val
    },
    toggleSelection(rows) {
      console.log(this.$refs.multipleTable)
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSetCriterion() {
      this.config.visible = true
      this.$nextTick(() => {
        var a = this.$d.GlobalAssessType.filter(v => {
          return !!this.form.GlobalAssessTypes.find(v1 => {
            return v1.DictionaryId === v.id
          })
        })
        this.toggleSelection(a)
      })
    },
    handleTypeSave() {
      this.form.GlobalAssessTypes = this.selectedList.map(v => {
        var o = this.form.GlobalAssessTypes.find(v1 => v1.DictionaryId === v.id)
        if (o) {
          return o
        } else {
          return {
            DictionaryId: v.id,
            ValueCN: v.raw.ValueCN,
            Value: v.raw.Value,
            IsBaseLineUse: false,
            IsFollowVisitUse: false
          }
        }
      })
      this.config.visible = false
    },
    initForm() {
      this.loading = true
      getTrialConfigGlobalReadingInfo({ trialId: this.$route.query.trialId, TrialReadingCriterionId: this.trialReadingCriterionId }).then(res => {
        if (Object.keys(res.Result).length > 0) {
          for (const k in res.Result) {
            if (this.form.hasOwnProperty(k)) {
              this.form[k] = res.Result[k]
            }
          }
          this.isSign = res.Result.IsSign
        }
        this.form.GlobalAssessTypeIdsStr = this.form.GlobalAssessTypeIds.map(v => this.$fd('GlobalAssessType', v, 'id')).toString()
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleSave(isPrompt) {
      return new Promise((resolve, reject) => {
        this.$refs['globalReadingForm'].validate((valid) => {
          if (!valid) {
            resolve(false)
          } else {
            this.loading = true
            // 保存配置信息
            this.form.TrialId = this.$route.query.trialId
            this.form.TrialReadingCriterionId = this.trialReadingCriterionId
            var params = {
              GlobalAssessList: this.form.GlobalAssessTypes,
              TrialReadingCriterionId: this.trialReadingCriterionId,
              TrialId: this.$route.query.trialId
            }
            setGlobalReadingInfo(params).then(res => {
              this.loading = false
              if (res.IsSuccess && isPrompt) {
                this.$message.success(this.$t('common:message:savedSuccessfully'))
              }
              resolve(true)
            }).catch(_ => {
              this.loading = false
              resolve(false)
            })
          }
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
