<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 :value="true" label="是" />
            <el-option :value="false" label="否" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否加急:">
          <el-select v-model="searchData.IsUrgent" clearable style="width:100px;">
            <el-option :value="true" label="是" />
            <el-option :value="false" label="否" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否启用:">
          <el-select v-model="searchData.IsEnable" clearable style="width:100px;">
            <el-option :value="true" label="是" />
            <el-option :value="false" label="否" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleReset">Reset</el-button>
          <el-button type="primary" @click="handleSearch">Search</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"
        >
          New
        </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" />

      <el-table-column
        prop="Code"
        label="Code"
        sortable="custom"
        show-overflow-tooltip
      />
      <el-table-column
        prop="ScenarioEnum"
        label="业务场景"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- <el-table-column
        prop="Title"
        label="标题"
        show-overflow-tooltip
      />
      <el-table-column
        prop="Body"
        label="正文"
        show-overflow-tooltip
      />
      <el-table-column
        prop="FromEmail"
        label="发件箱"
        show-overflow-tooltip
      />
      <el-table-column
        prop="AuthorizationCode"
        label="发件箱授权码"
        show-overflow-tooltip
      />
      <el-table-column
        prop="ReceiveEmail"
        label="收件人"
        show-overflow-tooltip
      />
      <el-table-column
        prop="CopyEmail"
        label="抄送邮箱"
        show-overflow-tooltip
      /> -->
      <el-table-column
        prop="IsReturnRequired"
        label="是否需要回执"
        show-overflow-tooltip
        sortable="custom"
        width="130"
      >
        <template slot-scope="scope">
          {{ scope.row.IsReturnRequired?'是':'否' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="IsUrgent"
        label="是否加急"
        show-overflow-tooltip
        sortable="custom"
      >
        <template slot-scope="scope">
          {{ scope.row.IsUrgent?'是':'否' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="IsAutoSend"
        label="是否自动发送"
        show-overflow-tooltip
        sortable="custom"
        width="130"
      >
        <template slot-scope="scope">
          {{ scope.row.IsAutoSend?'是':'否' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="IsEnable"
        label="是否启用"
        show-overflow-tooltip
        sortable="custom"
      >
        <template slot-scope="scope">
          {{ scope.row.IsEnable?'是':'否' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="UpdateTime"
        label="更新时间"
        show-overflow-tooltip
        sortable="custom"
      />

      <el-table-column
        prop="CreateTime"
        label="创建时间"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column label="Action" width="140" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleEdit(scope.row)"
          >
            Edit
          </el-button>

          <el-button type="text" @click="handleDelete(scope.row)">Delete</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"
      custom-class="base-dialog-wrapper"
    >
      <EmailForm :data="rowData" @closeDialog="closeDialog" @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 './components/EmailForm'
const searchDataDefault = () => {
  return {
    IsReturnRequired: '',
    IsUrgent: '',
    IsEnable: '',
    PageIndex: 1,
    PageSize: 20
  }
}
export default {
  name: 'EmailList',
  components: { BoxContent, Pagination, EmailForm },
  data() {
    return {
      searchData: searchDataDefault(),
      loading: false,
      list: [],
      total: 0,
      rowData: {},
      title: '',
      editVisible: false
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 获取受试者列表
    getList() {
      this.loading = true
      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 = 'Add'
      this.editVisible = true
    },
    // 编辑
    handleEdit(row) {
      this.rowData = { ...row }
      this.title = 'Edit'
      this.editVisible = true
    },
    // 删除
    handleDelete(row) {
      this.$confirm('Sure to delete?', {
        type: 'warning',
        distinguishCancelAndClose: true,
        confirmButtonText: 'Ok',
        cancelButtonText: 'Cancel'
      })
        .then(() => {
          deleteEmailNoticeConfig(row.Id)
            .then(res => {
              if (res.IsSuccess) {
                this.list.splice(this.list.findIndex(item => item.Id === row.Id), 1)
                this.$message.success('Deleted successfully!')
              }
            })
        })
    },
    // 查询
    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>
