<template>
  <BaseContainer>
    <template slot="main-container">
      <el-table ref="emailList" v-loading="loading" :data="list" stripe @sort-change="handleSortChange">
        <el-table-column type="index" width="40" />
        <el-table-column prop="TrialReadingCriterionId" :label="$t('trials:reviewTrack:table:criterionName')"
          show-overflow-tooltip width="100">
          <template slot-scope="scope">
            {{ $fd("CriterionType", scope.row.CriterionType) }}
          </template>
        </el-table-column>
        <!-- 业务场景 -->
        <el-table-column prop="BusinessScenarioEnum" :label="$t('trials:emailManageCfg:title:businessScenario')"
          show-overflow-tooltip width="100">
          <template slot-scope="scope">
            {{ $fd('Common_File_BusinessScenario', scope.row.BusinessScenarioEnum) }}
          </template>
        </el-table-column>
        <el-table-column prop="ToUserTypeList" :label="$t('trials:emailManageCfg:title:toUserTypeList')"
          show-overflow-tooltip width="100">
          <template slot-scope="scope">
            {{ scope.row.ToUserTypeNameList.length > 0 ? scope.row.ToUserTypeNameList.join('、') : '' }}
          </template>
        </el-table-column>
        <!-- 抄送人 -->
        <el-table-column prop="CopyUserTypeList" :label="$t('trials:emailManageCfg:title:copyUserTypeList')"
          show-overflow-tooltip width="100">
          <template slot-scope="scope">
            {{ scope.row.CopyUserTypeNameList.length > 0 ? scope.row.CopyUserTypeNameList.join('、') : '' }}
          </template>
        </el-table-column>
        <!-- 附件 -->
        <el-table-column prop="FileName" :label="$t('trials:subject:title:report')" show-overflow-tooltip
          min-width="120">
          <template slot-scope="scope">
            <el-button icon="el-icon-view" :disabled="!scope.row.fileInfo" circle
              :title="$t('trials:uploadedDicoms:action:preview')"
              @click="handlePreview(scope.row.fileInfo.RelativePath)" />
            <el-button icon="el-icon-pie-chart" circle :title="$t('trials:subject:title:addReport')"
              @click="handleGenerate(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('common:action:action')" width="80px" fixed="right">
          <template slot-scope="scope">
            <!-- 编辑 -->
            <el-button icon="el-icon-message" :disabled="!scope.row.fileInfo" circle
              :title="$t('trials:subject:title:send')" @click="sendEmail(scope.row)" />
          </template>
        </el-table-column>
      </el-table>
    </template>
  </BaseContainer>
</template>
<script>
import { getTrialEmailNoticeConfigList, manualGenerateEmailFile, manualSendEmail } from '@/api/trials'
import { getTrialCriterionList } from '@/api/trials/reading'
import BaseContainer from '@/components/BaseContainer'
const searchDataDefault = () => {
  return {
    BusinessScenarioEnum: null,
    CriterionTypeEnum: null,
    TrialReadingCriterionId: null
  }
}
export default {
  name: 'Message',
  components: { BaseContainer },
  props: {
    data: {
      type: Object,
      default() { return {} }
    }
  },
  data() {
    return {
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      currentRow: {},
      editDialog: { title: '', visible: false },
      loading: false,
      trialId: '',
      trialCriterionList: []
    }
  },
  watch: {
    list() {
      this.$nextTick(() => {
        this.$refs.emailList.doLayout()
      })
    }
  },
  mounted() {
    this.trialId = this.$route.query.trialId
    this.getList()
    // this.getTrialCriterionList()
  },
  methods: {
    sendEmail(row) {
      var message = this.$t('trials:subject:message:sendReport').replace('xxx', this.data.Code)
      this.$confirm(message).then(() => {
        this.loading = true
        manualSendEmail({
          VisitTaskId: row.fileInfo.VisitTaskId,
          SendFileRelativePath: row.fileInfo.RelativePath
        }).then(res => {
          this.loading = false
          // '发送成功'
          this.$message.success(this.$t('trials:subject:message:sendSuccessfully'))
        }).catch(() => {
          this.loading = false
        })
      })
    },
    handleGenerate(row) {
      this.loading = true
      manualGenerateEmailFile({
        SubjectId: this.data.Id,
        TrialReadingCriterionId: row.TrialReadingCriterionId,
        CriterionType: row.CriterionType,
        BusinessScenarioEnum: row.BusinessScenarioEnum
      }).then(res => {
        this.loading = false
        this.$set(row, 'fileInfo', res.Result)
      }).catch(() => {
        this.loading = false
      })
    },
    getTrialCriterionList() {
      getTrialCriterionList(this.trialId).then(res => {
        this.trialCriterionList = res.Result
        // this.activeTab = this.trialCriterionList[0].TrialReadingCriterionId
      }).catch(() => {
      })
    },

    getList() {
      this.loading = true
      this.searchData.TrialId = this.trialId
      this.searchData.IsDistinguishCriteria = true
      getTrialEmailNoticeConfigList(this.searchData).then(res => {
        this.loading = false
        res.Result.forEach(item => {
          item.ToUserTypeNameList = this.getUserTypeName(item.ToUserTypeList)
          item.CopyUserTypeNameList = this.getUserTypeName(item.CopyUserTypeList)
        })
        let list = []
        res.Result.forEach(item => {
          if (Array.isArray(item.CriterionTypeList) && item.CriterionTypeList.length > 0) {
            item.CriterionTypeList.forEach(CriterionType => {
              let obj = Object.assign({}, item)
              obj.CriterionType = CriterionType
              list.push(obj)
            })
          }
        })
        this.list = list
      }).catch(() => {
        this.loading = false
      })
    },
    getUserTypeName(userTypeList) {
      var userTypes = userTypeList.map(i => this.$fd('UserType', i))
      return userTypes
    },
    getUserList(userList, type) {
      return userList.filter(i => i.EmailUserType === type)
    },
    getUsersByType(userList, type) {
      var users = userList.filter(i => i.EmailUserType === type)
      var usersName = users.map(obj => {
        return obj.RealName
      })
      return usersName.join('、')
    },
    // 新增
    handleAdd() {
      this.editDialog.title = this.$t('common:button:new')// '新增'
      this.currentRow = { CriterionTypeEnum: this.criterionType }
      this.editDialog.visible = true
    },
    // 编辑
    handleEdit(row) {
      this.editDialog.title = this.$t('common:button:edit')// '编辑'
      this.currentRow = { ...row }
      this.editDialog.visible = true
    },
    handlePreview(filePath) {
      if (filePath) {
        window.open(window.location.origin + filePath, '_blank')
      }
    },
    handleSearch() {
      this.searchData.PageIndex = 1
      this.getList()
    },
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    closeDialog() {
      this.editDialog.visible = false
    },
    // 排序
    handleSortChange(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()
    }
  }
}
</script>
