<template>
  <BaseContainer>
    <!-- 搜索框 -->
    <template slot="search-container">
      <el-form :inline="true">
        <!-- 器官类型 -->
        <el-form-item :label="$t('trials:readingUnit:baseDataCfg:title:organType')">
          <el-select v-model="searchData.OrganType" clearable style="width:130px;">
            <el-option v-for="item of $d.OrganType" :key="item.id" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('trials:readingUnit:baseDataCfg:title:bodyPart')">
          <el-input v-model="searchData.Part" clearable style="width:130px;" />
        </el-form-item>
        <el-form-item :label="$t('trials:readingUnit:baseDataCfg:title:organ')">
          <el-input v-model="searchData.TULOC" clearable style="width:130px;" />
        </el-form-item>
        <el-form-item :label="$t('trials:readingUnit:baseDataCfg:title:isLymphNodes')">
          <el-select v-model="searchData.IsLymphNodes" clearable style="width:130px;">
            <el-option v-for="item of $d.YesOrNo" :key="item.id" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="handleSearch">
            {{ $t('common:button:search') }}
          </el-button>
          <el-button type="primary" icon="el-icon-refresh-left" size="small" @click="handleReset">
            {{ $t('common:button:reset') }}
          </el-button>
          <!-- 同步 -->
          <el-button type="primary" size="small" @click="synchronizeSystemOrganToTrial">
            {{ $t('trials:readingUnit:baseDataCfg:button:sync') }}
          </el-button>
        </el-form-item>
      </el-form>
      <span style="margin-left:auto;">
        <!-- 设置 -->
        <el-button
          v-if="!isConfirm && !isFromSystem"
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click="handleSet"
        >
          {{ $t('trials:readingUnit:baseDataCfg:button:set') }}
        </el-button>
      </span>
    </template>
    <template slot="main-container">
      <!-- 受试者列表 -->
      <el-table
        ref="organList"
        v-loading="loading"
        v-adaptive="{bottomOffset:60}"
        :data="list"
        stripe
        height="100"
        @sort-change="handleSortByColumn"
      >
        <el-table-column type="index" width="90" />
        <!-- 器官类型 -->
        <el-table-column
          prop="OrganType"
          :label="$t('trials:readingUnit:baseDataCfg:title:organType')"
          min-width="120"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            {{ $fd('OrganType', scope.row.OrganType) }}
          </template>

        </el-table-column>
        <!-- 分类 -->
        <el-table-column
          prop="Classification"
          :label="$t('trials:readingUnit:baseDataCfg:title:classification')"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            {{ $i18n.locale === 'zh' ? scope.row.Classification : scope.row.ClassificationEN }}
          </template>
        </el-table-column>
<!--        &lt;!&ndash; 分类（英文） &ndash;&gt;-->
<!--        <el-table-column-->
<!--          prop="ClassificationEN"-->
<!--          :label="$t('trials:readingUnit:baseDataCfg:title:classificationEn')"-->
<!--          min-width="140"-->
<!--          show-overflow-tooltip-->
<!--          sortable="custom"-->
<!--        />-->
        <!-- 部位 -->
        <el-table-column
          prop="Part"
          :label="$t('trials:readingUnit:baseDataCfg:title:bodyPart')"
          show-overflow-tooltip
          sortable="custom">
          <template slot-scope="scope">
            {{ $i18n.locale === 'zh' ? scope.row.Part : scope.row.PartEN }}
          </template>
        </el-table-column>
<!--        <el-table-column-->
<!--          prop="PartEN"-->
<!--          :label="$t('trials:readingUnit:baseDataCfg:title:bodyPartEn')"-->
<!--          min-width="140"-->
<!--          show-overflow-tooltip-->
<!--          sortable="custom"-->
<!--        />-->
        <!-- 器官 -->
        <el-table-column
          prop="TULOC"
          :label="$t('trials:readingUnit:baseDataCfg:title:organ')"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            {{ $i18n.locale === 'zh' ? scope.row.TULOC : scope.row.TULOCEN }}
          </template>
        </el-table-column>
        <!-- 位置 -->
        <el-table-column
          prop="TULAT"
          :label="$t('trials:readingUnit:baseDataCfg:title:TULAT')"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            {{ $i18n.locale === 'zh' ? scope.row.TULAT : scope.row.TULATEN }}
          </template>
        </el-table-column>
        <!-- 是否淋巴结 -->
        <el-table-column
          prop="IsLymphNodes"
          :label="$t('trials:readingUnit:baseDataCfg:title:isLymphNodes')"
          min-width="160"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsLymphNodes" type="danger">
              {{ $fd('ReadingYesOrNo', scope.row.IsLymphNodes) }}
            </el-tag>
            <el-tag v-else type="primary">{{ $fd('ReadingYesOrNo', scope.row.IsLymphNodes) }}</el-tag>
          </template>
        </el-table-column>
        <!-- 是否可以编辑 -->
        <el-table-column
          prop="IsCanEditPosition"
          :label="$t('trials:readingUnit:baseDataCfg:title:isCanEditPosition')"
          min-width="160"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsCanEditPosition" type="danger">
              {{ $fd('YesOrNo', scope.row.IsCanEditPosition) }}
            </el-tag>
            <el-tag v-else type="primary">{{ $fd('YesOrNo', scope.row.IsCanEditPosition) }}</el-tag>
          </template>
        </el-table-column>
        <!-- 序号 -->
        <el-table-column
          prop="ShowOrder"
          :label="$t('trials:readingUnit:baseDataCfg:title:showOrder')"
          show-overflow-tooltip
          min-width="80"
        />
        <!-- 是否显示 -->
        <el-table-column
          prop="IsEnable"
          :label="$t('trials:readingUnit:baseDataCfg:title:isEnable')"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-switch
              v-if="!isConfirm"
              v-model="scope.row.IsEnable"
              @change="changeEnableStatus($event, scope.row)"
            />
            <span v-else>{{ $fd('YesOrNo',scope.row.IsEnable) }}</span>
          </template>
        </el-table-column>
      </el-table>

    </template>

    <!-- 选择标准 -->
    <el-dialog
      v-if="criterion.visible"
      :visible.sync="criterion.visible"
      :close-on-click-modal="false"
      :title="criterion.title"
      width="500px"
      custom-class="base-dialog-wrapper"
      append-to-body
    >
      <el-form
        ref="criterionForm"
        v-loading="formLoading"
        :model="form"
        size="small"
      >
        <div class="base-dialog-body">
          <!-- 阅片标准 -->
          <el-form-item
            :label="$t('trials:readingUnit:readingCriterion')"
            prop="CriterionId"
            :rules="[
              { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur'] },
            ]"
          >
            <el-select
              v-model="form.CriterionId"
            >
              <el-option
                v-for="item of criterions"
                :key="item.CriterionId"
                :label="item.CriterionName"
                :value="item.CriterionId"
              />
            </el-select>
          </el-form-item>
        </div>
        <div class="base-dialog-footer" style="text-align:right;margin-top:10px;">
          <el-form-item style="text-align:right;">
            <el-button type="primary" @click="criterion.visible = false"> {{ $t('common:button:cancel') }}</el-button>
            <el-button size="small" type="primary" @click="handleSave">
              {{ $t('common:button:save') }}
            </el-button>
          </el-form-item>
        </div>
      </el-form>
    </el-dialog>
  </BaseContainer>
</template>
<script>
import { getTrialOrganList, deleteTrialOrganInfo, getSystemCriterionList, synchronizeSystemOrganToTrial, setOrganIsEnable } from '@/api/trials'
import BaseContainer from '@/components/BaseContainer'
const searchDataDefault = () => {
  return {
    OrganType: null,
    TULOC: null,
    TULAT: null,
    IsLymphNodes: null
  }
}
export default {
  name: 'OrgansList',
  components: { BaseContainer },
  props: {
    trialReadingCriterionId: {
      type: String,
      required: true
    },
    isFromSystem: {
      type: Boolean,
      required: true
    },
    isConfirm: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      searchData: searchDataDefault(),
      loading: false,
      list: [],
      total: 0,
      trialId: '',
      criterion: { title: this.$t('trials:readingUnit:baseDataCfg:title:set'), visible: false },
      formLoading: false,
      form: { CriterionId: '' },
      criterions: []
    }
  },
  watch: {
    list() {
      this.$nextTick(() => {
        this.$refs.organList.doLayout()
      })
    }
  },
  mounted() {
    this.getCriterions()
    this.getList()
  },
  methods: {
    synchronizeSystemOrganToTrial() {
      this.loading = true
      synchronizeSystemOrganToTrial({
        TrialReadingCriterionId: this.trialReadingCriterionId
      }).then(res => {
        this.loading = false
        this.getList()
        this.$message.success(this.$t('trials:readingUnit:baseDataCfg:message:sync'))// '同步成功'
      }).catch(() => {
        this.loading = false
      })
    },
    // 获取受试者列表
    getList() {
      this.loading = true
      this.searchData.TrialId = this.$route.query.trialId
      this.searchData.TrialReadingCriterionId = this.trialReadingCriterionId
      getTrialOrganList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result
        // this.list = res.Result.CurrentPageData
        // this.total = res.Result.TotalCount
      }).catch(() => { this.loading = false })
    },
    // 新增
    handleAdd() {
      this.addDialog.visible = true
    },
    changeEnableStatus(callback, row) {
      if (callback) {
        row.IsEnable = false
      } else {
        row.IsEnable = true
      }
      var message = this.$t('trials:readingUnit:baseDataCfg:message:updateConfirm')// '是否确认更改？'
      this.$confirm(message, {
        distinguishCancelAndClose: true,
        type: 'warning'
      }).then(() => {
        const state = !row.IsEnable
        this.loading = true
        var params = {
          isEnable: state,
          ids: [row.Id]
        }
        setOrganIsEnable(params).then(res => {
          this.loading = false
          if (res.IsSuccess) {
            this.$message.success(this.$t('common:message:savedSuccessfully'))
            this.getList()
          }
        }).catch(() => { this.loading = false })
      }).catch(() => {})
    },
    // 删除
    handleDelete(row) {
      // '是否确认删除'
      this.$confirm(this.$t('trials:readingUnit:baseDataCfg:message:deleteConfirm'), {
        type: 'warning',
        distinguishCancelAndClose: true
      })
        .then(() => {
          this.loading = true
          deleteTrialOrganInfo(row.Id)
            .then(res => {
              this.loading = false
              if (res.IsSuccess) {
                this.getList()
                // 删除成功！
                this.$message.success(this.$t('trials:readingUnit:baseDataCfg:message:deleteSuccessfully'))
              }
            }).catch(() => { this.loading = false })
        })
    },
    // 查询
    handleSearch() {
      // this.searchData.PageIndex = 1
      this.getList()
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    // 排序
    handleSortByColumn(column) {
      if (column.order === 'ascending') {
        this.searchData.Asc = true
      } else {
        this.searchData.Asc = false
      }
      this.searchData.SortField = column.prop
      this.searchData.PageIndex = 1
      this.getList()
    },
    getCriterions() {
      getSystemCriterionList().then(res => {
        this.criterions = res.Result
      })
    },
    handleSet() {
      this.criterion.visible = true
      this.form.CriterionId = ''
    },
    handleSave() {
      this.$refs['criterionForm'].validate((valid) => {
        if (!valid) return
        this.formLoading = true
        var param = {
          trialId: this.$route.query.trialId,
          systemCriterionId: this.form.CriterionId
        }
        synchronizeSystemOrganToTrial(param).then(res => {
          this.getList()
          this.criterion.visible = false
          this.formLoading = false
        }).catch(() => {
          this.formLoading = false
        })
      })
    }
  }
}
</script>
