<template>
  <div class="pl">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="search">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item label="消息标题">
            <el-input v-model="queryParams.title" placeholder="请输入消息标题" />
          </el-form-item>
          <el-form-item label="发送内容">
            <el-input v-model="queryParams.content" placeholder="请输入发送内容" />
          </el-form-item>
          <el-form-item label="发送时间">
            <el-date-picker v-model="queryParams.sendTime" type="date" placeholder="选择发送时间" />
          </el-form-item>
          <el-form-item label="发送对象">
            <el-input v-model="queryParams.recipient" placeholder="请输入发送对象" />
          </el-form-item>
          <el-form-item label="发送类型">
            <el-select v-model="queryParams.type" placeholder="请选择发送类型">
              <!-- 这里需要根据实际的发送类型选项添加 el-option -->
              <el-option label="类型1" value="1" />
              <el-option label="类型2" value="2" />
            </el-select>
          </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-button type="primary" @click="showRefuteDialog = true">发送信息</el-button>
      </div>
    </transition>
    <el-table
      ref="categoryTableRef"
      :data="messList"
      row-key="categoryId"
      border
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="id" prop="id" align="center" width="180" />
      <el-table-column label="发送者ID" prop="senderId" align="center" width="150" />
      <el-table-column label="标题" prop="title" align="center" width="150" />
      <el-table-column label="内容" prop="content" align="center" width="150" />
      <el-table-column label="创建时间" prop="createTime" align="center" width="200" />
      <el-table-column label="定时发送时间" prop="scheduledTime" align="center" width="200" />
      <el-table-column label="消息类型" prop="title" align="center" width="150">
        <template #default="{ row }">
          {{ row.msgType == 1 ? '手动' : '自动' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-tooltip content="撤回" placement="top">
            <el-button link type="primary" v-hasPermi="['workflow:video:remove']">撤回</el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <pagination v-if="total > 0" :total="total" v-model:page="queryParams.current" v-model:limit="queryParams.size" @pagination="getlist" />
    </div>
    <el-dialog v-model="showRefuteDialog" title="消息发送" @close="clearFormData">
      <el-form :model="dataparams" :rules="rules">
        <el-form-item label="模板名称">
          <!-- <el-input v-model="dataparams.title" placeholder="请输入标题" /> -->
          <el-select
            v-model="dataparams.templateId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入模板名称"
            :remote-method="idMethod"
            :loading="idloading"
          >
            <el-option v-for="item in idoptions" :key="item.id" :label="item.templateName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="指定">
          <el-button @click="dataparams.logmess = 1" :style="{ background: dataparams.logmess == 1 ? '#FFB6C1' : '' }">{{ '指定角色' }}</el-button>
          <el-button @click="dataparams.logmess = 2" :style="{ background: dataparams.logmess == 2 ? '#FFB6C1' : '' }">{{ '指定用户' }}</el-button>
          <!-- <el-button @click="dataparams.logmess = 3" :style="{ background: dataparams.logmess == 3 ? '#FFB6C1' : '' }">{{ '指定群' }}</el-button> -->
        </el-form-item>
        <el-form-item label="手机号/名称" v-show="dataparams.logmess == 2">
          <el-select
            v-model="dataparams.sendScope"
            multiple
            filterable
            remote
            reserve-keyword
            placeholder="请输入手机号/名称"
            :remote-method="remoteMethod"
            :loading="loading"
          >
            <el-option v-for="item in options" :key="item.userId" :label="item.nickName" :value="item.userName" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="群名称" v-show="dataparams.logmess == 3">
          <el-select
            v-model="dataparams.sendScope"
            multiple
            filterable
            remote
            reserve-keyword
            placeholder="请输入群名称"
            :remote-method="remoteMethod"
            :loading="loading"
          >
            <el-option v-for="item in options" :key="item.userId" :label="item.nickName" :value="item.userId" />
          </el-select>
        </el-form-item> -->
        <el-form-item label="指定角色" v-show="dataparams.logmess == 1">
          <el-select v-model="dataparams.sendScope" placeholder="请选择角色">
            <el-option v-for="(item, index) in sendScopelist" :label="item.label" :value="item.value" :key="index" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <!-- <el-button @click="showRefuteDialog = false">取消</el-button> -->
          <el-button type="primary" @click="cilcikgAdd">发送</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Leave" lang="ts">
import { listlist, listAdd, listuserlist, listtemplate, templateSelect } from '@/api/set/messageindex';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const showSearch = ref(true);
const queryFormRef = ref<ElFormInstance>();
import { LeaveForm, LeaveQuery, LeaveVO, LeaveListVO, optionListItem, AccountOV } from '@/api/set/messagetype';
const data = reactive<PageData<LeaveForm, LeaveQuery>>({
  form: {},
  queryParams: {
    current: 1,
    size: 10,
    title: null,
    content: null,
    sendTime: null,
    recipient: null,
    type: null
  },
  rules: {}
});
const isExpandAll = ref(true);
const { queryParams } = toRefs(data);
const messList = ref<LeaveVO[]>([]);
const total = ref(0);
const showRefuteDialog = ref<boolean>(false);
const getlist = async () => {
  const res = await listlist(queryParams.value);
  console.log(res);
  if (res.code == 200) {
    messList.value = res.data.records;
    total.value = res.data.total;
  }
};
const dataparams = ref<LeaveListVO>({
  sendScope: null,
  templateId: null,
  logmess: 1 // 1:角色 2:用户  3:群聊
});
const rules = reactive({
  id: [{ required: false, message: '请输入', trigger: 'blur' }],
  sendScope: [{ required: true, message: '请输入内容', trigger: 'blur' }]
});
const select = ref([
  { label: '自动', value: 0 },
  { label: '手动', value: 1 }
]);
const handleQuery = () => {};
const sendScopelist = ref([
  { label: '全部', value: 'all' },
  { label: '达人', value: 'expert' },
  { label: '商户', value: 'merchant' },
  { label: '用户', value: 'user' }
]);
//发送消息
const cilcikgAdd = async () => {
  const res = await listAdd(dataparams.value);
  if (res.code == 200) {
    dataparams.value = {
      sendScope: null,
      templateId: null,
      logmess: 1 // 1:角色 2:用户  3:群聊
    };
    showRefuteDialog.value = false;
    getlist();
  }
};
const clearFormData = () => {
  dataparams.value = {
    sendScope: null,
    templateId: null,
    logmess: 1 // 1:角色 2:用户  3:群聊
  };
};
const options = ref<optionListItem[]>([]);
const valuefn = ref<string | number>('');
const loading = ref(false);
interface ListItem {
  value: string;
  label: string;
}
const remoteMethod = (query: string) => {
  if (query) {
    loading.value = true;
    setTimeout(async () => {
      loading.value = false;
      const res = await listuserlist(query);
      options.value = res.data;
    }, 200);
  } else {
    options.value = [];
  }
};
//查询模板
const idloading = ref(false);
interface ListItemoptions {
  id: string | number;
  status: string;
  templateContent: string;
  templateName: string;
  templateType: string;
}
const idoptions = ref<ListItemoptions[]>([]);
const idMethod = (query: string) => {
  if (query) {
    idloading.value = true;
    setTimeout(async () => {
      idloading.value = false;
      const res = await templateSelect(query);
      idoptions.value = res.data;
    }, 200);
  } else {
    idoptions.value = [];
  }
};

// const monge = ref<AccountOV[]>([]);
// const getaccountlist = async () => {
//   const res = await listtemplate();
//   console.log(res);
//   if (res.code == 200) {
//     monge.value = res.data.records;
//   }
// };
onMounted(() => {
  getlist();
  // getaccountlist();
});
</script>
