<!-- d:\wordpack\WholeProcessPlatform\frontend\src\views\system\map\components\ConfigManagement\index.vue -->
<template>
  <div class="content">
    推送配置
    <!-- <ConfigManagementSearch
      ref="configManagementSearch"
      :handle-add="handleAdd"
      @reset="handleReset"
      @search-finish="onSearchFinish"
    /> -->
    <div><a-button type="primary" @click="handleAdd">新增</a-button></div>
    <BasicTable
      ref="basicTable"
      :columns="columns"
      :list-url="getPushConfigList"
      :search-params="searchParams"
    >
      <template #isSms="{ column, record }">
        <a-switch
          v-model:checked="record.isSms"
          :unCheckedValue="0"
          :checkedValue="1"
          @change="switchChange(record.isSms, record)"
        />
      </template>
      <template #action="{ column, record }">
        <div class="flex gap-[6px]">
          <a-button
            class="!p-0"
            type="link"
            size="small"
            @click="handlePushTarget(record)"
            >推送目标</a-button
          >
          <a-button
            class="!p-0"
            type="link"
            size="small"
            @click="handleEdit(record)"
            >编辑</a-button
          >
          <a-button
            class="!p-0"
            type="link"
            size="small"
            danger
            @click="handleDelete(record)"
            >删除</a-button
          >
        </div>
      </template>
    </BasicTable>
    <ConfigManagementForm
      ref="configManagementForm"
      v-model:visible="editModalVisible"
      :initial-values="currentRecord"
      @cancel="editModalCancel"
      @ok="handleEditSubmit"
    />
    <PushTargetModal
      ref="pushTargetModal"
      v-model:visible="pushTargetModalVisible"
      :initial-values="pushTargetInitialValues"
      @cancel="pushTargetModalCancel"
      @ok="handlePushTargetSubmit"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, nextTick, computed,onMounted } from 'vue';
import BasicTable from '@/components/BasicTable/index.vue';
import ConfigManagementForm from './ConfigManagementForm.vue';
import PushTargetModal from './PushTargetModal.vue';
import { message, Modal } from 'ant-design-vue';
import {
  getPushConfigList,
  addPushConfig,
  updatePushConfig,
  deletePushConfig,
  savePushTargets,
  setPushConfigSmsSwitch
} from '@/api/system/map/pushManagement';

// 表格实例
const basicTable = ref<any>(null);
// 表格列配置
const columns = [
  {
    title: '消息类型',
    dataIndex: 'messageType',
    key: 'messageType'
  },
  {
    title: '当日推送次数',
    key: 'dailyPushCount',
    dataIndex: 'dailyPushCount'
  },
  {
    title: '累计推送次数',
    dataIndex: 'pushCount',
    key: 'pushCount'
  },
  {
    title: '短信提醒',
    key: 'isSms',
    dataIndex: 'isSms',
    slots: { customRender: 'isSms' }
  },
  {
    title: '操作',
    key: 'action',
    width: 160,
    fixed: 'right',
    slots: { customRender: 'action' }
  }
];

// 搜索参数
const searchParams = ref({});
// 编辑弹窗数据
const currentRecord = ref<any | null>(null);
const editModalVisible = ref(false);

// 推送目标弹窗
const pushTargetModal = ref<any>(null);
const pushTargetModalVisible = ref(false);
const pushTargetInitialValues = ref<any | null>(null);

// 新增处理
const handleAdd = () => {
  currentRecord.value = null;
  editModalVisible.value = true;
};

// 编辑处理
const handleEdit = (record: any) => {
  currentRecord.value = { ...record };
  editModalVisible.value = true;
};

// 推送目标处理
const handlePushTarget = (record: any) => {
  pushTargetInitialValues.value = { ...record };
  pushTargetModalVisible.value = true;
};

// 推送目标弹窗取消
const pushTargetModalCancel = () => {
  pushTargetInitialValues.value = null;
  pushTargetModalVisible.value = false;
};

// 推送目标提交
const handlePushTargetSubmit = async (values: any) => {
  try {
    let res: any = await savePushTargets(values);
    if (res.code == 0) {
      message.success('保存成功');
    }
    pushTargetModalVisible.value = false;
    basicTable.value.refresh();
  } catch (error) {
    // message.error('保存失败');
    return;
  }
};

// 删除处理
const handleDelete = (record: any) => {
  Modal.confirm({
    title: '确认删除',
    content: '确定要删除选中的记录吗？',
    zIndex: 2002,
    onOk: async () => {
      try {
        let res:any = await deletePushConfig([record.id]);
        if ((res.code = 0)) {
          message.success('删除成功');
        }
        basicTable.value.refresh();
      } catch (error) {
        // message.error('删除失败');
        return;
      }
    }
  });
};

// 搜索完成处理
const onSearchFinish = () => {
  const params = {
    logic: 'and',
    filters: [].filter(Boolean)
  };
  basicTable.value.getList(params);
};
// 表单取消
const editModalCancel = () => {
  currentRecord.value = null;
  editModalVisible.value = false;
};

// 表单提交
const handleEditSubmit = async (values: any) => {
  try {
    //updatePushConfig
    if (values.id) {
      let res: any = await updatePushConfig({ ...values });
      if ((res.code = 0)) {
        message.success(`修改成功`);
      }
    } else {
      let res: any = await addPushConfig({ ...values });
      if ((res.code = 0)) {
        message.success(`保存成功`);
      }
    }

    editModalVisible.value = false;
    basicTable.value.refresh();
  } catch (error) {
    // message.error(`保存失败`);

    editModalVisible.value = false;
    return;
  }
};
//
const switchChange = async (checked: any, record: any) => {
  Modal.confirm({
    title: '确认修改',
    content: '确定要修改短信提醒的开关状态吗？',
    zIndex: 2002,
    onOk: async () => {
      try {
        let res: any = await setPushConfigSmsSwitch({
          id: record.id,
          isSms: checked ? '1' : '0'
        });
        if (res.code == 0) {
          message.success(`修改成功`);
          basicTable.value.refresh();
        }
      } catch (error) {
        return;
      }
    }
  });
};

const initOption = async () => {};
onMounted(() => {
  initOption();
  onSearchFinish();
});
</script>

<style scoped lang="scss">
.config-management {
  height: 100%;
  padding: 16px;
}
</style>
