<template>
  <div style="position: relative">
    <el-form size="small" :inline="true" style="position: relative;">
      <!-- 阅片规则 -->
      <el-divider content-position="left">{{ $t('dictionary:template:globalConfig:readingRules') }}</el-divider>
      <!-- 是否必须全局阅片 -->
      <el-form-item :label="$t('dictionary:template:globalConfig:isMustGlobalReading')">
        {{ $fd('YesOrNo', IsMustGlobalReading) }}
      </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" style="position: relative">
      <!-- 名称 -->
      <el-divider content-position="left">{{ $t('dictionary:template:globalConfig:name') }}</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:globalConfig:updateType')" style="width: 100%">
      </el-form-item>
      <el-table v-loading="loading" style="width: 100%" :data="list" stripe>
        <!-- 序号 -->
        <el-table-column prop="" :label="$t('dictionary:template:globalConfig:order')" width="80">
          <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-column prop="IsBaseLineUse" :label="$t('dictionary:template:globalConfig:isBaseLineUse')"
          show-overflow-tooltip>
          <template slot-scope="scope">
            <!--              <el-switch-->
            <!--                @change="(v) => {BaseLineUseChange(v, scope.row)}"-->
            <!--                v-model="scope.row.IsBaseLineUse"-->
            <!--              >-->
            <!--              </el-switch>-->
            <el-tag v-if="scope.row.IsBaseLineUse" type="primary">{{ $fd('YesOrNo', scope.row.IsBaseLineUse) }}</el-tag>
            <el-tag v-else type="danger">{{ $fd('YesOrNo', scope.row.IsBaseLineUse) }}</el-tag>
          </template>
        </el-table-column>
        <!-- 是否随访评估 -->
        <el-table-column prop="IsFollowVisitUse" :label="$t('dictionary:template:globalConfig:isFollowVisitUse')"
          show-overflow-tooltip>
          <template slot-scope="scope">
            <!--              <el-switch-->
            <!--                @change="(v) => {FollowVisitUseChange(v, scope.row)}"-->
            <!--                v-model="scope.row.IsFollowVisitUse"-->
            <!--              >-->
            <!--              </el-switch>-->
            <el-tag v-if="scope.row.IsFollowVisitUse" type="primary">{{ $fd('YesOrNo', scope.row.IsFollowVisitUse)
            }}</el-tag>
            <el-tag v-else type="danger">{{ $fd('YesOrNo', scope.row.IsFollowVisitUse) }}</el-tag>
            <!--              <span>{{$fd('YesOrNo', scope.row.IsFollowVisitUse)}}</span>-->
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <base-model :config="config">
      <template slot="dialog-body">
        <el-form size="small" :inline="true" label-width="130px">
          <!-- 是否必须全局阅片 -->
          <el-form-item :label="$t('dictionary:template:globalConfig:isMustGlobalReading')"
            v-if="config.configType === 1">
            <el-radio-group v-model="form.IsMustGlobalReading">
              <el-radio v-for="item of $d.YesOrNo" :key="'form.IsMustGlobalReading' + item.value" :label="item.value">{{
                item.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <!-- 评估更新类型 @selection-change="handleSelectionChange"-->
          <el-form-item v-if="config.configType === 2" :label="$t('dictionary:template:globalConfig:updateType')"
            style="width: 100%">
          </el-form-item>
          <el-table v-if="config.configType === 2" v-loading="loading" ref="multipleTable" :data="GlobalAssessType"
            stripe>
            <!-- <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-column prop="IsBaseLineUse" :label="$t('dictionary:template:globalConfig:isBaseLineUse')"
              show-overflow-tooltip>
              <template slot-scope="scope">
                <el-switch v-model="scope.row.IsBaseLineUse" @change="() => $forceUpdate()">
                </el-switch>
                <span>{{ $fd('YesOrNo', scope.row.IsBaseLineUse) }}</span>
              </template>
            </el-table-column>
            <!-- 是否随访评估 -->
            <el-table-column prop="IsFollowVisitUse" :label="$t('dictionary:template:globalConfig:isFollowVisitUse')"
              show-overflow-tooltip>
              <template slot-scope="scope">
                <el-switch v-model="scope.row.IsFollowVisitUse" @change="() => $forceUpdate()">
                </el-switch>
                <span>{{ $fd('YesOrNo', scope.row.IsFollowVisitUse) }}</span>
              </template>
            </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 { getCriterionDictionary, getAssessType, setAssessType, setDictionaryBaseLineUse, setDictionaryFollowVisitUse, setSystemGlobalInfo, getSystemGlobalInfo } from '@/api/dictionary'
export default {
  props: {
    data: {
      type: Object,
      default() { return {} }
    },
    criterionId: {
      type: String,
      required: true
    }
  },
  components: { BaseModel },
  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('dictionary:template:globalConfig:updateType'), appendToBody: true, width: '600px', configType: 0 },
      selectedList: [],
      IsMustGlobalReading: false,
      form: {
        IsMustGlobalReading: false
      },
      GlobalAssessType: []
    }
  },
  beforecreate() {
    this.$d.GlobalAssessType.forEach(v => {
      v.IsBaseLineUse = false
      v.IsFollowVisitUse = false
    })
  },
  mounted() {
    this.getList()
    this.getSystemGlobalInfo()
    // this.getCriterionDictionary()
  },
  methods: {
    getCriterionDictionary() {
      getCriterionDictionary({
        ReadingCriterionId: this.criterionId,
        DictionaryCode: 'GlobalAssessType'
      }).then(res => {
        this.GlobalAssessType = res.Result.GlobalAssessType
      })
    },
    getSystemGlobalInfo() {
      getSystemGlobalInfo({
        SystemCriterionId: this.criterionId
      }).then(res => {
        this.GlobalAssessType = res.Result.DictionaryList
      })
    },
    FollowVisitUseChange(v, row) {
      this.loading = true
      setDictionaryFollowVisitUse({ Id: row.Id, IsFollowVisitUse: v }).then(res => {
        this.$message.success(this.$t('common:message:savedSuccessfully'))
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    BaseLineUseChange(v, row) {
      this.loading = true
      setDictionaryBaseLineUse({ Id: row.Id, isBaseLineUse: v }).then(res => {
        this.$message.success(this.$t('common:message:savedSuccessfully'))
        this.loading = false
      }).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) {
      console.log(this.selectedList, 'this.selectedList')
      this.selectedList = val
    },
    getList() {
      this.loading = true
      getSystemGlobalInfo({
        SystemCriterionId: this.criterionId,
      }).then(res => {
        this.loading = false
        this.list = res.Result.DictionaryList
        this.IsMustGlobalReading = res.Result.IsMustGlobalReading
        console.log(this.$d.GlobalAssessType)
      })
    },
    handleAdd(n) {
      this.config.configType = n
      this.form.IsMustGlobalReading = this.IsMustGlobalReading
      if (n === 1) {
        this.config.title = this.$t('dictionary:template:globalConfig:isMustGlobalReading')
        this.config.visible = true
      } else {
        this.config.title = this.$t('dictionary:template:globalConfig:updateType')
        this.config.visible = true
      }
    },
    handleSave() {
      this.loading = true
      var a = this.$d.GlobalAssessType.filter(v => {
        return !!this.list.find(v1 => {
          return v1.DictionaryId === v.id
        })
      })
      var params = {
        SystemCriterionId: this.criterionId,
        IsMustGlobalReading: this.form.IsMustGlobalReading,
        DictionaryList: this.config.configType === 1 ? this.list : this.GlobalAssessType.map(v => {
          return {
            DictionaryId: v.DictionaryId,
            IsBaseLineUse: v.IsBaseLineUse ? v.IsBaseLineUse : false,
            IsFollowVisitUse: v.IsFollowVisitUse ? v.IsFollowVisitUse : false,
          }
        }),
      }
      setSystemGlobalInfo(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>
