<template>
  <BaseContainer style="height:100%;">
    <!-- 搜索框 -->
    <template slot="search-container">
      <el-form :inline="true">
        <!-- 中心编号 -->
        <el-form-item :label="$t('trials:crcQuestion:table:siteId')">
          <el-select v-model="searchData.SiteId" clearable filterable style="width:120px;">
            <el-option
              v-for="(item,index) of siteOptions"
              :key="index"
              :label="item.TrialSiteCode"
              :value="item.SiteId"
            />
          </el-select>
        </el-form-item>
        <!-- 受试者编号 -->
        <el-form-item :label="$t('trials:crcQuestion:table:subjectId')">
          <el-input v-model="searchData.SubjectCode" style="width:100px;" />
        </el-form-item>
        <!-- 访视名称 -->
        <el-form-item class="my_multiple" :label="$t('trials:crcQuestion:table:visitName')">
          <el-select
            v-model="searchData.VisitPlanArray"
            style="width:140px"
            clearable
            multiple
          >
            <el-option
              v-for="(item, index) of visitPlanOptions"
              :key="index"
              :label="item.VisitName"
              :value="item.VisitNum"
            >
              <span style="float: left">{{ item.VisitName }}</span>
            </el-option>
            <el-option
              key="Other"
              label="Out of Plan"
              value="1.11"
            />
          </el-select>
        </el-form-item>
        <!-- 是否超限 -->
        <el-form-item :label="$t('trials:crcQuestion:table:isOverTime')">
          <el-select v-model="searchData.IsOverTime" clearable style="width:120px">
            <el-option v-for="item of $d.YesOrNo" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('trials:crcQuestion:table:isClose')">
          <el-select v-model="searchData.IsClosed" clearable style="width:120px">
            <el-option v-for="item of $d.YesOrNo" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('trials:crcQuestion:table:reuploadSatus')">
          <el-select v-model="searchData.ReuploadEnum" clearable style="width:120px">
            <el-option v-for="item of $d.ReuploadEnum" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">
            {{ $t('common:button:search') }}
          </el-button>
          <el-button type="primary" icon="el-icon-refresh-left" @click="handleReset">
            {{ $t('common:button:reset') }}
          </el-button>
          <!--导出-->
          <el-button type="primary" icon="el-icon-download" @click="handleExport">
            {{ $t('common:button:export') }}
          </el-button>
        </el-form-item>
      </el-form>
    </template>

    <!-- crc质疑列表 -->
    <template slot="main-container">
      <el-table
        v-loading="loading"
        v-adaptive="{bottomOffset:60}"
        :data="list"
        stripe
        height="100"
        @sort-change="handleSortByColumn"
      >
        <!-- 质疑编号 -->
        <el-table-column
          ref="crcQsTable"
          prop="ChallengeCode"
          :label="$t('trials:crcQuestion:table:challengeCode')"
          show-overflow-tooltip
          width="100"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              :style="{color:scope.row.IsClosed?'':'red'}"
              @click="handleReplay(scope.row)"
            >
              {{ scope.row.ChallengeCode }}
            </el-button>
          </template>
        </el-table-column>
        <!-- 中心编号 -->
        <el-table-column
          prop="TrialSiteCode"
          :label="$t('trials:crcQuestion:table:siteId')"
          show-overflow-tooltip
          min-width="100"
          sortable="custom"
        />
        <!-- 受试者编号 -->
        <el-table-column
          prop="SubjectCode"
          :label="$t('trials:crcQuestion:table:subjectId')"
          show-overflow-tooltip
          min-width="120"
          sortable="custom"
        />
        <!-- 访视名称 -->
        <el-table-column
          prop="VisitName"
          :label="$t('trials:crcQuestion:table:visitName')"
          show-overflow-tooltip
          min-width="120"
          sortable="custom"
        />
        <el-table-column
          prop="QCProcessEnum"
          :label="$t('trials:crcQuestion:table:qCProcessEnum')"
          show-overflow-tooltip
          min-width="100"
          sortable="custom"
        >
          <template slot-scope="scope">

            <div v-if="otherInfo.QCProcessEnum === 1">
              <!-- {{ scope.row.CurrentQCEnum === 1?'审核':'--' }} -->
              <el-tag v-if="scope.row.CurrentQCEnum === 1" type="primary">{{ $fd('CurrentQCType', 0) }}</el-tag>
              <span v-else>--</span>
            </div>
            <div v-else-if="otherInfo.QCProcessEnum === 2">
              <!-- {{ scope.row.CurrentQCEnum === 1?'初审':scope.row.CurrentQCEnum === 2?'复审':'--' }} -->
              <el-tag v-if="scope.row.CurrentQCEnum === 1" type="primary">{{ $fd('CurrentQCType', scope.row.CurrentQCEnum) }}</el-tag>
              <el-tag v-else-if="scope.row.CurrentQCEnum === 2" type="warning">{{ $fd('CurrentQCType', scope.row.CurrentQCEnum) }}</el-tag>
              <span v-else>--</span>
            </div>
          </template>
        </el-table-column>
        <!-- 重传状态 -->
        <el-table-column
          prop="ReuploadEnum"
          :label="$t('trials:crcQuestion:table:reuploadSatus')"
          show-overflow-tooltip
          width="180"
          sortable="custom"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.ReuploadEnum === 0">--</span>
            <el-tag v-else-if="scope.row.ReuploadEnum === 1" type="danger">{{ $fd('ReuploadEnum', scope.row.ReuploadEnum) }}</el-tag>
            <el-tag v-else-if="scope.row.ReuploadEnum === 2" type="warning">{{ $fd('ReuploadEnum', scope.row.ReuploadEnum) }}</el-tag>
            <el-tag v-else-if="scope.row.ReuploadEnum === 3" type="primary">{{ $fd('ReuploadEnum', scope.row.ReuploadEnum) }}</el-tag>
          </template>
        </el-table-column>
        <!-- 重传完成时间 -->
        <el-table-column
          prop="ReUploadedTime"
          :label="$t('trials:crcQuestion:table:reuploadTime')"
          show-overflow-tooltip
          min-width="150"
          sortable="custom"
        />
        <!-- 创建时间 -->
        <el-table-column
          prop="CreateTime"
          :label="$t('trials:crcQuestion:table:createTime')"
          show-overflow-tooltip
          min-width="140"
          sortable="custom"
        />
        <!-- 处理截止时限 -->
        <el-table-column
          prop="DeadlineTime"
          :label="$t('trials:crcQuestion:table:deadline')"
          show-overflow-tooltip
          width="180"
          sortable="custom"
        />
        <!-- 是否超限 -->
        <el-table-column
          prop="IsOverTime"
          :label="$t('trials:crcQuestion:table:isOverTime')"
          show-overflow-tooltip
          width="140"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsOverTime" type="danger">{{ $fd('YesOrNo', scope.row.IsOverTime) }}</el-tag>
            <el-tag v-else>{{ $fd('YesOrNo', scope.row.IsOverTime) }}</el-tag>
          </template>
        </el-table-column>
        <!-- 是否关闭 -->
        <el-table-column
          prop="IsClosed"
          :label="$t('trials:crcQuestion:table:isClose')"
          show-overflow-tooltip
          min-width="120"
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsClosed">{{ $fd('YesOrNo', scope.row.IsClosed) }}</el-tag>
            <el-tag v-else type="danger">{{ $fd('YesOrNo', scope.row.IsClosed) }}</el-tag>
          </template>
        </el-table-column>
        <!-- 最新回复时间 -->
        <el-table-column
          prop="LatestMsgTime"
          :label="$t('trials:crcQuestion:table:latestMsgTime')"
          show-overflow-tooltip
          min-width="130"
          sortable="custom"
        />
        <!-- 关闭时间 -->
        <el-table-column
          prop="ClosedTime"
          :label="$t('trials:crcQuestion:table:closedTime')"
          show-overflow-tooltip
          min-width="120"
          sortable="custom"
        />

        <el-table-column
          v-if="hasPermi(['trials:trials-panel:visit:crc-question:send'])"
          :label="$t('common:action:action')"
          width="100"
          fixed="right"
        >
          <template slot-scope="scope">
            <!-- 回复 -->
            <el-button
              v-hasPermi="['trials:trials-panel:visit:crc-question:send']"
              icon="el-icon-chat-dot-square"
              :disabled="scope.row.IsClosed"
              circle
              :title="$t('trials:crcQuestion:action:reply')"
              @click="handleReplay(scope.row)"
            />
          </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="chatVisible"
      :visible.sync="chatVisible"
      :close-on-click-modal="false"
      width="800px"
      :title="$t('trials:crcQuestion:dialogTitle:message1') + `(${currentQCRow.SubjectCode} ${currentQCRow.VisitName})`"
    >
      <chat-form ref="chatForm" :data="currentQCRow" user-type="crc" :body-parts="otherInfo.BodyPartTypes" :modalities="otherInfo.Modalitys" :subject-visit-id="currentQCRow.Id" :clinical-enum="otherInfo.ClinicalInformationTransmissionEnum" @close="close" @getDialogList="getDialogList" />
    </el-dialog>
  </BaseContainer>
</template>
<script>
import { getCRCChallengeList, getTrialSiteSelect, getTrialVisitStageSelect, getQCChallengeDialogList } from '@/api/trials'
import { getQCChallengeList_Export } from '@/api/export'
import ChatForm from './components/chatForm'
import BaseContainer from '@/components/BaseContainer'
import Pagination from '@/components/Pagination'
const searchDataDefault = () => {
  return {
    IsOverTime: null,
    SubjectCode: '',
    SiteId: '',
    VisitPlanArray: [],
    IsClosed: null,
    PageIndex: 1,
    PageSize: 20
  }
}
export default {
  name: 'CrcQusetion',
  components: { BaseContainer, Pagination, ChatForm },
  data() {
    return {
      searchData: searchDataDefault(),
      total: 0,
      list: [],
      loading: false,
      chatVisible: false,
      currentQCRow: {},
      siteOptions: [],
      visitPlanOptions: [],
      otherInfo: {},
      trialId: this.$route.query.trialId
    }
  },
  mounted() {
    this.getList()
    this.getSite()
    this.getVisitPlanOptions()
  },
  methods: {
    handleExport() {
      getQCChallengeList_Export(this.searchData).then(res => {
      }).catch(() => { this.loading = false })
    },
    // 获取质疑列表
    getList() {
      this.loading = true
      this.searchData.TrialId = this.trialId
      getCRCChallengeList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
        this.otherInfo = res.OtherInfo
      }).catch(() => { this.loading = false })
    },
    // 回复质疑
    handleReplay(row) {
      this.loading = true
      getQCChallengeDialogList(row.Id).then(res => {
        this.loading = false
        if (res.IsSuccess) {
          if (res.Result.length > 0) { Object.assign(row, res.Result[0]) }
          this.currentQCRow = { ...row }
          this.chatVisible = true
        }
      }).catch(() => { this.loading = false })
    },
    close() {
      this.chatVisible = false
    },
    getDialogList() {
      this.loading = true
      getQCChallengeDialogList(this.currentQCRow.Id).then(res => {
        this.loading = false
        if (res.IsSuccess && res.Result.length > 0) {
          var i = this.list.findIndex(item => item.Id === this.currentQCRow.Id)
          if (i > -1) {
            this.currentQCRow = Object.assign(this.list[i], res.Result[0])
            this.$refs['chatForm'].addMessage(res.Result[0].DialogList[res.Result[0].DialogList.length - 1])
          }
        }
      }).catch(() => { this.loading = false })
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    // 查询
    handleSearch() {
      this.searchData.PageIndex = 1
      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()
    },
    // 获取site下拉框数据
    getSite() {
      getTrialSiteSelect(this.trialId).then(res => {
        this.siteOptions = res.Result
      })
    },
    // 获取访视下拉框数据
    getVisitPlanOptions() {
      getTrialVisitStageSelect(this.trialId)
        .then((res) => {
          this.visitPlanOptions = res.Result
        })
    }
  }
}
</script>
