<template>
  <div>
    <el-form  size="small" :inline="true"  label-width="110px" style="position: relative;">
      <!-- 阅片规则 -->
      <el-divider content-position="left">{{ $t('dictionary:template:globalConfig:readingRules') }}</el-divider>
      <!-- 是否肿瘤学阅片 -->
      <el-form-item :label="$t('dictionary:template:oncologyConfig:isOncologyReading')" style="width: 100%">
        {{ $fd('YesOrNo', IsOncologyReading)}}
      </el-form-item>
      <!-- 配置 -->
      <el-button
        size="mini"
        type="primary"
        style="position: absolute;right: 10px;top: 15px"
        v-if="Object.keys(data).length > 0 && !(data.IsCompleteConfig || data.IsBeUsed)"
        @click="handleAdd(1)"
      >
        {{ $t('dictionary:template:globalConfig:config') }}
      </el-button>
    </el-form>
    <el-form  size="small" :inline="true"  label-width="110px" style="position: relative">
      <!-- 阅片问题 -->
      <el-divider content-position="left">{{ $t('dictionary:template:oncologyConfig:reviewQs') }}</el-divider>
      <!-- 配置 -->
      <el-button
        size="mini"
        type="primary"
        style="position: absolute;right: 10px;top: 15px"
        v-if="Object.keys(data).length > 0 && !(data.IsCompleteConfig || data.IsBeUsed)"
        @click="handleAdd(2)"
      >
        {{ $t('dictionary:template:globalConfig:config') }}
      </el-button>
      <!-- 肿瘤学阅片结果 -->
      <el-form-item :label="$t('dictionary:template:oncologyConfig:reviewResult')" style="width: 100%">
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      style="width: 100%"
      :data="list"
      stripe
    >
      <!-- 序号 -->
      <el-table-column
        prop=""
        :label="$t('dictionary:template:globalConfig:order')"
        width="50"
      >
        <template slot-scope="scope">
          {{scope.$index + 1}}
        </template>
      </el-table-column>
      <!-- 中文值 -->
      <el-table-column
        prop="ValueCN"
        :label="$t('dictionary:template:globalConfig:valueCN')"
        show-overflow-tooltip
      >
      </el-table-column>
      <!-- 英文值 -->
      <el-table-column
        prop="Value"
        :label="$t('dictionary:template:globalConfig:value')"
        show-overflow-tooltip
      >
      </el-table-column>
    </el-table>
    <base-model :config="config">
      <template slot="dialog-body">
        <el-form  size="small" :inline="true"  label-width="110px">
          <!-- 是否肿瘤学阅片 -->
          <el-form-item :label="$t('dictionary:template:oncologyConfig:isOncologyReading')" v-if="config.configType === 1">
            <el-radio-group v-model="form.IsOncologyReading">
              <el-radio v-for="item of $d.YesOrNo" :key="'form.IsOncologyReading' + item.value" :label="item.value">{{ item.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <!-- 肿瘤学阅片结果 -->
          <el-form-item :label="$t('dictionary:template:oncologyConfig:reviewResult')" style="width: 100%" v-if="config.configType === 2">
          </el-form-item>
          <el-table
            v-if="config.configType === 2"
            v-loading="loading"
            style="width: 100%"
            ref="multipleTable"
            :data="OncologyAssessType"
            stripe
            @selection-change="handleSelectionChange"
          >
            <el-table-column
              type="selection"
              width="55">
            </el-table-column>
            <!-- 中文值 -->
            <el-table-column
              prop="ValueCN"
              :label="$t('dictionary:template:globalConfig:valueCN')"
              show-overflow-tooltip
            >
            </el-table-column>
            <!-- 英文值 -->
            <el-table-column
              prop="Value"
              :label="$t('dictionary:template:globalConfig:value')"
              show-overflow-tooltip
            >
            </el-table-column>
          </el-table>
        </el-form>
      </template>
      <template slot="dialog-footer">
        <el-button :disabled="loading" size="small" type="primary" @click="config.visible = false">
          {{ $t('common:button:cancel') }}
        </el-button>
        <el-button size="small" type="primary" :loading="loading" @click="handleSave">
          {{ $t('common:button:save') }}
        </el-button>
      </template>
    </base-model>
  </div>
</template>

<script>
import BaseModel from '@/components/BaseModel'
import { getSystemOncologyInfo, setSystemOncologyInfo, getCriterionDictionary } from '@/api/dictionary'
export default {
  props: {
    data: {
      type: String,
      default() { return {} }
    },
    criterionId: {
      type: String,
      required: true
    }
  },
  components: { BaseModel },
  data() {
    return {
      form: {
        IsOncologyReading: true
      },
      list: [],
      total: 0,
      loading: false,
      rowData: {},
      activeName: '0',
      addOrEdit: { visible: false, title: '' },
      config: { visible: false, title: this.$t('dictionary:template:oncologyConfig:reviewResult'), appendToBody: true, width: '600px', configType: 0 },
      selectedList: [],
      OncologyAssessType: [],
      IsOncologyReading: true
    }
  },
  mounted() {
    this.getList()
    this.getCriterionDictionary()
  },
  methods: {
    getCriterionDictionary() {
      getCriterionDictionary({
        ReadingCriterionId: this.criterionId,
        DictionaryCode: 'OncologyAssessType'
      }).then(res => {
        this.OncologyAssessType = res.Result.OncologyAssessType
      })
    },
    toggleSelection(rows) {
      console.log(this.$refs.multipleTable)
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.selectedList = val
    },
    getList() {
      this.loading = true
      getSystemOncologyInfo({
        SystemCriterionId: this.criterionId
      }).then(res => {
        this.loading = false
        this.list = res.Result.DictionaryList
        this.IsOncologyReading = res.Result.IsOncologyReading
        this.form.IsOncologyReading = res.Result.IsOncologyReading
        console.log(this.$d.OncologyAssessType)
      })
    },
    handleAdd(n) {
      this.config.configType = n
      if (n === 1) {
        this.config.title = this.$t('dictionary:template:oncologyConfig:isOncologyReading')
        this.config.visible = true
      } else {
        this.config.title = this.$t('dictionary:template:oncologyConfig:reviewResult')
        this.config.visible = true
        this.$nextTick(() => {
          var a = this.OncologyAssessType.filter(v => {
            return !!this.list.find(v1 => {
              return v1.DictionaryId === v.Id
            })
          })
          this.toggleSelection(a)
        })
      }
    },
    handleSave() {
      this.loading = true
      var a = this.OncologyAssessType.filter(v => {
        return !!this.list.find(v1 => {
          return v1.DictionaryId === v.Id
        })
      })
      var params = {
        SystemCriterionId: this.criterionId,
        ParentCode: 'OncologyAssessType',
        DictionaryIds: this.config.configType === 1 ? a.map(v => v.Id) : this.selectedList.map(v => v.Id),
        IsSystemCriterion: true,
        IsOncologyReading: this.form.IsOncologyReading
      }
      setSystemOncologyInfo(params).then(res => {
        this.loading = false
        this.$message.success(this.$t('common:message:savedSuccessfully'))
        this.config.visible = false
        this.selectedList = []
        this.getList()
      }).catch(() => {
      })
    },
    handleDelete() {

    }
  }
}
</script>
