<template>
  <div class="participants_content">
    <div class="title">
      <div>{{ $t('trials:staffResearch:title:historicalStaff') }}</div>
    </div>
    <div v-for="item in list" :key="item.Id" class="participant_info">
      <div class="p_icon">
        <i class="el-icon-user" />
      </div>
      <div class="p_info">
        <div
          class="p_info_basic"
          :style="{maxWidth:w+'px','white-space': 'nowrap',overflow: 'hidden','text-overflow': 'ellipsis'}"
        >
          <div class="p_text">{{ `${item.LastName} / ${item.FirstName}` }}</div>
          <div class="p_text" style="margin:0 10px">{{ $fd('SiteSurvey_UserRoles',item.TrialRoleCode) }}</div> 
        </div>
        <div class="p_text">{{ item.Phone }}</div>
        <div class="p_text">{{ item.Email }}</div>
        <div class="p_text">{{ item.OrganizationName }}</div>
      </div>
      <div v-if="state === 0 && (userTypeEnumInt===0 ) && !isHistory" class="p_func">
        <!-- 编辑 -->
        <el-button
          v-if="userTypeEnumInt===0"
          type="text"
          @click="editParticipant(item)"
        >
          <i class="el-icon-edit-outline" style="font-size: 20px;" />
        </el-button>
      </div>
    </div>

    <!-- 编辑人员信息 -->
    <el-drawer
      :title="title"
      :visible.sync="formVisible"
      direction="btt"
      size="70%"
    >
      <HistoricalParticipantForm
        v-if="formVisible"
        :state="state"
        :participant-info="participantInfo"
        @getList="getList"
        @close="close"
      />
    </el-drawer>
  </div>
</template>
<script>
import { getTrialSiteUserSurveyList } from '@/api/research'
import HistoricalParticipantForm from './HistoricalParticipantForm'
import { getQueryString } from '@/utils/history.js'
export default {
  name: 'HisParticipantsResearch',
  components: { HistoricalParticipantForm },
  props: {
    isHistory: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: false,
      list: [],
      formVisible: false,
      title: '',
      participantInfo: null,
      w: 0,
      state: null,
      trialSiteSurveyId: '',
      trialId: '',
      userTypeEnumInt: 0,
      warningList: []
    }
  },
  mounted() {
    this.w = document.body.clientWidth - 200
    if (zzSessionStorage.getItem('userTypeEnumInt')) {
      this.userTypeEnumInt = zzSessionStorage.getItem('userTypeEnumInt') * 1
    }
    this.trialSiteSurveyId = getQueryString('trialSiteSurveyId')
    this.trialId = getQueryString('trialId')
  },
  methods: {
    // 获取参与者列表数据
    async getList() {
      try {
        this.loading = true
        var params = {
          trialSiteSurveyId: this.trialSiteSurveyId,
          isHistoryUser: true
        }
        const res = await getTrialSiteUserSurveyList(params)
        this.loading = false
        if (res.IsSuccess) {
          res.Result.map(item => {
            var obj = this.warningList.find(i => i.id === item.Id)
            if (obj) {
              item.ErrorMessage = obj.errorMessage
            }
          })
          this.list = res.Result
        }
      } catch (e) {
        this.loading = false
        console.log(e)
      }
    },
    // 编辑
    editParticipant(row) {
      this.participantInfo = Object.assign({ ...row })
      this.title = this.$t('trials:staffResearch:dialogTitle:edit')
      this.formVisible = true
    },
    // 初始化列表
    initList(trialSiteUserSurveyList, trialSiteSurvey) {
      this.list = trialSiteUserSurveyList
      this.state = trialSiteSurvey.State
      this.$forceUpdate()
    },
    // 关闭窗口
    async close(obj) {
      this.formVisible = false
      var idx = this.warningList.findIndex(i => i.id === obj.id)
      if (idx > -1) {
        this.warningList[idx].errorMessage = obj.errorMessage
      } else {
        this.warningList.push({ id: obj.id, errorMessage: obj.errorMessage })
      }
      this.getList()
    }
  }
}
</script>
<style lang="scss" scoped>
.participants_content{
  background: #fff;
  padding: 10px;
  .title{
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
  }
  .participant_info{
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
    border-top: 1px solid #f5f7fa;
    .p_icon{
      width: 70px;
      font-size: 25px;
      font-weight: bold;
      text-align: center;
    }
    .p_info{
      position: relative;
      flex:1;
      display: flex;
      flex-direction: column;
      justify-content: space-evenly;
      // border-right: 1px solid #f5f7fa;
      font-size: 13px;
    }
    .p_info:after {
        content: '';
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        right: 0;
        height: 50px;
        width: 1px;
        background-color: #f5f7fa;
    }
    .p_info_basic{
      display: flex;
      flex-direction: row;
      align-items: center;

    }
    .p_text{
      line-height: 25px;
      color: #82848a;
    }
    .p_func{
      width: 80px;
      padding: 0 10px;
      text-align: center;
    }
  }
}

</style>
