<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">-->
<!--      <el-form-item label="开启短信" prop="flagSms">-->
<!--        <el-select v-model="queryParams.flagSms" placeholder="请选择开启短信" clearable>-->
<!--          <el-option-->
<!--              v-for="dict in sys_yes_no"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="开启邮件" prop="flagMail">-->
<!--        <el-select v-model="queryParams.flagMail" placeholder="请选择开启邮件" clearable>-->
<!--          <el-option-->
<!--              v-for="dict in sys_yes_no"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="低级别告警短信" prop="lowSms">-->
<!--        <el-input-->
<!--            v-model="queryParams.lowSms"-->
<!--            placeholder="请输入低级别告警短信"-->
<!--            clearable-->
<!--            @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="低级别告警邮件" prop="lowMail">-->
<!--        <el-input-->
<!--            v-model="queryParams.lowMail"-->
<!--            placeholder="请输入低级别告警邮件"-->
<!--            clearable-->
<!--            @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="中级别告警短信" prop="midSms">-->
<!--        <el-input-->
<!--            v-model="queryParams.midSms"-->
<!--            placeholder="请输入中级别告警短信"-->
<!--            clearable-->
<!--            @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="中级别告警邮件" prop="midMail">-->
<!--        <el-input-->
<!--            v-model="queryParams.midMail"-->
<!--            placeholder="请输入中级别告警邮件"-->
<!--            clearable-->
<!--            @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="高级别告警短信" prop="highSms">-->
<!--        <el-input-->
<!--            v-model="queryParams.highSms"-->
<!--            placeholder="请输入高级别告警短信"-->
<!--            clearable-->
<!--            @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="高级别告警邮件" prop="highMail">-->
<!--        <el-input-->
<!--            v-model="queryParams.highMail"-->
<!--            placeholder="请输入高级别告警邮件"-->
<!--            clearable-->
<!--            @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="Refresh" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
<!--    </el-form>-->

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="primary"-->
<!--            plain-->
<!--            icon="Plus"-->
<!--            @click="handleAdd"-->
<!--            v-hasPermi="['push:pushConfig:add']"-->
<!--        >新增-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="success"-->
<!--            plain-->
<!--            icon="Edit"-->
<!--            :disabled="single"-->
<!--            @click="handleUpdate"-->
<!--            v-hasPermi="['push:pushConfig:edit']"-->
<!--        >修改-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="danger"-->
<!--            plain-->
<!--            icon="Delete"-->
<!--            :disabled="multiple"-->
<!--            @click="handleDelete"-->
<!--            v-hasPermi="['push:pushConfig:remove']"-->
<!--        >删除-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="warning"-->
<!--            plain-->
<!--            icon="Download"-->
<!--            @click="handleExport"-->
<!--            v-hasPermi="['push:pushConfig:export']"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

<!--    <el-table v-loading="loading" :data="pushConfigList" @selection-change="handleSelectionChange">-->
<!--      <el-table-column type="selection" width="55" align="center"/>-->
<!--      <el-table-column label="推送配置ID" align="center" prop="pcId"/>-->
<!--      <el-table-column label="开启短信" align="center" prop="flagSms">-->
<!--        <template #default="scope">-->
<!--          <dict-tag :options="sys_yes_no" :value="scope.row.flagSms"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="开启邮件" align="center" prop="flagMail">-->
<!--        <template #default="scope">-->
<!--          <dict-tag :options="sys_yes_no" :value="scope.row.flagMail"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="短信配置" align="center" prop="configSms"/>-->
<!--      <el-table-column label="邮件配置" align="center" prop="configMail"/>-->
<!--      <el-table-column label="低级别告警短信" align="center" prop="lowSms"/>-->
<!--      <el-table-column label="低级别告警邮件" align="center" prop="lowMail"/>-->
<!--      <el-table-column label="中级别告警短信" align="center" prop="midSms"/>-->
<!--      <el-table-column label="中级别告警邮件" align="center" prop="midMail"/>-->
<!--      <el-table-column label="高级别告警短信" align="center" prop="highSms"/>-->
<!--      <el-table-column label="高级别告警邮件" align="center" prop="highMail"/>-->
<!--      <el-table-column label="备注" align="center" prop="remark"/>-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template #default="scope">-->
<!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"-->
<!--                     v-hasPermi="['push:pushConfig:edit']">修改-->
<!--          </el-button>-->
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"-->
<!--                     v-hasPermi="['push:pushConfig:remove']">删除-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--    </el-table>-->

<!--    <pagination-->
<!--        v-show="total>0"-->
<!--        :total="total"-->
<!--        v-model:page="queryParams.pageNum"-->
<!--        v-model:limit="queryParams.pageSize"-->
<!--        @pagination="getList"-->
<!--    />-->
<!--    &lt;!&ndash; 添加或修改推送配置对话框 &ndash;&gt;-->
<!--    <el-dialog :title="title" v-model="open" width="500px" append-to-body>-->
      <el-form ref="pushConfigRef" :model="form" :rules="rules" label-width="120px">
        <el-tabs v-model="activeName" style="margin-top:-20px;">
          <el-tab-pane label="短信配置" name="sms">
            <el-form-item label="开启短信" prop="flagSms">
              <el-switch v-model="form.flagSms"/>
              <!--          <el-select v-model="form.flagSms" placeholder="请选择开启短信">-->
              <!--            <el-option-->
              <!--              v-for="dict in sys_yes_no"-->
              <!--              :key="dict.value"-->
              <!--              :label="dict.label"-->
              <!--              :value="dict.value"-->
              <!--            ></el-option>-->
              <!--          </el-select>-->
            </el-form-item>
            <el-form-item label="短信配置" prop="configSms">
              <el-input v-model="form.configSms" type="textarea" placeholder="请输入内容"
                        :autosize="{minRows:4,maxRows:4}"/>
              <el-button type="primary" @click="handleTest('sms')" size="small" style="margin-top:4px">短信测试
              </el-button>
            </el-form-item>
            <el-form-item label="低级别告警短信" prop="lowSms">
              <el-input v-model="form.lowSms" placeholder="请输入低级别告警短信"/>
            </el-form-item>
            <el-form-item label="中级别告警短信" prop="midSms">
              <el-input v-model="form.midSms" placeholder="请输入中级别告警短信"/>
            </el-form-item>
            <el-form-item label="高级别告警短信" prop="highSms">
              <el-input v-model="form.highSms" placeholder="请输入高级别告警短信"/>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="邮箱配置" name="mail">
            <el-form-item label="开启邮件" prop="flagMail">
              <el-switch v-model="form.flagMail"/>
              <!--          <el-select v-model="form.flagMail" placeholder="请选择开启邮件">-->
              <!--            <el-option-->
              <!--              v-for="dict in sys_yes_no"-->
              <!--              :key="dict.value"-->
              <!--              :label="dict.label"-->
              <!--              :value="dict.value"-->
              <!--            ></el-option>-->
              <!--          </el-select>-->
            </el-form-item>
            <el-form-item label="邮件配置" prop="configMail">
              <el-input v-model="form.configMail" type="textarea" placeholder="请输入内容"
                        :autosize="{minRows:4,maxRows:4}"/>
              <el-button type="primary" @click="handleTest('mail')" size="small" style="margin-top:4px">短信测试
              </el-button>
            </el-form-item>
            <el-form-item label="低级别告警邮件" prop="lowMail">
              <el-input v-model="form.lowMail" placeholder="请输入低级别告警邮件"/>
            </el-form-item>
            <el-form-item label="中级别告警邮件" prop="midMail">
              <el-input v-model="form.midMail" placeholder="请输入中级别告警邮件"/>
            </el-form-item>
            <el-form-item label="高级别告警邮件" prop="highMail">
              <el-input v-model="form.highMail" placeholder="请输入高级别告警邮件"/>
            </el-form-item>
          </el-tab-pane>
        </el-tabs>
      </el-form>
<!--      <template #footer>-->
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
<!--      </template>-->
<!--    </el-dialog>-->

    <el-dialog :title="testType === 'sms' ? '短信测试' : '邮件测试'" v-model="testVisible" width="500" append-to-body>
      <el-form ref="tForm" :model="testForm" :rules="rules" label-width="100px">
        <el-row :gutter="10" v-if="testType === 'sms'">
          <el-col :span="24">
            <el-form-item label="手机号码" prop="phone">
              <el-input v-model="testForm.phone" placeholder="请输入手机号码"/>
            </el-form-item>
            <el-form-item label="内容" prop="content">
              <el-input type="textarea" v-model="testForm.content" placeholder="请输入内容"/>
            </el-form-item>
            <el-form-item>
              <el-button type="primary">发送短信</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10" v-if="testType === 'mail'">
          <el-col :span="24">
            <el-form-item label="邮箱" prop="mail">
              <el-input v-model="testForm.mail" placeholder="请输入收件人"/>
            </el-form-item>
            <el-form-item label="邮箱标题" prop="title">
              <el-input v-model="testForm.title" placeholder="请输入标题"/>
            </el-form-item>
            <el-form-item label="内容" prop="content">
              <el-input type="textarea" v-model="testForm.content" placeholder="请输入内容"/>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="sendTest('mail')">发送短信</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

  </div>
</template>

<script setup name="PushConfig">
import {addPushConfig, delPushConfig, getPushConfig, listPushConfig, updatePushConfig,sendTestConfig} from "@/api/push/pushConfig";

const {proxy} = getCurrentInstance();
const {sys_yes_no} = proxy.useDict('sys_yes_no');

const pushConfigList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const activeName = ref("sms")

const testForm = ref({
  phone: "",
  content: "",
  mail: "",
  title: "",
})

const data = reactive({
  form: {
    pcId: 1,
    flagSms: null,
    flagMail: null,
    configSms: null,
    configMail: null,
    lowSms: null,
    lowMail: null,
    midSms: null,
    midMail: null,
    highSms: null,
    highMail: null,
    remark: null
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    flagSms: null,
    flagMail: null,
    configSms: null,
    configMail: null,
    lowSms: null,
    lowMail: null,
    midSms: null,
    midMail: null,
    highSms: null,
    highMail: null,
    remark: null
  },
  rules: {
    phone: [
      {required: true, message: "手机号码不能为空", trigger: "blur"}
    ],
    content: [
      {required: true, message: "内容不能为空", trigger: "blur"}
    ],
    mail: [
      {required: true, message: "邮箱不能为空", trigger: "blur"}
    ],
    title: [
      {required: true, message: "标题不能为空", trigger: "blur"}
    ]
  }
});

const {queryParams, form, rules} = toRefs(data);
const testType = ref(null)
const testVisible = ref(false)

function sendTest(type){
  proxy.$refs["tForm"].validate(async valid => {
    if(valid){
      const {phone, content,mail, title} = testForm.value
      let data = {}
      if(type === 'sms'){
        data = {phone, content}
      }else{
        data = {content,mail, title}
      }
      await sendTestConfig(type,data)
      proxy.$message.success("测试成功")
      testVisible.value = false
    }
  })
}

function handleTest(type) {
  testType.value = type
  testVisible.value = true
  proxy.resetForm("tForm");
}

/** 查询推送配置列表 */
function getList() {
  loading.value = true;
  listPushConfig(queryParams.value).then(response => {
    pushConfigList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    pcId: 1,
    flagSms: null,
    flagMail: null,
    configSms: null,
    configMail: null,
    lowSms: null,
    lowMail: null,
    midSms: null,
    midMail: null,
    highSms: null,
    highMail: null,
    remark: null
  };
  proxy.resetForm("pushConfigRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.pcId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加推送配置";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _pcId = row.pcId || ids.value
  getPushConfig(_pcId).then(response => {
    activeName.value = "sms"
    response.data.flagSms = response.data.flagSms === "Y"
    response.data.flagMail = response.data.flagMail === "Y"
    form.value = response.data;
    open.value = true;
    title.value = "修改推送配置";

  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["pushConfigRef"].validate(valid => {
    if (valid) {
      const data = {...form.value}
      data.flagSms = data.flagSms ? "Y" : "N"
      data.flagMail = data.flagMail ? "Y" : "N"
      if (data.pcId != null) {
        updatePushConfig(data).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          // open.value = false;
          // getList();
        });
      } else {
        addPushConfig(data).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          // open.value = false;
          // getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _pcIds = row.pcId || ids.value;
  proxy.$modal.confirm('是否确认删除推送配置编号为"' + _pcIds + '"的数据项？').then(function () {
    return delPushConfig(_pcIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('push/pushConfig/export', {
    ...queryParams.value
  }, `pushConfig_${new Date().getTime()}.xlsx`)
}

// getList();
</script>
<style lang="scss" scoped>
.dialog-footer{
  display: flex;
  justify-content: center;
  margin-top:20px;
}
</style>
