<template>
  <div class="equipments_content">
    <div class="title">
      <div>{{ $t('trials:equiptResearch:title:equiptResearch') }}</div>
      <div v-if="state === 0 && userTypeEnumInt === 0 && !isHistory">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">
          {{ $t('common:button:add') }}
        </el-button>
      </div>
    </div>
    <div v-for="item in list" :key="item.name" class="equipment_info">
      <div class="p_icon">
        <i class="el-icon-receiving" />
      </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" v-if="EquipmentControlFieldList.includes('EquipmentTypeEnum')">
            {{ item.OtherEquipmentType ? item.OtherEquipmentType :
              $fd('SiteSurvey_ScanEquipmentType', item.EquipmentTypeEnum) }}</div>
          <div v-if="EquipmentControlFieldList.includes('Parameters')" class="p_text" style="margin-left:10px">{{
            item.Parameters }}</div>
          <div class="p_text" style="margin-left:10px" v-if="EquipmentControlFieldList.includes('ManufacturerType')">{{
            item.ManufacturerName ? item.ManufacturerName : $fd('ManufacturerType',
              item.ManufacturerType) }}</div>
        </div>
        <div class="p_text" v-if="EquipmentControlFieldList.includes('ScannerType')">{{ item.ScannerType }}</div>
        <div class="p_text" v-if="EquipmentControlFieldList.includes('Note')">{{ item.Note }}</div>
      </div>
      <div v-if="state === 0 && userTypeEnumInt === 0 && !isHistory" class="p_func">
        <el-button type="text" @click="handleEdit(item)">
          <i class="el-icon-edit-outline" style="font-size: 20px;" />
        </el-button>
        <el-button type="text" @click="handleDelete(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%">
      <EquipmentForm v-if="formVisible" :equipment-info="equipmentInfo"
        :EquipmentControlFieldList="EquipmentControlFieldList" @getList="getList" @close="close" />
    </el-drawer>
  </div>
</template>
<script>
import { getTrialSiteEquipmentSurveyList, deleteTrialSiteEquipmentSurvey } from '@/api/research'
import EquipmentForm from './EquipmentForm'
import { getQueryString } from '@/utils/history.js'
export default {
  name: 'EquipmentsResearch',
  components: { EquipmentForm },
  props: {
    trialSiteSurveyEquipmentType: {
      type: String,
      default: ''
    },
    isHistory: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      list: [],
      formVisible: false,
      title: '',
      equipmentInfo: {},
      w: 0,
      loading: false,
      userTypeEnumInt: 0,
      state: null,
      trialSiteSurveyId: '',
      trialId: '',
      EquipmentControlFieldList: []
    }
  },
  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
        const res = await getTrialSiteEquipmentSurveyList(this.trialSiteSurveyId)
        this.loading = false
        if (res.IsSuccess) {
          this.list = res.Result
        }
      } catch (e) {
        this.loading = false
        console.log(e)
      }
    },
    // 新增
    handleAdd() {
      this.equipmentInfo = {}
      this.title = this.$t('trials:equiptResearch:dialogTitle:add')
      this.formVisible = true
    },
    // 编辑
    handleEdit(obj) {
      this.equipmentInfo = Object.assign({ ...obj })
      this.title = this.$t('trials:equiptResearch:dialogTitle:edit')
      this.formVisible = true
    },
    // 删除
    async handleDelete(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 deleteTrialSiteEquipmentSurvey(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:equiptResearch:message:delSuccessfully'))
        }
      } catch (e) {
        this.loading = false
        console.log(e)
      }
    },
    initList(TrialSiteEquipmentSurveyList, trialSiteSurvey, EquipmentControlFieldList) {
      this.EquipmentControlFieldList = []
      EquipmentControlFieldList.forEach(item => {
        this.EquipmentControlFieldList.push(item.FiledName)
      })
      this.list = TrialSiteEquipmentSurveyList
      this.state = trialSiteSurvey.State
      this.$forceUpdate()
      console.log(this.list)
    },
    // 关闭窗口
    close() {
      this.formVisible = false
    }
  }
}
</script>
<style lang="scss" scoped>
.equipments_content {
  background: #fff;
  padding: 10px;

  .title {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
  }

  .equipment_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>
