<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item :label="t('mp.name')" prop="name">
        <el-input
          v-model="queryParams.name"
          :placeholder="t('mp.name')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" />{{t('common.search')}}</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" />{{t('common.reset')}}</el-button>
        <el-button type="primary" @click="openForm('create')" v-hasPermi="['ma:account:create']">
          <Icon icon="ep:plus" class="mr-5px" /> {{t('action.add')}}
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column :label="t('mp.name')" align="center" prop="name" />
      <el-table-column label="appId" align="center" prop="appId" width="180" />
      <el-table-column :label="t('mp.isMainAccount')" align="center" prop="isMain">
        <template #default="scope">
          <span v-if="scope.row.isMain == 1" style="color:red">{{t('common.yes')}}</span>
          <span v-else>{{t('common.no')}}</span>
         </template>
      </el-table-column>
      <el-table-column :label="t('public.remark')" align="center" prop="remark" />
      <el-table-column :label="t('public.operate')" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['ma:account:update']"
          >
          {{t('action.edit')}}
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['ma:account:delete']"
          >
            {{ t('public.delete') }}
          </el-button>
          <el-button
            link
            type="primary"
            @click="setMain(scope.row)"
            v-hasPermi="['ma:account:set-main']"
          >
          {{ t('mp.setAsMainAccount') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 对话框(添加 / 修改) -->
  <AccountForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts" name="MpAccount">
import * as AccountApi from '@/api/mp/account2'
import AccountForm from './AccountForm.vue'
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: null,
  account: null,
  appId: null
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await AccountApi.getAccountPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

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

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

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await AccountApi.deleteAccount(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}


const setMain = async (row) => {
  try {
    // 清空 API 配额的二次确认
    await message.confirm('是否确认设置公众号账号编号为"' + row.name + '"主账户?')
    // 发起清空 API 配额
    await AccountApi.setAccountMain(row.id)
    message.success(t('mp.settingSucceeded'))
    getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
