<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="vendor">
              <el-input v-model="queryParams.title" placeholder="请输入主题" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="模版内容" prop="templateContent">
              <el-input v-model="queryParams.templateContent" placeholder="请输入模版内容" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item v-if="false" label="所属模块" prop="module">
              <el-select v-model="queryParams.module" placeholder="请选择所属模块" clearable>
                <el-option v-for="dict in print_module" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="归属机构" prop="institution">
              <el-select v-model="queryParams.institution" placeholder="请输入归属机构" clearable @click="handleSelectClick">
                <template #header>
                  <el-input v-model="likeName" placeholder="可根据机构编码或名称模糊查询" @input="filterByManageCom"></el-input>
                </template>
                <el-option v-for="dict in filteredManagecom" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item v-if="false" label="服务商" prop="serviceId">
              <el-select v-model="queryParams.serviceId" placeholder="请选择服务商" clearable>
                <el-option v-for="dict in serviceList" :key="dict.providerId" :label="dict.providerName" :value="dict.providerId" />
              </el-select>
            </el-form-item>
            <el-form-item style="width: 100%">
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
              <el-button v-hasPermi="['msg:manualinfo:add']" type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
            </el-form-item>
          </el-form>
        </el-card>
        <el-card shadow="never">
          <template #header>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button v-if="false" v-hasPermi="['msg:manualinfo:add']" type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
              </el-col>
              <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
            </el-row>
          </template>
          <el-table v-loading="loading" :data="manualInfoList">
            <el-table-column label="主题" align="center" prop="title" />
            <el-table-column label="模版内容" align="center" width="200px" prop="templateContent" show-overflow-tooltip />
            <el-table-column label="归属机构" align="center" prop="institution">
              <template #default="scope">
                <dict-tag :options="sys_managecom" :value="scope.row.institution" />
              </template>
            </el-table-column>
            <el-table-column label="总数量" align="center" prop="totalNumber" />
            <el-table-column label="有效数量" align="center" prop="effectiveNumber" />
            <el-table-column label="服务商" align="center" prop="serviceId">
              <template #default="scope">
                <span>{{ getServiceName(scope.row.serviceId) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="申请时间" align="center" prop="applyTime" />
            <el-table-column label="发送类型" align="center" prop="sendType">
              <template #default="scope">
                <dict-tag :options="manual_send_type" :value="scope.row.sendType" />
              </template>
            </el-table-column>
            <el-table-column label="发送时间" align="center" prop="sendTime" />
            <el-table-column label="任务状态" align="center" prop="status">
              <template #default="scope">
                <dict-tag :options="manual_apply_status" :value="scope.row.status" />
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template #default="scope">
                <el-tooltip content="编辑" placement="top">
                  <el-button v-hasPermi="['msg:manualinfo:edit']" link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
                </el-tooltip>
                <el-tooltip content="删除" placement="top">
                  <el-button v-hasPermi="['msg:manualinfo:remove']" link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
                </el-tooltip>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total > 0"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            :total="total"
            @pagination="getList"
          />
        </el-card>
      </div>
    </transition>
  </div>
</template>
<script setup name="ManualInfo" lang="ts">
import { getServiceConfig, listManualInfo, revokeSmsManualApply } from '@/api/msg/smsmanual/manual';
import { parseTime } from '@/utils/ruoyi';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

//归属模块
const { print_module } = toRefs<any>(proxy?.useDict('print_module'));
//发送类型
const { manual_send_type, sys_managecom, manual_apply_status } = toRefs<any>(
  proxy?.useDict('manual_send_type', 'sys_managecom', 'manual_apply_status')
);

const serviceList = ref([]);
const showSearch = ref(true);
const manualInfoList = ref([]);
const likeName = ref('');
const filteredManagecom = ref([]);

const initFormData = {};
const data = reactive({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: undefined,
    templateContent: undefined,
    module: undefined,
    institution: undefined,
    status: '0',
    params: {}
  },
  rules: {}
});
const { queryParams, form, rules } = toRefs(data);
const loading = ref(false);
const total = ref(0);

//获取服务商
const getServiceInfo = async () => {
  const res = await getServiceConfig();
  serviceList.value = res.data;
};

const getServiceName = (serviceId) => {
  return serviceList.value.find((item) => item.providerId === serviceId)?.providerName;
};

/** 查询手工短信申请信息 */
const getList = async () => {
  loading.value = true;
  const res = await listManualInfo(queryParams.value);
  manualInfoList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 新增按钮 */
const handleAdd = () => {
  proxy.$router.push('/msg/smsmanual/manualDetail/');
};

/** 修改按钮 */
const handleUpdate = (row) => {
  proxy.$router.push({
    path: '/msg/smsmanual/manualDetail',
    query: {
      manualId: row.manualId
    }
  });
};

/**
 * 删除
 */
const handleDelete = async (row) => {
  const res = await revokeSmsManualApply(row.manualId);
  if (res.data) {
    proxy?.$modal.msgSuccess('操作成功');
    await getList();
  }
};

const handleSelectClick = () => {
  filterByManageCom(); // 触发过滤逻辑
};

const filterByManageCom = async () => {
  if (likeName.value) {
    filteredManagecom.value = sys_managecom.value.filter((item) => {
      return item.label.includes(likeName.value) || item.value.includes(likeName.value);
    });
  } else {
    filteredManagecom.value = sys_managecom.value;
  }
};

onMounted(() => {
  getList();
  getServiceInfo();
});
</script>
