<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="发送通道" prop="smsSendChannel" label-width="100px">
              <el-select v-model="smsSendChannel" placeholder="请选择发送通道">
                <el-option v-for="dict in channel" :key="dict.providerId" :label="dict.providerName" :value="dict.providerId"></el-option>
              </el-select>
            </el-form-item>
            <el-row style="margin-top: 10px; margin-left: 80px">
              <el-form-item>
                <el-button v-hasPermi="['msg:manualChannelConfig:apply']" type="primary" :loading="saveLoading" @click="channelApply">申请</el-button>
                <el-button type="primary" icon="Search" @click="handleQuery" style="margin-left: 50px">历史轨迹查询 </el-button>
              </el-form-item>
            </el-row>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never" v-if="show">
      <el-table v-loading="loading" :data="manualChannelConfigList" @selection-change="handleSelectionChange">
        <el-table-column label="服务商ID" align="center" prop="providerId" />
        <el-table-column label="服务商名称" align="center" prop="providerName" />
        <el-table-column label="服务商使用状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="msg_service_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="申请人" align="center" prop="createBy" />
        <el-table-column label="申请时间" align="center" prop="createTime" />
      </el-table>
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="ManualChannelConfig" lang="ts">
import { listManualChannelConfig, saveSendChannel,sendChannelApply } from '@/api/msg/manualChannelConfig';
import { ManualChannelConfigVO, ManualChannelConfigQuery, ManualChannelConfigForm } from '@/api/msg/manualChannelConfig/types';
import { getServiceConfig } from '@/api/msg/smsmanual/manual';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const { msg_service_status } = toRefs<any>(proxy?.useDict('msg_service_status'));
const manualChannelConfigList = ref<ManualChannelConfigVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const smsSendChannel = ref();
const channel = ref([]);
const show = ref(false);
const saveLoading = ref(false);
const updateChannel = ref();

const queryFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: ManualChannelConfigForm = {
  id: undefined,
  providerId: undefined,
  providerName: undefined,
  status: undefined
};
const data = reactive<PageData<ManualChannelConfigForm, ManualChannelConfigQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    providerId: undefined,
    providerName: undefined,
    status: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '主键ID不能为空', trigger: 'blur' }],
    providerId: [{ required: true, message: '服务商ID不能为空', trigger: 'blur' }],
    providerName: [{ required: true, message: '服务商名称不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '服务商使用状态(0-启用 1-禁用)不能为空', trigger: 'change' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询手工短信发送通道配置列表 */
const getList = async () => {
  show.value = true;
  loading.value = true;
  const res = await listManualChannelConfig(queryParams.value);
  manualChannelConfigList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

/**
 * 申请
 */
const channelApply = async () => {
  if (!smsSendChannel.value) {
    proxy?.$modal.alert('服务商不能为空');
    return;
  }
  try {
    saveLoading.value = true;
    await sendChannelApply(smsSendChannel.value, updateChannel.value);
    proxy?.$modal.msgSuccess('操作成功');
  } catch (error) {
  } finally {
    saveLoading.value = false;
    await getList();
  }
};

/**
 * 保存
 */
const saveChannel = async () => {
  if (!smsSendChannel.value) {
    proxy?.$modal.alert('服务商不能为空');
    return;
  }

  try {
    saveLoading.value = true;

    await proxy?.$modal
      .confirm('是否更新所有在途申请模板的发送通道？')
      .then(() => {
        updateChannel.value = 'Y';
      })
      .catch(() => {
        updateChannel.value = 'N';
      });
    await saveSendChannel(smsSendChannel.value, updateChannel.value);
    proxy?.$modal.msgSuccess('操作成功');
  } catch (error) {
  } finally {
    saveLoading.value = false;
    await getList();
  }
};

const getSmsSendChannel = async () => {
  const res = await getServiceConfig();
  channel.value = res.data;
  //  console.log(channel.value);
};

onMounted(() => {
  getSmsSendChannel();
});
</script>
