<template>
  <BaseContainer>

    <template slot="search-container">
      <el-form :inline="true" size="small">
        <!-- 业务场景 -->
        <el-form-item :label="$t('trials:emailManageCfg:title:businessScenario')">
          <el-select
            v-model="searchData.BusinessScenarioEnum"
            style="width:150px;"
          >
            <el-option
              v-for="item of $d.Common_File_BusinessScenario"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />

          </el-select>
        </el-form-item>
        <el-form-item style="margin-bottom:10px" v-if="isDistinguishCriteria" :label="$t('trials:reviewTrack:table:criterionName')">
          <el-select v-model="searchData.TrialReadingCriterionId" clearable style="width:120px;">
            <el-option v-for="item of trialCriterionList" :key="'TrialReadingCriterionId' + item.TrialReadingCriterionId" :value="item.TrialReadingCriterionId" :label="item.TrialReadingCriterionName" />
          </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-form-item>
      </el-form>
      <!-- 自定义标准允许添加 -->
      <el-button
        v-if="criterionType === 0"
        type="primary"
        icon="el-icon-plus"
        style="margin-left:auto;"
        size="small"
        :disabled="isEdit"
        @click="handleAdd"
      >
        {{ $t('common:button:new') }}
      </el-button>
    </template>
    <template slot="main-container">
      <el-table
        ref="emailList"
        v-loading="loading"
        :data="list"
        stripe
        height="720px"
        @sort-change="handleSortChange"
      >
        <el-table-column type="index" width="40" />
        <!-- Code -->
        <el-table-column
          prop="Code"
          :label="$t('trials:emailManageCfg:title:code')"
          sortable="custom"
          show-overflow-tooltip
          width="100"
        />
        <el-table-column
          prop="TrialReadingCriterionId"
          :label="$t('trials:reviewTrack:table:criterionName')"
          show-overflow-tooltip
          sortable="custom"
          width="100"
        >
          <template slot-scope="scope">
            {{ scope.row.TrialCriterionName }}
          </template>
        </el-table-column>
        <!-- 业务场景 -->
        <el-table-column
          prop="BusinessScenarioEnum"
          :label="$t('trials:emailManageCfg:title:businessScenario')"
          show-overflow-tooltip
          sortable="custom"
          width="100"
        >
          <template slot-scope="scope">
            {{ $fd('Common_File_BusinessScenario',scope.row.BusinessScenarioEnum) }}
          </template>
        </el-table-column>
<!--        &lt;!&ndash; 发件人 &ndash;&gt;-->
<!--        <el-table-column-->
<!--          prop="FromName"-->
<!--          :label="$t('trials:emailManageCfg:title:fromName')"-->
<!--          sortable="custom"-->
<!--          show-overflow-tooltip-->
<!--          width="100"-->
<!--        />-->
<!--        &lt;!&ndash; 发件箱 &ndash;&gt;-->
<!--        <el-table-column-->
<!--          prop="FromEmail"-->
<!--          :label="$t('trials:emailManageCfg:title:fromEmail')"-->
<!--          sortable="custom"-->
<!--          show-overflow-tooltip-->
<!--          width="100"-->
<!--        />-->
        <!-- <el-table-column
          prop="FromUsers"
          label="发件人"
          show-overflow-tooltip
        >
          <template slot-scope="scope">

            <el-popover
              placement="bottom"
              trigger="click"
              width="600"
            >
              <el-table :data="scope.row.FromUserList">
                <el-table-column prop="RealName" label="姓名" show-overflow-tooltip />
                <el-table-column prop="UserName" label="用户名" show-overflow-tooltip />
                <el-table-column prop="Email" label="邮箱" show-overflow-tooltip />
              </el-table>
              <el-button slot="reference" type="text"> {{ scope.row.FromUsers }}</el-button>
            </el-popover>

          </template>
        </el-table-column> -->
<!--        &lt;!&ndash; SMTP服务器地址 &ndash;&gt;-->
<!--        <el-table-column-->
<!--          prop="SMTPServerAddress"-->
<!--          :label="$t('trials:emailManageCfg:title:SMTPServerAddress')"-->
<!--          show-overflow-tooltip-->
<!--          width="150"-->
<!--        />-->
<!--        &lt;!&ndash; SMTP服务器端口 &ndash;&gt;-->
<!--        <el-table-column-->
<!--          prop="SMTPServerPort"-->
<!--          :label="$t('trials:emailManageCfg:title:SMTPServerPort')"-->
<!--          show-overflow-tooltip-->
<!--          width="150"-->
<!--        />-->
<!--        &lt;!&ndash; 发件箱授权码 &ndash;&gt;-->
<!--        <el-table-column-->
<!--          prop="AuthorizationCode"-->
<!--          :label="$t('trials:emailManageCfg:title:authorizationCode')"-->
<!--          show-overflow-tooltip-->
<!--          width="150"-->
<!--        />-->
        <!-- 收件人 -->
        <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:emailManageCfg:title:fileName')"
          show-overflow-tooltip
          width="100"
        >
          <template slot-scope="scope">
            <el-button v-if="scope.row.FilePath" type="text" @click="handlePreview(scope.row.FilePath)">
              {{ scope.row.FileName }}
            </el-button>
          </template>
        </el-table-column>
        <!-- 是否需要回执 -->
        <el-table-column
          prop="IsReturnRequired"
          :label="$t('trials:emailManageCfg:title:isReturnRequired')"
          show-overflow-tooltip
          sortable="custom"
          width="140"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsReturnRequired" type="danger">
              {{ $fd('YesOrNo', scope.row.IsReturnRequired) }}
            </el-tag>
            <el-tag v-else type="primary">{{ $fd('YesOrNo', scope.row.IsReturnRequired) }}</el-tag>
          </template>
        </el-table-column>
        <!-- 是否加急 -->
        <el-table-column
          prop="IsUrgent"
          :label="$t('trials:emailManageCfg:title:isUrgent')"
          show-overflow-tooltip
          sortable="custom"
          width="100"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsUrgent" type="danger">
              {{ $fd('YesOrNo', scope.row.IsUrgent) }}
            </el-tag>
            <el-tag v-else type="primary">{{ $fd('YesOrNo', scope.row.IsUrgent) }}</el-tag>
          </template>
        </el-table-column>
        <!-- 是否自动发送 -->
        <el-table-column
          prop="IsAutoSend"
          :label="$t('trials:emailManageCfg:title:isAutoSend')"
          show-overflow-tooltip
          sortable="custom"
          width="140"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsAutoSend" type="danger">
              {{ $fd('YesOrNo', scope.row.IsAutoSend) }}
            </el-tag>
            <el-tag v-else type="primary">{{ $fd('YesOrNo', scope.row.IsAutoSend) }}</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column
          prop="IsDistinguishCriteria"
          label="是否区分标准"
          show-overflow-tooltip
          sortable="custom"
          width="140"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsDistinguishCriteria" type="danger">
              {{ $fd('YesOrNo', scope.row.IsDistinguishCriteria) }}
            </el-tag>
            <el-tag v-else type="primary">{{ $fd('YesOrNo', scope.row.IsDistinguishCriteria) }}</el-tag>
          </template>
        </el-table-column> -->
        <!-- <el-table-column
          prop="IsEnable"
          label="是否启用"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsEnable" type="danger">{{ $fd('YesOrNo', scope.row.IsEnable) }}</el-tag>
            <el-tag v-else type="primary">{{ $fd('YesOrNo', scope.row.IsEnable) }}</el-tag>
          </template>
        </el-table-column> -->
        <!-- 更新时间 -->
        <el-table-column
          prop="UpdateTime"
          :label="$t('trials:emailManageCfg:title:updateTime')"
          show-overflow-tooltip
          sortable="custom"
          width="120"
        />
        <!-- 创建时间 -->
        <el-table-column
          prop="CreateTime"
          :label="$t('trials:emailManageCfg:title:createTime')"
          show-overflow-tooltip
          sortable="custom"
          width="120"
        />

        <el-table-column :label="$t('common:action:action')" fixed="right">
          <template slot-scope="scope">
            <!-- 编辑 -->
            <el-button
              icon="el-icon-edit-outline"
              circle
              :disabled="isEdit"
              :title="$t('common:button:edit')"
              @click="handleEdit(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>

      <!-- 新增/编辑 -->
      <el-dialog
        v-if="editDialog.visible"
        :visible.sync="editDialog.visible"
        :close-on-click-modal="false"
        :title="editDialog.title"
        width="600px"
        custom-class="base-dialog-wrapper"
      >
        <EmailForm
          :data="currentRow"
          :criterion-type="criterionType"
          @closeDialog="closeDialog"
          @getList="getList"
        />
      </el-dialog>
    </template>
  </BaseContainer>
</template>
<script>
import { getTrialEmailNoticeConfigList } from '@/api/trials'
import { getTrialCriterionList } from '@/api/trials/reading'
import BaseContainer from '@/components/BaseContainer'
import EmailForm from './EmailForm'
const searchDataDefault = () => {
  return {
    BusinessScenarioEnum: null,
    CriterionTypeEnum: null,
    TrialReadingCriterionId: null
  }
}
export default {
  name: 'EmailList',
  components: { BaseContainer, EmailForm },
  props: {
    isDistinguishCriteria: {
      type: Boolean,
      default() {
        return false
      }
    },
    isEdit: {
      type: Boolean,
      default() {
        return false
      }
    },
  },
  data() {
    return {
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      currentRow: {},
      editDialog: { title: '', visible: false },
      loading: false,
      trialId: '',
      trialCriterionList: [],
      criterionType: null
    }
  },
  watch: {
    list() {
      this.$nextTick(() => {
        this.$refs.emailList.doLayout()
      })
    }
  },
  mounted() {
    this.trialId = this.$route.query.trialId
    this.getList()
    this.getTrialCriterionList()
  },
  methods: {
    getTrialCriterionList() {
      this.loading = true
      getTrialCriterionList(this.trialId).then(res => {
        this.trialCriterionList = res.Result
        // this.activeTab = this.trialCriterionList[0].TrialReadingCriterionId
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    getList() {
      this.loading = true
      this.searchData.TrialId = this.trialId
      this.searchData.IsDistinguishCriteria = this.isDistinguishCriteria
      getTrialEmailNoticeConfigList(this.searchData).then(res => {
        this.loading = false
        res.Result.forEach(item => {
          // item.FromUsers = this.getUsersByType(item.TrialEmailNoticeUserList, 1)
          // item.FromUserList = this.getUserList(item.TrialEmailNoticeUserList, 1)
          // item.ToUsers = this.getUsersByType(item.TrialEmailNoticeUserList, 2)
          // item.ToUserList = this.getUserList(item.TrialEmailNoticeUserList, 2)
          // item.CopyUsers = this.getUsersByType(item.TrialEmailNoticeUserList, 3)
          // item.CopyUserList = this.getUserList(item.TrialEmailNoticeUserList, 3)
          item.ToUserTypeNameList = this.getUserTypeName(item.ToUserTypeList)
          item.CopyUserTypeNameList = this.getUserTypeName(item.CopyUserTypeList)
        })
        this.list = res.Result
      }).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(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>
