<template>
  <box-content>
    <div class="search">
      <el-form :inline="true" size="mini" class="base-search-form">
        <el-form-item label="是否需要回执:">
          <el-select v-model="searchData.IsReturnRequired" clearable style="width:100px;">
            <el-option v-for="item of $d.YesOrNo" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否加急:">
          <el-select v-model="searchData.IsUrgent" clearable style="width:100px;">
            <el-option v-for="item of $d.YesOrNo" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否启用:">
          <el-select v-model="searchData.IsEnable" clearable style="width:100px;">
            <el-option v-for="item of $d.YesOrNo" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="阅片标准:" v-if="isDistinguishCriteria">
          <el-select v-model="searchData.CriterionTypeEnum" clearable style="width:100px;">
            <el-option v-for="item of $d.CriterionType" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
      <span style="margin-left:auto;">
    <!-- <el-button
      type="primary"
      size="mini"
    >
      场景配置
    </el-button> -->
    <el-button
      type="primary"
      size="mini"
      @click="handleAdd"
    >
      新增
    </el-button>
  </span>

    </div>
    <!-- 受试者列表 -->
    <el-table
      v-loading="loading"
      v-adaptive="{bottomOffset:45}"
      :data="list"
      stripe
      height="100"
      style="width:100%"
      @sort-change="handleSortByColumn"
    >
      <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
        v-if="isDistinguishCriteria"
        prop="TrialReadingCriterionId"
        :label="$t('trials:reviewTrack:table:criterionName')"
        show-overflow-tooltip
        sortable="custom"
        width="120"
      >
        <template slot-scope="scope">
          {{ scope.row.TrialCriterionName }}
        </template>
      </el-table-column>
      <el-table-column
        prop="EmailTopicCN"
        label="邮件主题（中文）"
        sortable="custom"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        prop="EmailTopic"
        label="邮件主题（英文）"
        sortable="custom"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        prop="BusinessLevelEnum"
        label="业务层级"
        sortable="custom"
        show-overflow-tooltip
        width="100">
        <template slot-scope="scope">
          {{ $fd('BusinessLevel',scope.row.BusinessLevelEnum) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="BusinessModuleEnum"
        label="业务模块"
        sortable="custom"
        show-overflow-tooltip
        width="100">
        <template slot-scope="scope">
          {{ $fd('BusinessModule',scope.row.BusinessModuleEnum) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="BusinessScenarioEnum"
        label="业务场景"
        sortable="custom"
        show-overflow-tooltip
        width="130"
      >
        <template slot-scope="scope">
          {{ $fd('Email_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.ToUserTypeList.length > 0?scope.row.ToUserTypeList.map(v => $fd('UserType', v)).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.CopyUserTypeList.length > 0?scope.row.CopyUserTypeList.map(v => $fd('UserType', v)).join('、'):'' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="EmailUrgentEnum"
        label="加急状态"
        sortable="custom"
        show-overflow-tooltip
        width="100"
      >
        <template slot-scope="scope">
          {{ $fd('EmailUrgent',scope.row.EmailUrgentEnum) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="EmailCron"
        label="发送周期和时间"
        sortable="custom"
        show-overflow-tooltip
        width="160"
      />
      <!-- 附件 -->
      <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="IsAutoSend"
        :label="$t('trials:emailManageCfg:title:isAutoSend')"
        show-overflow-tooltip
        sortable="custom"
        width="160"
      >
        <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="IsEnable"
        :label="$t('trials:emailManageCfg:table:IsEnable')"
        show-overflow-tooltip
        sortable="custom"
        width="160"
      >
        <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="160"
      />
      <!-- 创建时间 -->
      <el-table-column
        prop="CreateTime"
        :label="$t('trials:emailManageCfg:title:createTime')"
        show-overflow-tooltip
        sortable="custom"
        width="160"
      />
      <el-table-column label="Action" width="140" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleEdit(scope.row)"
          >
            编辑
          </el-button>

          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>

        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination class="page" :total="total" :page.sync="searchData.PageIndex" :limit.sync="searchData.PageSize" @pagination="getList" />
    <!-- 搜索框 -->
    <!-- 修改受试者状态 -->
    <el-dialog
      v-if="editVisible"
      :visible.sync="editVisible"
      :close-on-click-modal="false"
      :title="title"
      width="700px"
      fullscreen
      custom-class="base-dialog-wrapper"
    >
      <EmailForm :data="rowData" @closeDialog="closeDialog" @getList="getList" />
    </el-dialog>

    <!-- 修改受试者状态 -->
    <el-dialog
      v-if="attachmentVisible"
      :visible.sync="attachmentVisible"
      :close-on-click-modal="false"
      title="附件"
      width="80%"
      custom-class="base-dialog-wrapper"
      append-to-body
    >
      <AttachmentList
        :business-scenario-enum="rowData.BusinessScenarioEnum"
        :is-distinguish-criteria="rowData.IsDistinguishCriteria"
        @getList="getList"
      />
    </el-dialog>
  </box-content>
</template>
<script>
import { getEmailNoticeConfigList, deleteEmailNoticeConfig } from '@/api/dictionary'
import BoxContent from '@/components/BoxContent'
import Pagination from '@/components/Pagination'
import EmailForm from './EmailForm'
import AttachmentList from './AttachmentList'
const searchDataDefault = () => {
  return {
    IsReturnRequired: '',
    IsUrgent: '',
    IsEnable: '',
    CriterionTypeEnum: null,
    PageIndex: 1,
    PageSize: 20
  }
}
export default {
  name: 'EmailList',
  components: { BoxContent, Pagination, EmailForm, AttachmentList },
  props: {
    isDistinguishCriteria: {
      type: Boolean,
      default() {
        return false
      }
    }
  },
  data() {
    return {
      activeTab: '0',
      searchData: searchDataDefault(),
      loading: false,
      list: [],
      total: 0,
      rowData: {},
      title: '',
      editVisible: false,
      attachmentVisible: false
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 获取受试者列表
    getList() {
      this.loading = true
      this.searchData.IsDistinguishCriteria = this.isDistinguishCriteria
      getEmailNoticeConfigList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      }).catch(() => { this.loading = false })
    },
    // 新增
    handleAdd() {
      this.rowData = {}
      this.title = '新增'
      this.editVisible = true
    },
    // 编辑
    handleEdit(row) {
      this.rowData = { ...row }
      this.title = '编辑'
      this.editVisible = true
    },
    // 删除
    handleDelete(row) {
      this.$confirm(this.$t('trials:staffResearch:message:confirmDel'), {
        type: 'warning',
        distinguishCancelAndClose: true
      })
        .then(() => {
          deleteEmailNoticeConfig(row.Id)
            .then(res => {
              if (res.IsSuccess) {
                this.getList()
                this.$message.success('删除成功!')
              }
            })
        })
    },
    handleDetail(row) {
      this.rowData = { ...row }
      this.attachmentVisible = true
    },
    // 查询
    handleSearch() {
      this.searchData.PageIndex = 1
      this.getList()
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    // 排序
    handleSortByColumn(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()
    },
    // 关闭新增、编辑框
    closeDialog() {
      this.editVisible = false
    }
  }
}
</script>
