<template>
  <div>
    <el-col style="width: 1138px">
      <el-card>
        <span class="show-text">姓名:</span>
        <el-input
          id="inputopName"
          v-model="opName"
          class="personal-name"
          clearable
          placeholder="请输入姓名"
          style="width: 280px;margin-right: 26px"/>
        <span class="show-text">邮箱:</span>
        <el-input
          id="inputemail"
          v-model="email"
          class="personal-name"
          clearable
          placeholder="请输入邮箱"
          style="width: 280px;margin-right: 14px"/>
        <el-button :disabled="loading" icon="el-icon-search" plain type="primary" @click="searchTable()">查询
        </el-button>
        <el-button
          icon="el-icon-circle-plus-outline"
          style="margin-left: 14px"
          type="success"
          @click="editType('form')">新增
        </el-button>
        <div style="margin-top: 26px">
          <el-table
            v-loading="loading"
            :data="tableData"
            :header-row-style="{fontSize: '18px',fontFamily:'MiSans Demibold',height: '64px',backgroundColor:'#fff'}"
            :row-style="{fontSize: '16px',fontFamily:'MiSans Regular',height: '64px'}"
            border
            element-loading-background="rgba(0, 0, 0, 0.2)"
            element-loading-spinner="el-icon-loading"
            element-loading-text="数据加载中"
            height="705"
            size="mini"
            stripe
            style="width: 1130px"
            tooltip-effect="dark">
            <el-table-column align="center" label=" " type="index" width="100"/>
            <el-table-column align="center" label="姓名" show-overflow-tooltip width="150">
              <template slot-scope="scope">
                <span>{{ scope.row.opName }}</span>
              </template>
            </el-table-column>
            <el-table-column align="center" label="邮箱" min-width="350" show-overflow-tooltip>
              <template slot-scope="scope">
                <span>{{ scope.row.email }}</span>
              </template>
            </el-table-column>
            <el-table-column align="center" label="编辑" width="250">
              <template slot-scope="scope">
                <el-button
                  icon="el-icon-edit"
                  style="font-size: 16px;font-family: MiSans Regular,serif;color: #4d77d6"
                  type="text"
                  @click="confirmEdit2(scope.row)">编辑
                </el-button>
                <el-button
                  icon="el-icon-delete"
                  style="font-size: 16px;font-family: MiSans Regular,serif;color: #c61a1a"
                  type="text"
                  @click="openDelete(scope.row)">删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="block" style="margin-top: 10px;">
            <el-pagination
              v-if="!loading"
              :current-page="pageList"
              :page-size="pageSize"
              :page-sizes="[10, 20, 30, 40]"
              :total="total"
              background
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"/>
          </div>
        </div>
      </el-card>
    </el-col>
    <el-col style="width: 450px;margin-left: 16px">
      <el-card>
        <div class="send-person">发件人信息设定</div>
        <el-divider style="margin: 0;width: 420px"/>
        <el-form ref="form1" :model="form1" :rules="rules1" label-position="right" label-width="100px">
          <el-form-item label="发件人" prop="opName">
            <el-input
              v-model="form1.opName"
              clearable
              placeholder="请输入发件人"
              style="width: 240px;border-radius: 4px"/>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input
              v-model="form1.email"
              clearable
              placeholder="请输入发件人邮箱"
              style="width: 240px;border-radius: 4px"/>
          </el-form-item>
          <el-form-item label="授权码" prop="emailPassword">
            <el-input
              v-model="form1.emailPassword"
              clearable
              placeholder="请输入授权码"
              style="width: 240px;border-radius: 4px"/>
          </el-form-item>
          <el-form-item label="服务器" prop="service">
            <el-input
              v-model="form1.service"
              clearable
              placeholder="请输入SMTP服务器"
              style="width: 240px;border-radius: 4px"/>
          </el-form-item>
          <el-form-item label="发件主题" prop="topic">
            <el-input
              v-model="form1.topic"
              clearable
              placeholder="请输入发件主题"
              style="width: 240px;border-radius: 4px"/>
          </el-form-item>
          <el-form-item label="启用推送" prop="used">
            <el-radio-group v-model="form1.used">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-row>
            <el-col :span="14"/>
            <el-form-item>
              <el-button style="width: 80px;height: 40px;margin-left: 50px" type="primary" @click="submit1('form1')">确
              定
              </el-button>
            </el-form-item>
          </el-row>
        </el-form>
      </el-card>
    </el-col>
    <el-dialog :show-close="false" :visible.sync="dialogFormVisible" center title="新增收件人信息" width="440px">
      <el-divider style="margin: 0"/>
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="100px">
        <el-form-item label="姓名" prop="opName">
          <el-input v-model="form.opName" clearable placeholder="请输入姓名" style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" clearable placeholder="请输入邮箱" style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-row>
          <el-col :span="14"/>
          <el-form-item>
            <el-button style="width: 80px;height: 40px" type="primary" @click="submit('form')">确 定</el-button>
            <el-button style="width: 80px;height: 40px" @click="callOff('form')">取消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>
    <el-dialog :show-close="false" :visible.sync="dialogFormVisible2" center title="编辑收件人信息" width="440px">
      <el-divider style="margin: 0"/>
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="right" label-width="100px">
        <el-form-item label="姓名" prop="opName">
          <el-input v-model="form2.opName" clearable placeholder="请输入姓名" style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form2.email" clearable placeholder="请输入邮箱" style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-row>
          <el-col :span="14"/>
          <el-form-item>
            <el-button style="width: 80px;height: 40px" type="primary" @click="submitAdd2('form2')">确 定</el-button>
            <el-button style="width: 80px;height: 40px" @click="callOff2('form2')">取消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
// smtp.exmail.qq.com
// smtphz.qiye.163.com
export default {
  name: 'SendEmail',
  data() {
    const checkEmail = (rule, value, callback) => {
      const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
      if (!value) {
        return callback(new Error('邮箱不能为空'))
      }
      setTimeout(() => {
        if (mailReg.test(value)) {
          callback()
        } else {
          callback(new Error('请输入正确的邮箱格式'))
        }
      }, 100)
    }
    return {
      opName: '', // 姓名
      email: '', // 邮箱
      title: '',
      dialogFormVisible: false,
      dialogFormVisible2: false,
      form: {
        opName: '',
        email: ''
      },
      rules: {
        opName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        email: [{ validator: checkEmail, required: true, trigger: 'blur' }]
      },
      form2: {
        opName: '',
        email: '',
        email_yuan: ''
      },
      rules2: {
        opName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        email: [{ validator: checkEmail, required: true, trigger: 'blur' }]
      },
      form1: {
        opName: '',
        email: '',
        emailPassword: '',
        topic: '',
        ID: '',
        used: 0,
        service: ''
      },
      rules1: {
        opName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        email: [{ validator: checkEmail, required: true, trigger: 'blur' }],
        emailPassword: [{ required: true, message: '请输入邮箱密码', trigger: 'blur' }],
        service: [{ required: true, message: '请输入服务器', trigger: 'blur' }]
      },
      loading: false,
      tableData: [],
      total: 0, // 总条数
      pageSize: 10, // 每页显示条数
      pageList: 1 // 后台获取页
    }
  },
  created() {
    this.searchTable()
    this.searchTable1()
  },
  methods: {
    // 查询发件人
    searchTable1() {
      var param = []
      param[0] = ['pushType', 1]
      var Data = this.CreateData('11', '基础数据_推送管理_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          let info = JSON.parse(response.data[0].pushSetting)
          if (!info) info = {}
          this.form1.opName = response.data[0].opName
          this.form1.email = Object.hasOwnProperty.call(info, 'email') ? info['email'] : ''
          this.form1.topic = Object.hasOwnProperty.call(info, 'topic') ? info['topic'] : ''
          this.form1.emailPassword = Object.hasOwnProperty.call(info, 'emailPassword') ? info['emailPassword'] : ''
          this.form1.service = Object.hasOwnProperty.call(info, 'stmpServer') ? info['stmpServer'] : ''
          this.form1.ID = response.data[0].ID
          this.form1.used = response.data[0].used === 1 ? 1 : 0
        } else {
          this.form1.opName = ''
          this.form1.email = ''
          this.form1.emailPassword = ''
          this.form1.ID = ''
          this.form1.service = ''
          this.form1.used = 0
        }
      })
    },
    // 查询收件人
    searchTable() {
      this.loading = true
      this.tableData = []
      var param = []
      param[0] = ['opName', this.opName]
      param[1] = ['email', this.email]
      param[2] = ['PageCurrent', this.pageList]
      param[3] = ['PageSize', this.pageSize]
      param[4] = ['PageCount', '1111', 'int', '1']
      param[5] = ['ItemCount', '1111', 'int', '1']
      var Data = this.CreateData('11', '基础数据_Email_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData = response.data.result
        }
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
      })
    },
    // 编辑
    confirmEdit2(row) {
      if (this.$refs['form2'] !== undefined) {
        this.$refs['form2'].resetFields()
      }
      this.form2.opName = row.opName
      this.form2.email_yuan = row.email
      this.form2.email = row.email
      this.dialogFormVisible2 = true
    },
    // 确定编辑
    submitAdd2(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['opName', this.form2.opName]
          param[1] = ['email_yuan', this.form2.email_yuan]
          param[2] = ['email', this.form2.email]
          var Data = this.CreateData('11', '基础数据_Email_编辑', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === 1) {
              this.$message({
                message: '信息编辑成功',
                type: 'success'
              })
              this.searchTable()
              this.dialogFormVisible2 = false
            } else {
              this.$message.error('邮箱已存在，信息编辑失败')
            }
          })
        }
      })
    },
    // 重置表单
    callOff2(formName) {
      this.dialogFormVisible2 = false
      this.$refs[formName].resetFields()
    },
    // 删除
    openDelete(row) {
      this.$confirm('当前邮箱将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirm-confirm-button',
        cancelButtonClass: 'confirm-cancel-button',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['ID', row.ID]
        var Data = this.CreateData('12', '基础数据_Email_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message({
              message: '邮箱删除成功',
              type: 'success'
            })
            this.searchTable()
          } else {
            this.$message.error('邮箱删除失败')
          }
        })
      })
    },

    // 点击增加
    editType(formName) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
      this.form.opName = ''
      this.form.email = ''
      this.dialogFormVisible = true
    },
    // 增加确定
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['opName', this.form.opName]
          param[1] = ['email', this.form.email]
          var Data = this.CreateData('11', '基础数据_Email_新增', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === 1) {
              this.$message({
                message: '邮箱信息增加成功',
                type: 'success'
              })
              this.pageList = 1
              this.pageSize = 10
              this.searchTable()
              this.dialogFormVisible = false
            } else {
              this.$message.error('邮箱已经存在，信息增加失败！')
            }
          })
        } else {
          return false
        }
      })
    },
    // 重置表单
    callOff(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },

    // 增加确定发件人
    submit1(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          const pushSetting = {
            email: this.form1.email,
            topic: this.form1.topic,
            emailPassword: this.form1.emailPassword,
            stmpServer: this.form1.service
          }
          param[0] = ['ID', this.form1.ID]
          param[1] = ['pushSetting', JSON.stringify(pushSetting)]
          param[2] = ['opName', this.form1.opName]
          param[3] = ['used', this.form1.used]
          var Data = this.CreateData('11', '基础数据_推送管理_编辑', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === 1) {
              this.$message({
                message: '发件人信息编辑成功',
                type: 'success'
              })
              this.searchTable1()
            } else {
              this.$message.error('发件人信息编辑失败！')
            }
          })
        } else {
          return false
        }
      })
    },
    // 分页
    handleSizeChange(val) {
      this.pageList = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageList = val
      this.searchTable()
    }
  }
}
</script>

<style scoped>
.send-person {
  font-size: 22px;
  font-family: "MiSans Demibold", serif;
  text-align: center;
  margin-bottom: 12px;
}
</style>
