<template>
  <div class="flex flex-col justify-between w-full sm:flex-row">
    <div class="w-full">
      <Search :search="search" :reset="reset">
        <template v-slot:body>
          <el-form-item label="邮箱">
            <el-input v-model="query.email" clearable />
          </el-form-item>
          <el-form-item label="用户名">
            <el-input v-model="query.username" clearable />
          </el-form-item>
          <el-form-item label="小程序">
            <el-select v-model="query.miniProgramId" filterable clearable placeholder="选择小程序">
              <el-option v-for="item in applet" :key="item.miniprogram_id" :label="item.name"
                :value="item.miniprogram_id" />
            </el-select>
          </el-form-item>
        </template>
      </Search>
      <div class="table-default">
        <Operate :show="open" v-action="'channel.Advertiser.addAdvertiser'" />
        <el-table :data="tableData" class="mt-3" v-loading="loading">
          <el-table-column prop="id" label="ID" />
          <el-table-column prop="email" label="邮箱">
            <template #default="scope">
              <el-tooltip placement="top" v-if="scope.row.remark">
                <template #content> {{ scope.row.remark }}<br /> </template>
                <span>{{ scope.row.email }}</span>
              </el-tooltip>
              <span v-else>{{ scope.row.email }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="用户名" />
          <el-table-column prop="miniProgramIds" label="已绑定小程序">
            <template #default="scope">
              <div v-for="applet in scope.row.miniPrograms" :key="applet.id" class="flex items-center h-8">
                <el-image style="width: 20px; height: 20px" class="mt-1 mr-5" :src="applet.type_logo" fit="contain" />
                <span class="mr-3 text-sm font-medium">{{ applet.name }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" v-action="'User.user.enable'">
            <template #default="scope">
              <Status v-model="scope.row.status" :id="scope.row.id" :api="statusapi" />
            </template>
          </el-table-column>
          <el-table-column prop="created_at" label="创建时间" />
          <el-table-column label="操作" width="200">
            <template #default="scope">
              <Update @click="open(scope.row.id)" v-action="'channel.Advertiser.updateAdvertiser'" />
            </template>
          </el-table-column>
        </el-table>

        <Paginate />
      </div>

      <Dialog v-model="visible" :title="title" destroy-on-close>
        <Create @close="close(search)" :primary="id" />
      </Dialog>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref } from 'vue';
import Create from './create.vue';
import { useGetList } from '@/hook/curd/useGetList';
import { useOpen } from '@/hook/curd/useOpen';
import { channelMiniprogramList } from '@/api/applet/index'

const statusapi = 'users';
const applet = ref([])
const api = 'channel/advertiser/listAdvertiser';
const { data, query, search, reset, loading } = useGetList(api);
const { open, close, title, visible, id } = useOpen();

const tableData = computed(() => data.value?.data);
const initApplet = () => {
  channelMiniprogramList().then(res => {
    console.log(res, 'channelMiniprogramListchannelMiniprogramList');
    applet.value = res.data
  })
}

onMounted(() => {
  initApplet();
  search();
});
</script>
