<template>
  <BaseContainer>
    <!-- <template slot="search-container">
      <el-button
        v-hasPermi="['trials:trials-panel:enrolled-reviewers:list:enroll']"
        icon="el-icon-plus"
        type="primary"
        style="margin-left: auto"
        @click="handleEnroll"
      >Enroll</el-button>
    </template> -->

    <template slot="main-container">
      <!-- 入组医生列表 -->
      <el-table
        ref="myTable"
        v-loading="listLoading"
        v-adaptive="{bottomOffset:55}"
        :data="list"
        stripe
        height="100"
        @sort-change="handleSortByColumn"
      >
        <el-table-column type="index" width="40" />
        <!-- Name -->
        <el-table-column
          :label="$t('trials:enrolledReviews:table:name')"
          show-overflow-tooltip
          width="150"
          prop="FirstName"
          sortable="custom"
        >
          <template slot-scope="scope">
            <router-link
              style="color: #428bca;"
              tag="a"
              :to="{
                path: `/trialsResume?doctorId=${scope.row.DoctorId}&token=${token}`,
              }"
              target="_blank"
            >{{ scope.row.LastName }} / {{ scope.row.FirstName }}</router-link>
          </template>
        </el-table-column>
        <!-- Name CN -->
        <el-table-column
          prop="ChineseName"
          sortable="custom"
          :label="$t('trials:enrolledReviews:table:nameCN')"
          width="120"
          show-overflow-tooltip
        />
        <!-- 用户名 -->
        <el-table-column
          prop="UserName"
          :label="$t('trials:enrolledReviews:table:userName')"
          width="150"
          sortable="custom"
          show-overflow-tooltip
        />
        <!-- 入组时间 -->
        <el-table-column
          prop="EnrollTimeStr"
          :label="$t('trials:enrolledReviews:table:enrollmentTime')"
          width="200"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span v-if="scope.row.EnrollTime">
              {{ `${scope.row.EnrollTime}` }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="EnrollTimeStr"
          :label="$t('trials:enrolledReviews:table:status')"
          width="100"
          sortable="custom"
        >
          <template slot-scope="scope">
            <div>
              <el-switch
                v-model="scope.row.IsEnable"
                :active-value="true"
                :inactive-value="false"
                @change="(v) => {return isEnableChange(scope.row, v)}"
              />
              <span>{{$fd('IsEnable', scope.row.IsEnable)}}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          v-for="ite of TrialReadingCriterionList"
          :label="ite.TrialReadingCriterionName"
          width="360"
          header-align="center"
          show-overflow-tooltip
        >
          <el-table-column
            :label="$t('trials:enrolledReviews:table:readingType')"
            width="300"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <span v-for="item of scope.row.CriterionCategoryList.find(v => {return v.TrialReadingCriterionId === ite.TrialReadingCriterionId}) ? scope.row.CriterionCategoryList.find(v => {return v.TrialReadingCriterionId === ite.TrialReadingCriterionId}).ReadingCategorys : []">
                <el-tag v-if="item === 1" type="primary">{{ $fd('ReadingCategory', item) + ' & ' + $fd('ReadingCategory', 2) }}</el-tag>
                <!--              <el-tag v-if="item === 2" type="info">{{ $fd('ReadingCategory', item) }}</el-tag>-->
                <el-tag v-if="item === 4" type="danger">{{ $fd('ReadingCategory', item) }}</el-tag>
                <el-tag v-if="item === 5" type="warning">{{ $fd('ReadingCategory', item) }}</el-tag>
              </span>
              <el-button
                v-hasPermi="['trials:trials-panel:enrolled-reviewers:list:edit']"
                type="text"
                @click="openSetEnrollReadingCategory(scope.row, ite.TrialReadingCriterionId)"
              >
                {{ $t('trials:enrolledReviews:button:config') }}
              </el-button>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('trials:enrolledReviews:message:SOW')"
            width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <div v-if="scope.row.CriterionCategoryList.find(v => {return v.TrialReadingCriterionId === ite.TrialReadingCriterionId}).StatementCriterionFileList.length > 0">
                <el-button type="text" @click="windowOpen(scope.row.CriterionCategoryList.find(v => {return v.TrialReadingCriterionId === ite.TrialReadingCriterionId}).StatementCriterionFileList[0].FilePath)">View</el-button>
              </div>
              <div v-else>
                <span>{{ $t('trials:enrolledReviews:label:notUpload') }}</span>
                <!-- 上传 -->
                <el-button
                  v-if="ite.CriterionType === 0"
                  v-hasPermi="['trials:trials-panel:enrolled-reviewers:list:edit']"
                  type="text"
                  style="margin-left: 10px"
                  @click="addCol(0, scope.row, ite, $t('trials:enrolledReviews:message:SOW'))"
                >
                  {{ $t('trials:enrolledReviews:button:upload') }}
                </el-button>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('trials:enrolledReviews:message:EQC')"
            width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <div v-if="scope.row.CriterionCategoryList.find(v => {return v.TrialReadingCriterionId === ite.TrialReadingCriterionId}).AcknowledgementCriterionFileList.length > 0">
                <el-button type="text" @click="windowOpen(scope.row.CriterionCategoryList.find(v => {return v.TrialReadingCriterionId === ite.TrialReadingCriterionId}).AcknowledgementCriterionFileList[0].FilePath)">View</el-button>
              </div>
              <div v-else>
                <span>{{ $t('trials:enrolledReviews:label:notUpload') }}</span>
                <!-- 上传 -->
                <el-button
                  v-if="ite.CriterionType === 0"
                  v-hasPermi="['trials:trials-panel:enrolled-reviewers:list:edit']"
                  type="text"
                  style="margin-left: 10px"
                  @click="addCol(1, scope.row, ite, $t('trials:enrolledReviews:message:SOW'))"
                >
                  {{ $t('trials:enrolledReviews:button:upload') }}
                </el-button>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('trials:enrolledReviews:button:tackNum')"
            width="300"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <div>
                {{ scope.row.CriterionCategoryList.find(v => {return v.TrialReadingCriterionId === ite.TrialReadingCriterionId}).PendingCount }}
                /
                {{ scope.row.CriterionCategoryList.find(v => {return v.TrialReadingCriterionId === ite.TrialReadingCriterionId}).ComplectedCount }}
                /
                {{ scope.row.CriterionCategoryList.find(v => {return v.TrialReadingCriterionId === ite.TrialReadingCriterionId}).TotalCount }}
              </div>
            </template>
          </el-table-column>
        </el-table-column>

      </el-table>
    </template>

    <!-- 上传SOW -->
    <base-model :config="sow_model">
      <template slot="dialog-body">
        <upload-acksow
          ref="uploadAckSow"
          :trial-id="listQuery.TrialId"
          :doctor-id="doctorId"
          @getFileList="getFileList"
        />
      </template>
    </base-model>

    <!-- 修改 Reading Type -->
    <base-model :config="readingType_model">
      <template slot="dialog-body">
        <!-- Reading Type -->
        <label>{{ $t('trials:enrolledReviews:table:readingType') }}: </label>
        <el-select
          v-model="readingType"
          filterable
          allow-create
          default-first-option
          style="width: 70%"
        >
          <el-option :value="0" label="TP&GL&AD" />
          <el-option :value="1" label="TP&GL" />
          <el-option :value="2" label="AD" />
        </el-select>
      </template>
      <template slot="dialog-footer">
        <el-button
          :disabled="readingType_model.btnLoading"
          type="primary"
          @click="readingType_model.visible = false"
        >
          {{ $t('common:button:cancel') }}
        </el-button>
        <el-button
          type="primary"
          :loading="readingType_model.btnLoading"
          @click="handleUpdateReadingType"
        >
          {{ $t('common:button:save') }}
        </el-button>
      </template>
    </base-model>
    <!-- 修改 readingCategory -->
    <base-model :config="readingCategory_model">
      <template slot="dialog-body">
        <!-- Reading Type -->
        <label>{{ $t('trials:enrolledReviews:table:readingType') }}: </label>
        <el-select
          v-model="ReadingCategorys"
          filterable
          allow-create
          multiple
          default-first-option
          style="width: 70%"
        >
          <el-option v-for="item of $d.ReadingCategory" v-if="item.value === 1" :key="item.id" :value="1" :label="$fd('ReadingCategory', 1) + ' & ' + $fd('ReadingCategory', 2)" />
          <el-option v-for="item of $d.ReadingCategory" v-if="item.value === 4 && IsArbitrationReading" :key="item.id" :value="item.value" :label="$fd('ReadingCategory', 4)" />
          <el-option v-for="item of $d.ReadingCategory" v-if="item.value === 5 && IsOncologyReading" :key="item.id" :value="item.value" :label="$fd('ReadingCategory', 5)" />
        </el-select>
      </template>
      <template slot="dialog-footer">
        <el-button
          :disabled="readingCategory_model.btnLoading"
          type="primary"
          @click="readingCategory_model.visible = false"
        >
          {{ $t('common:button:cancel') }}
        </el-button>
        <el-button
          type="primary"
          :loading="readingCategory_model.btnLoading"
          @click="handleUpdateReadingCategory"
        >
          {{ $t('common:button:save') }}
        </el-button>
      </template>
    </base-model>
    <!-- 修改医生状态 -->
    <base-model :config="reviewerStatus_model">
      <template slot="dialog-body">
        <el-form
          ref="updateStatusForm"
          :model="updateStatusForm"
          :rules="updateStatusFormrules"
        >
          <el-form-item :label="$t('trials:enrolledReviews:label:enrollmentStatus')" prop="Status">
            <el-select
              v-model="updateStatusForm.Status"
              style="width:60%;"
            >
              <!-- 0：回退；1：出组； -->
              <el-option v-for="item of $d.OutOrInEnrollment" :key="`OutOrInEnrollment${item.value}`" :value="item.value" :label="item.label" :disabled="item.value === 0 && currentWorkload>0" />
            </el-select>
          </el-form-item>
          <el-form-item
            v-if="updateStatusForm.Status== 1"
            :label="$t('trials:enrolledReviews:label:outOfEnrollmentTime')"
            prop="OutEnrollmentTime"
          >
            <el-date-picker
              v-model="updateStatusForm.OutEnrollmentTime"
              type="date"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-form>
      </template>
      <template slot="dialog-footer">
        <el-button
          :disabled="reviewerStatus_model.btnLoading"
          type="primary"
          @click="reviewerStatus_model.visible = false"
        >
          {{ $t('common:button:cancel') }}
        </el-button>
        <el-button
          type="primary"
          :loading="reviewerStatus_model.btnLoading"
          @click="handleUpdateReviewerStatus"
        >
          {{ $t('common:button:save') }}
        </el-button>
      </template>
    </base-model>
    <BaseModel :config="model_cfg">
      <template slot="dialog-body">
        <el-form
          ref="DictionaryTypeConfigForm"
          :model="form"
          label-width="120px"
          size="small"
        >
          <el-form-item label="Criterion Type " prop="CriterionType">
            <el-input v-model="form.CriterionName" disabled />
            <!--            <el-select v-model="form.CriterionType">-->
            <!--              <el-option-->
            <!--                v-for="item of $d.CriterionType"-->
            <!--                :key="item.id"-->
            <!--                :value="item.value"-->
            <!--                :label="item.label"-->
            <!--              />-->
            <!--            </el-select>-->
          </el-form-item>
          <el-form-item label="File" prop="FilePath">
            <el-upload
              class="upload-demo"
              action
              :before-upload="beforeUpload"
              :http-request="handleUploadFile"
              :on-preview="handlePreview2"
              :on-remove="handleRemoveFile2"
              :show-file-list="true"
              :limit="1"
              :file-list="fileList"
            >
              <el-button size="small" type="primary" :disabled="fileList.length > 0">点击上传</el-button>
            </el-upload>
          </el-form-item>
        </el-form>
      </template>
      <template slot="dialog-footer">
        <el-button :disabled="btnLoading" size="small" type="primary" @click="handleCancle">Cancel</el-button>
        <el-button size="small" type="primary" :loading="btnLoading" @click="handleSave">Save</el-button>
      </template>
    </BaseModel>
  </BaseContainer>
</template>
<script>
import {
  getTrialEnrollmentWorkloadStats,
  deleteReviewerAckSOW,
  updateReviewerReadingType,
  enrollBackOrOut,
  setEnrollReadingCategory
} from '@/api/trials'
import { addDoctorCriterionFile } from '@/api/reviewers'
import { uploadFile } from '@/api/attachment'
import { updateTrialReviewerState } from '@/api/trials/reading'
import UploadAcksow from './components/UploadAcksow'
import store from '@/store'
import BaseContainer from '@/components/BaseContainer'
import BaseModel from '@/components/BaseModel'
export default {
  components: { UploadAcksow, BaseContainer, BaseModel },
  data() {
    return {
      list: [],
      TrialReadingCriterionId: null,
      listQuery: {
        TrialId: '',
        PageIndex: 1,
        PageSize: 100,
        Asc: false,
        SortField: ''
      },
      doctorId: '',
      currentRow: {},
      listLoading: false,
      total: 0,
      trialId: '',
      currentClickRowData: '',
      btnLoading: false,
      readingType: '',
      model_cfg: { visible: false, showClose: true, width: '600px', title: '', appendToBody: true },
      sow_model: {
        visible: false,
        title: this.$t('trials:enrolledReviews:dialogTitle:signedAckOfSow'),
        showClose: true,
        width: '500px'
      },
      readingType_model: {
        visible: false,
        title: this.$t('trials:enrolledReviews:dialogTitle:changeReadingType'),
        showClose: true,
        width: '500px',
        btnLoading: false
      },
      readingCategory_model: {
        visible: false,
        title: this.$t('trials:enrolledReviews:dialogTitle:changeReadingType'),
        showClose: true,
        width: '500px',
        btnLoading: false
      },
      reviewerStatus_model: {
        visible: false,
        title: this.$t('trials:enrolledReviews:dialogTitle:changeStatus'),
        showClose: true,
        width: '500px',
        btnLoading: false
      },
      updateStatusForm: {
        Status: '',
        OutEnrollmentTime: ''
      },
      updateStatusFormrules: {
        Status: [{ required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }],
        OutEnrollmentTime: [{ required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }]
      },
      token: store.getters.token,
      ReadingCategorys: [],
      apiReadingCategorys: [],
      TrialReadingCriterionList: [],
      IsArbitrationReading: true,
      IsOncologyReading: true,
      fileList: [],
      form: {}
    }
  },
  computed: {
    currentWorkload() {
      if (Object.keys(this.currentRow).length === 0) return 0
      var total = this.currentRow.Adjudication + this.currentRow.AdjudicationIn24H + this.currentRow.AdjudicationIn48H + this.currentRow.Timepoint + this.currentRow.TimepointIn24H + this.currentRow.TimepointIn48H + this.currentRow.TrainingTimes + this.currentRow.RefresherTraining + this.currentRow.Global + this.currentRow.Downtime
      return total
    }
  },
  mounted() {
    this.trialId = this.$route.query.trialCode
    this.listQuery.TrialId = this.$route.query.trialId
    this.initPage()
  },
  methods: {
    beforeUpload() {
      if (this.fileList.length > 0) {
        // 只允许上传1个文件
        this.$alert(this.$t('trials:enrolledReviews:message:uploadSowWaring1'))
        return
      }
    },
    handleSave() {
      this.$refs['DictionaryTypeConfigForm'].validate(valid => {
        if (!valid) return
        addDoctorCriterionFile(this.form).then(res => {
          // 保存成功
          this.$message.success(this.$t('common:message:savedSuccessfully'))
          this.initPage()
          this.handleCancle()
        })
      })
    },
    handlePreview3(row) {
      if (row.FilePath) {
        window.open(row.FilePath, '_blank')
      }
    },
    handlePreview2(row, r2) {
      if (row.fullPath) {
        window.open(row.fullPath, '_blank')
      }
    },
    handleUploadFile(param) {
      this.btnLoading = true
      const formData = new FormData()
      formData.append('file', param.file)
      this.form.FileName = param.file.name
      // this.form.FileType === 0 ? '既往阅片情况声明' : '入项资格确认书'
      uploadFile(param.file, this.form.FileType === 0 ? this.$t('trials:enrolledReviews:message:SOW') : this.$t('trials:enrolledReviews:message:EQC'), this.form.DoctorId).then(res => {
        this.form.FilePath = res.Result.FilePath
        this.btnLoading = false
        this.fileList[0] = { name: param.file.name, path: res.Result.FilePath, fullPath: res.Result.FullFilePath }
      })
    },
    handleCancle() {
      this.model_cfg.visible = false
    },
    handleRemoveFile2() {
      this.form.FileName = null
      this.form.FilePath = null
      this.fileList = []
    },
    addCol(fileType, row, item) {
      this.model_cfg.visible = true
      this.form = {
        Remark: null,
        FileType: fileType,
        FileName: null,
        FilePath: null,
        IsEnable: true,
        TrialId: this.listQuery.TrialId,
        DoctorId: row.DoctorId,
        CriterionType: item.CriterionType,
        CriterionName: item.TrialReadingCriterionName,
        TrialReadingCriterionId: item.TrialReadingCriterionId
      }
      this.handleRemoveFile2()
      this.model_cfg.title = fileType === 0 ? this.$t('trials:enrolledReviews:message:SOW') : this.$t('trials:enrolledReviews:message:EQC')// '既往阅片情况说明' : '入项资格确认书'
    },
    windowOpen(url) {
      console.log(url)
      window.open(url)
    },
    isEnableChange(row, v) {
      console.log(row)
      this.listLoading = true
      updateTrialReviewerState({
        TrialId: this.listQuery.TrialId,
        IsEnable: v,
        EnrollId: row.EnrollId
      }).then(res => {
        // '保存成功'
        this.$message.success(this.$t('common:message:savedSuccessfully'))
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleUpdateReadingCategory() {
      this.readingCategory_model.btnLoading = true
      this.readingCategory_model.showClose = false
      var params = {
        TrialId: this.$route.query.trialId,
        EnrollId: this.currentRow.EnrollId,
        ReadingCategorys: Object.assign([], this.ReadingCategorys),
        TrialReadingCriterionId: this.TrialReadingCriterionId
      }
      if (~this.ReadingCategorys.indexOf(1)) {
        params.ReadingCategorys.push(2)
      }
      setEnrollReadingCategory(params)
        .then(res => {
          this.readingCategory_model.visible = false
          this.readingCategory_model.btnLoading = false
          this.readingCategory_model.showClose = true
          this.initPage()
          this.$message.success(this.$t('common:message:savedSuccessfully'))
        }).catch(_ => {
          this.readingCategory_model.btnLoading = false
          this.readingCategory_model.showClose = true
        })
    },
    openSetEnrollReadingCategory(row, TrialReadingCriterionId) {
      // this.ReadingCategorys = []
      console.log(row)
      this.currentRow = row
      this.TrialReadingCriterionId = TrialReadingCriterionId
      this.readingCategory_model.visible = true
      var o = row.CriterionCategoryList.find(v => {
        return v.TrialReadingCriterionId === TrialReadingCriterionId
      }) ? row.CriterionCategoryList.find(v => {
          return v.TrialReadingCriterionId === TrialReadingCriterionId
        }) : null
      var i = row.TrialReadingCriterionList.find(v => {
        return v.TrialReadingCriterionId === TrialReadingCriterionId
      }) ? row.TrialReadingCriterionList.find(v => {
          return v.TrialReadingCriterionId === TrialReadingCriterionId
        }) : null
      if (i) {
        this.IsArbitrationReading = i.IsArbitrationReading
        this.IsOncologyReading = i.IsOncologyReading
      }
      // index不能等于-1(删最后一个元素)和空(删第一个元素)
      this.ReadingCategorys = Object.assign([], o.ReadingCategorys)
      this.currentRow.TrialReadingCriterionId = TrialReadingCriterionId
      var index = o ? o.ReadingCategorys.indexOf(2) : -1
      if (index >= 0) {
        this.ReadingCategorys.splice(index, 1)
      }
    },
    // 获取入组医生列表数据
    initPage() {
      this.listLoading = true
      getTrialEnrollmentWorkloadStats(this.listQuery).then((res) => {
        this.listLoading = false
        this.list = res.Result.CurrentPageData
        if (this.list.length > 0) {
          this.TrialReadingCriterionList = this.list[0].TrialReadingCriterionList
        }
        this.total = res.Result.TotalCount
        this.$nextTick(() => {
          // myTable是表格的ref属性值
          if (this.$refs.myTable && this.$refs.myTable.doLayout) {
            this.$refs.myTable.doLayout()
          }
        })
      }).catch(() => {
        this.listLoading = false
      })
    },

    // Enroll按钮回调
    handleEnroll() {
      this.$router.push({
        path: `/trials/trials-panel/enrolled-reviewers/enroll?trialId=${this.listQuery.TrialId}&trialCode=${this.trialId}`
      })
    },

    // 打开修改状态模态框
    changeStatus(row) {
      this.reviewerStatus_model.visible = true
      this.currentRow = row
    },

    // 更新医生状态信息
    handleUpdateReviewerStatus() {
      this.$refs.updateStatusForm.validate(valid => {
        if (!valid) return
        this.reviewerStatus_model.btnLoading = true
        this.reviewerStatus_model.showClose = false
        enrollBackOrOut(this.listQuery.TrialId, this.currentRow.DoctorId, this.updateStatusForm.Status)
          .then(res => {
            this.$nextTick(() => {
              this.$refs['updateStatusForm'].resetFields()
            })
            this.reviewerStatus_model.btnLoading = false
            this.reviewerStatus_model.visible = false
            this.reviewerStatus_model.showClose = true
            this.initPage()
            this.$message.success(this.$t('common:message:savedSuccessfully'))
          })
          .catch(() => {
            this.reviewerStatus_model.btnLoading = false
            this.reviewerStatus_model.showClose = true
          })
      })
    },

    // 打开修改reading type模态框
    handleConfigReadingType(row) {
      this.readingType_model.visible = true
      const { ReviewerReadingType } = row
      this.readingType = ReviewerReadingType
      this.currentRow = row
    },

    // 更新 reading type
    handleUpdateReadingType() {
      this.readingType_model.btnLoading = true
      this.readingType_model.showClose = false
      updateReviewerReadingType(this.listQuery.TrialId, this.currentRow.DoctorId, this.readingType)
        .then(res => {
          this.readingType_model.visible = false
          this.readingType_model.btnLoading = false
          this.readingType_model.showClose = true
          this.initPage()
          this.$message.success(this.$t('common:message:savedSuccessfully'))
        }).catch(_ => {
          this.readingType_model.btnLoading = false
          this.readingType_model.showClose = true
        })
    },
    // 排序
    handleSortByColumn(column) {
      if (column.order === 'ascending') {
        this.listQuery.Asc = true
      } else {
        this.listQuery.Asc = false
      }
      column.prop === 'EnrollTimeStr'
        ? (this.listQuery.SortField = 'EnrollTime')
        : (this.listQuery.SortField = column.prop)
      this.listQuery.PageIndex = 1
      this.initPage()
    },

    // 医生详情页跳转
    handleReviewerDetail(row) {
      const { href } = this.$router.resolve({ path: `/trials/trials-panel/enrolled-reviewers/resume?doctorId=${row.DoctorId}` })
      window.open(href, '_blank')
    },

    // 上传SOW
    handleUploadAckSow(row) {
      this.doctorId = row.DoctorId
      this.currentRow = row
      this.sow_model.visible = true
      let fileList = []
      if (row.AgreementId !== '00000000-0000-0000-0000-000000000000') {
        fileList = [
          {
            DoctorId: row.DoctorId,
            name: row.AgreementFileName,
            FileName: row.AgreementFileName,
            Path: row.AgreementPath,
            FullPath: row.AgreementFullPath,
            Type: 'ACK of SOW'
          }
        ]
      }
      this.$nextTick(function() {
        this.$refs.uploadAckSow.initFileList(fileList)
      })
    },

    // 获取上传控件种SOW文件列表
    getFileList(fileList) {
      this.currentRow.AgreementId =
        fileList.length > 0
          ? fileList[0].Id
          : '00000000-0000-0000-0000-000000000000'
      this.currentRow.AgreementFileName =
        fileList.length > 0 ? fileList[0].FileName : ''
      this.currentRow.AgreementFullPath =
        fileList.length > 0 ? fileList[0].FullPath : ''
      this.currentRow.AgreementPath =
        fileList.length > 0 ? fileList[0].Path : ''
    },

    // 预览SOW
    handleViewAckSow(row) {
      row.AgreementFullPath ? window.open(row.AgreementFullPath, '_blank') : ''
    },

    // 删除SOW文件
    handleDeleteAckSOW(row) {
      this.$confirm(this.$t('trials:enrolledReviews:message:deleteSOWWarning'), {
        type: 'warning',
        distinguishCancelAndClose: true
      })
        .then(() => {
          deleteReviewerAckSOW(
            this.listQuery.TrialId,
            row.DoctorId,
            row.AgreementId
          ).then((res) => {
            row.AgreementId = '00000000-0000-0000-0000-000000000000'
            row.AgreementFileName = ''
            row.AgreementFullPath = ''
            row.AgreementPath = ''
            this.$message.success(this.$t('common:message:savedSuccessfully'))
          })
        })
        .catch((action) => {})
    },

    // 跳转工作量统计页
    handleStats(row) {
      this.currentClickRowData = row
      if (row.AgreementId === '00000000-0000-0000-0000-000000000000') {
        // 'Ack-Sow is not uploaded, continue？'
        this.$confirm(this.$t('trials:enrolledReviews:message:statsWarning'), {
          type: 'warning',
          distinguishCancelAndClose: true
        })
          .then(() => {
            this.handleTipCloseOK()
          })
          .catch(_ => {})
      } else {
        this.handleTipCloseOK()
      }
    },

    handleTipCloseOK() {
      zzSessionStorage.setItem(
        'ReviewerName',
        this.currentClickRowData.FirstName +
        ' ' +
        this.currentClickRowData.LastName
      )
      this.$router.push({
        path: `/trials/trials-panel/enrolled-reviewers/stats?doctorId=${this.currentClickRowData.DoctorId}&trialId=${this.listQuery.TrialId}&trialCode=${this.trialId}&doctorName=${this.currentClickRowData.LastName}/${this.currentClickRowData.FirstName}`
      })
    },

    // 合计行
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 1) {
          sums[index] = 'Total (Current Page)'
          return
        }
        const values = data.map((item) => Number(item[column.property]))
        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
        }
      })
      return sums
    }
  }
}
</script>
<style lang="scss">
  .enroll-list {
    height: 100%;
    .filter-box {
      display: flex;
      align-items: center;
      padding: 5px 0px;
    }
  }
</style>
