<template>
  <BaseContainer>
    <!-- 搜索框 -->
    <template slot="search-container">
      <el-form :inline="true" size="mini">
        <!-- 器官类型 -->
        <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 CriterionDictionaryOrganTypeList && CriterionDictionaryOrganTypeList.OrganType" :key="item.Id" :value="parseInt(item.Code)" :label="item.ValueCN" />
          </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.ReadingYesOrNo" :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="mini" @click="handleSearch">
            {{ $t('common:button:search') }}
          </el-button>
          <!-- 重置 -->
          <el-button type="primary" icon="el-icon-refresh-left" size="mini" @click="handleReset">
            {{ $t('common:button:reset') }}
          </el-button>
        </el-form-item>
      </el-form>
      <span style="margin-left:auto">
        <!-- 设置 -->
        <el-button
          v-if="!isCompleteConfig"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleSet"
        >
          {{ $t('trials:readingUnit:baseDataCfg:button:set') }}
        </el-button>
        <!-- <el-button

          type="primary"
          size="mini"
          @click="lesionType.visible = true"
        >
          病灶类型配置
        </el-button> -->
        <el-button
          type="primary"
          size="mini"
          @click="lesionType.visible = true"
        >
          {{ this.$t('dictionary:template:basicData:config') }}
        </el-button>
        <!--        <el-button-->
        <!--          v-if="!isCompleteConfig"-->
        <!--          type="primary"-->
        <!--          size="mini"-->
        <!--          @click="handleSet"-->
        <!--        >-->
        <!--          默认数据配置-->
        <!--        </el-button>-->
        <el-button
          v-if="!isCompleteConfig"
          type="primary"
          size="mini"
          @click="handleAdd"
        >
          {{ $t('common:button:new') }}
        </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="140"
          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"
          min-width="140"
        />
        <el-table-column
          prop="ClassificationEN"
          :label="$t('trials:readingUnit:baseDataCfg:title:classificationEn')"
          min-width="160"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- 部位 -->
        <el-table-column
          prop="Part"
          :label="$t('trials:readingUnit:baseDataCfg:title:bodyPart')"
          show-overflow-tooltip
          sortable="custom"
          min-width="120"
        />
        <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"
          min-width="120"
        />
        <el-table-column
          prop="TULOCEN"
          :label="$t('trials:readingUnit:baseDataCfg:title:TULOCEN')"
          min-width="140"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- 位置 -->
        <el-table-column
          prop="TULAT"
          :label="$t('trials:readingUnit:baseDataCfg:title:TULAT')"
          show-overflow-tooltip
          sortable="custom"
          min-width="120"
        />
        <el-table-column
          prop="TULATEN"
          :label="$t('trials:readingUnit:baseDataCfg:title:TULATEn')"
          min-width="140"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- 是否淋巴结 -->
        <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="Remark"
          :label="$t('trials:readingUnit:baseDataCfg:title:remark')"
          show-overflow-tooltip
        />

        <el-table-column
          v-if="!isCompleteConfig"
          :label="$t('common:action:action')"
          width="120"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleEdit(scope.row)"
            >
              {{ $t('common:button:edit') }}
            </el-button>
            <el-button
              type="text"
              @click="handleDelete(scope.row)"
            > 
              {{ $t('common:button:delete') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <pagination class="page" :total="total" :page.sync="searchData.PageIndex" :limit.sync="searchData.PageSize" @pagination="getList" />
    </template>

    <!-- 新增/编辑 -->
    <el-dialog
      v-if="editDialog.visible"
      :visible.sync="editDialog.visible"
      :close-on-click-modal="false"
      :title="editDialog.title"
      width="600px"
      custom-class="base-dialog-wrapper"
      append-to-body
    >
      <OrganForm
        :data="rowData"
        :criterion-id="criterionId"
        @close="editDialog.visible = false"
        @getList="getList"
      />
    </el-dialog>

    <!-- 病灶类型 -->
    <el-dialog
      v-if="lesionType.visible"
      :visible.sync="lesionType.visible"
      :close-on-click-modal="false"
      :title="lesionType.title"
      width="800px"
      custom-class="base-dialog-wrapper"
      append-to-body
    >
      <LesionTypeTbl
        :criterion-id="criterionId"
        :is-complete-config="isCompleteConfig"
        @close="lesionType.visible = false"
      />
    </el-dialog>

    <!-- 选择标准 -->
    <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:auditRecord:table:criterion')"
            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"
                :disabled="criterionId === 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 { getOrganPageList, deleteOrganInfo, getCriterionNidusList, copySystemCriterionData, synchronizeSystemOrgan, getCriterionDictionary } from '@/api/dictionary'
import { getSystemCriterionList, synchronizeSystemOrganToTrial } from '@/api/trials'
import BaseContainer from '@/components/BaseContainer'
import Pagination from '@/components/Pagination'
import OrganForm from './OrganForm'
import LesionTypeTbl from './LesionTypeTbl'
const searchDataDefault = () => {
  return {
    SystemCriterionId: '',
    OrganType: null,
    PageIndex: 1,
    PageSize: 20,
    TULOC: null,
    TULAT: null,
    IsLymphNodes: null
  }
}
export default {
  name: 'OrgansList',
  components: { BaseContainer, Pagination, OrganForm, LesionTypeTbl },
  props: {
    criterionId: {
      type: String,
      required: true
    },
    isCompleteConfig: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      searchData: searchDataDefault(),
      loading: false,
      list: [],
      total: 0,
      rowData: {},
      editDialog: { title: '', visible: false },
      lesionType: { title: this.$t('dictionary:template:basicData:title1'), visible: false },//病灶器官关联关系
      criterion: { title: this.$t('dictionary:template:basicData:title2'), visible: false },//默认数据配置
      formLoading: false,
      form: { CriterionId: '' },
      criterions: [],
      CriterionDictionaryOrganTypeList: null
    }
  },
  watch: {
    list() {
      this.$nextTick(() => {
        if(!this.$refs.organList) return
        this.$refs.organList.doLayout()
      })
    }
  },
  mounted() {
    this.getCriterions()
    this.getList()
    this.getCriterionDictionaryOrganType()
  },
  methods: {
    getCriterionDictionaryOrganType() {
      getCriterionDictionary({
        ReadingCriterionId: this.criterionId,
        DictionaryCode: 'OrganType'
      }).then(res => {
        this.CriterionDictionaryOrganTypeList = res.Result
      })
    },
    synchronizeSystemOrganToTrial() {
      this.loading = true
      synchronizeSystemOrganToTrial({
        TrialReadingCriterionId: this.criterionId
      }).then(res => {
        this.loading = false
        this.$message.success(this.$t('dictionary:template:basicData:message1'))
      }).catch(() => {
        this.loading = false
      })
    },
    // 获取受试者列表
    getList() {
      this.loading = true
      this.searchData.SystemCriterionId = this.criterionId
      getOrganPageList(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 })
    },
    getLesionTypes() {
      return new Promise((resolve, reject) => {
        getCriterionNidusList({ criterionId: this.criterionId }).then(res => {
          this.lesionTypes = res.Result
          resolve()
        }).catch(() => {
          reject()
        })
      })
    },
    // 新增
    handleAdd() {
      this.rowData = { SystemCriterionId: this.criterionId }
      this.editDialog.title = this.$t('common:button:new')
      this.editDialog.visible = true
    },
    // 编辑
    handleEdit(row) {
      this.rowData = { ...row }
      this.editDialog.title = this.$t('common:button:edit')
      this.editDialog.visible = true
    },
    // 删除
    handleDelete(row) {
      this.$confirm(this.$t('trials:readingUnit:baseDataCfg:message:deleteConfirm'), {
        type: 'warning',
        distinguishCancelAndClose: true
      })
        .then(() => {
          this.loading = true
          deleteOrganInfo(row.Id)
            .then(res => {
              this.loading = false
              if (res.IsSuccess) {
                this.getList()
                this.$message.success(this.$t('common:message:deletedSuccessfully'))
              }
            }).catch(() => { this.loading = false })
        })
    },
    getCriterions() {
      getSystemCriterionList().then(res => {
        this.criterions = res.Result
      })
    },
    // 查询
    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()
    },
    handleSet() {
      this.criterion.visible = true
      this.form.CriterionId = ''
    },
    handleSave() {
      this.$refs['criterionForm'].validate((valid) => {
        if (!valid) return
        this.formLoading = true
        var param = {
          FromCriterionId: this.form.CriterionId,
          ToCriterionId: this.criterionId
        }
        synchronizeSystemOrgan(param).then(res => {
          this.getList()
          this.criterion.visible = false
          this.formLoading = false
        }).catch(() => {
          this.formLoading = false
        })
      })
    }
  }
}
</script>
