<template>
  <div class="participants_content">
    <div class="title">
      <div>{{ $t('trials:staffResearch:title:newStaff') }}</div>
      <div v-if="state === 0 && userTypeEnumInt === 0 && !isHistory">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addParticipant"
        >
          {{ $t('common:button:add') }}
        </el-button>
      </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
          type="text"
          :disabled="state!==0 || item.IsGenerateSuccess"
          @click="editParticipant(item)"
        >
          <i class="el-icon-edit-outline" style="font-size: 20px;" />
        </el-button>
        <!-- 移除 -->
        <el-button
          type="text"
          :disabled="state!==0 || item.IsGenerateSuccess"
          @click="removeParticipant(item)"
        >
          <i class="el-icon-delete" style="font-size: 20px;" />
        </el-button>
      </div>
    </div>

    <!-- 添加/编辑人员信息 -->
    <el-drawer
      :title="title"
      :visible.sync="formVisible"
      direction="btt"
      size="70%"
    >
      <ParticipantForm
        v-if="formVisible"
        :state="state"
        :participant-info="participantInfo"
        @getList="getList"
        @close="close"
      />
    </el-drawer>
  </div>
</template>
<script>
import { getTrialSiteUserSurveyList, deleteTrialSiteUserSurvey } from '@/api/research'
import ParticipantForm from './ParticipantForm'
import { getQueryString } from '@/utils/history.js'
export default {
  name: 'ParticipantsResearch',
  components: { ParticipantForm },
  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: false
        }
        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)
      }
    },
    // 新增
    addParticipant() {
      this.participantInfo = {}
      this.title = this.$t('trials:staffResearch:dialogTitle:add')
      this.formVisible = true
    },
    // 编辑
    editParticipant(obj) {
      this.participantInfo = Object.assign({ ...obj })
      this.title = this.$t('trials:staffResearch:dialogTitle:edit')
      this.formVisible = true
    },
    // 删除
    async removeParticipant(obj) {
      try {
        const confirm = await this.$confirm(
          this.$t('trials:staffResearch:message:confirmDel'),
          {
            type: 'warning',
            distinguishCancelAndClose: true
          }
        )
        if (confirm !== 'confirm') return
        this.loading = true
        const res = await deleteTrialSiteUserSurvey(obj.Id, this.trialId)
        this.loading = false
        if (res.IsSuccess) {
          this.list.splice(this.list.findIndex((item) => item.Id === obj.Id), 1)
          this.$message.success(this.$t('trials:staffResearch:message:delSuccessfully'))
        }
      } catch (e) {
        this.loading = false
        console.log(e)
      }
    },
    isExistIncorrect() {
      return this.list.some(item => item.IsCorrect === false)
    },

    // 初始化列表
    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>
