<template>
  <div>
    <div class="search-form" style="display:flex;justify-content: space-between;">
      <div>
      </div>
      <div>
        <el-button size="mini" type="primary" @click="handleAdd" style="margin-right: 10px;" v-if="!isCompleteConfig">
          {{ $t('dictionary:template:criterionDictionary:button:config') }}
        </el-button>
      </div>
    </div>
    <el-table v-loading="loading" v-adaptive="{ bottomOffset: 0 }" height="100" style="width: 100%" :data="list" stripe>
      <el-table-column prop="" label="" width="50">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <!-- 键值 -->
      <el-table-column prop="Code" :label="$t('dictionary:template:criterionDictionary:table:code')"
        show-overflow-tooltip />
      <!-- 中文值 -->
      <el-table-column prop="ValueCN" :label="$t('dictionary:template:criterionDictionary:table:valueCN')"
        show-overflow-tooltip>
      </el-table-column>
      <!-- 英文值 -->
      <el-table-column prop="Value" :label="$t('dictionary:template:criterionDictionary:table:value')"
        show-overflow-tooltip>
      </el-table-column>
      <!-- 描述 -->
      <el-table-column prop="Description" :label="$t('dictionary:template:criterionDictionary:table:description')"
        show-overflow-tooltip>
      </el-table-column>
      <!-- 分组 -->
      <el-table-column prop="Description" :label="$t('dictionary:template:criterionDictionary:table:group')"
        show-overflow-tooltip>
        <template slot-scope="scope">
          <div v-if="!isCompleteConfig">
            <el-radio-group v-model="scope.row.CrterionDictionaryGroup"
              @change="(v) => { crterionDictionaryGroupChange(v, scope.row) }" size="mini">
              <el-radio-button v-for="item of $d.CrterionDictionaryGroup" :label="item.value" :key="item.id">{{
                item.label }}</el-radio-button>
            </el-radio-group>
          </div>
          <div v-else>{{ $fd('CrterionDictionaryGroup', scope.row.CrterionDictionaryGroup) }}</div>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-if="config.visible" :visible.sync="config.visible" :close-on-click-modal="false" :title="config.title"
      width="90%" append-to-body>
      <div>
        <div style="text-align: right;">
          <el-button size="mini" type="primary" @click="handleSave" v-loading="loading">
            {{ $t('common:button:save') }}
          </el-button>
        </div>
        <el-table v-loading="loading" v-adaptive="{ bottomOffset: 100 }" height="100" ref="multipleTable"
          :data="$d[parentCode]" stripe @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55">
          </el-table-column>
          <!-- 键值 -->
          <el-table-column prop="raw.Code" :label="$t('dictionary:template:criterionDictionary:table:code')"
            show-overflow-tooltip>
          </el-table-column>
          <!-- 中文值 -->
          <el-table-column prop="raw.ValueCN" :label="$t('dictionary:template:criterionDictionary:table:valueCN')"
            show-overflow-tooltip>
          </el-table-column>
          <!-- 英文值 -->
          <el-table-column prop="raw.Value" :label="$t('dictionary:template:criterionDictionary:table:value')"
            show-overflow-tooltip>
          </el-table-column>
          <!-- 描述 -->
          <el-table-column prop="raw.Description"
            :label="$t('dictionary:template:criterionDictionary:table:description')" show-overflow-tooltip>
          </el-table-column>
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAssessType, setCriterionDictionary, setDictionaryBaseLineUse, setDictionaryFollowVisitUse, editCriterionDictionary } from '@/api/dictionary'
export default {
  props: {
    criterionId: {
      type: String,
      required: true
    },
    parentCode: {
      type: String,
      required: true
    },
    isCompleteConfig: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      loading: false,
      rowData: {},
      activeName: '0',
      addOrEdit: { visible: false, title: '' },
      preview: { visible: false, title: 'eCRF预览' },
      config: { visible: false, title: this.$t('template:criterionDictionary:message:msg2') }, //标准字典值
      selectedList: []
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    crterionDictionaryGroupChange(v, row) {
      this.loading = true
      editCriterionDictionary({
        Id: row.Id,
        CrterionDictionaryGroup: v
      }).then(res => {
        this.loading = false
        this.$message.success(this.$t('common:message:savedSuccessfully'))
        this.getList()
      }).catch(() => { this.loading = false })
    },
    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
      getAssessType({
        SystemCriterionId: this.criterionId,
        ParentCode: this.parentCode
      }).then(res => {
        this.loading = false
        this.list = res.Result
      })
    },
    handleAdd() {
      this.config.visible = true
      this.$nextTick(() => {
        var a = this.$d[this.parentCode].filter(v => {
          return !!this.list.find(v1 => {
            return v1.DictionaryId === v.id
          })
        })
        this.toggleSelection(a)
      })
    },
    handleSave() {
      this.loading = true
      var params = {
        CriterionId: this.criterionId,
        ParentCode: this.parentCode,
        DictionaryIds: this.selectedList.map(v => v.id),
        isSystemCriterion: true
      }
      setCriterionDictionary(params).then(res => {
        this.loading = false
        this.$message.success(this.$t('common:message:savedSuccessfully'))
        this.config.visible = false
        this.selectedList = []
        this.getList()
        this.$emit('getList')
      }).catch(() => {
      })
    },
    handleDelete() {

    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-form-item__content {
  width: calc(100% - 110px);
}
</style>
