<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.name" clearable />
          </el-form-item>
          <el-form-item label="对应剧场名称">
            <el-input v-model="query.play_name" clearable />
          </el-form-item>
          <el-form-item label="所属公司">
            <el-select v-model="query.company" clearable filterable placeholder="请选择">
              <el-option v-for="(item, index) in companylist" :key="index" :label="item" :value="item" />
            </el-select>
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="query.type" clearable filterable placeholder="请选择">
              <el-option v-for="(item, index) in miniprogramTypelist" :key="index" :label="item.name"
                :value="item.value" />
            </el-select>
          </el-form-item>
        </template>
      </Search>
      <div class="table-default">
        <Operate :show="open" v-action="'manage.miniprogram.store'" />
        <el-table :data="tableData" class="mt-3" v-loading="loading">
          <el-table-column prop="id" label="ID" />
          <el-table-column prop="name" label="小程序名称" min-width="200">
            <template #default="scope">
              <el-tooltip placement="top" v-if="scope.row.remark">
                <template #content> {{ scope.row.remark }}<br /> </template>
                <div class="flex flex-col">
                  <div class="text-lg font-bold text-blue-400">{{ scope.row.name }}</div>
                  <div>
                    <span class="text-base">app id：</span>
                    <span class="text-base font-bold">{{ scope.row.appid }}</span>
                  </div>
                  <div v-if="isShowSecret">
                    <span class="text-base">app 密匙：</span>
                    <span class="text-base font-bold">{{ scope.row.appsecret }}</span>
                  </div>
                </div>
              </el-tooltip>
              <div v-else class="flex flex-col">
                <div class="text-lg font-bold text-blue-400 ">{{ scope.row.name }}</div>
                <div>
                  <span class="text-base">app id：</span>
                  <span class="text-base font-bold">{{ scope.row.appid }}</span>
                </div>
                <div v-if="isShowSecret">
                  <span class="text-base">app 密匙：</span>
                  <span class="text-base font-bold">{{ scope.row.appsecret }}</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="company" label="所属公司" />
          <el-table-column prop="play_name" label="对应剧场名称" />
          <el-table-column prop="pay_merchant_name" label="已绑定支付信息" />
          <el-table-column prop="status_name" label="状态" />
          <el-table-column prop="type_name" label="类型" />
          <el-table-column label="操作" width="200" fixed="right">
            <template #default="scope">
              <el-button link type="primary" size="small" @click="opendepots(scope.row)"
                v-action="'manage.miniprogram.allocationStore'">分配</el-button>
              <el-button link type="primary" size="small" @click="open(scope.row.id)"
                v-action="'manage.miniprogram.update'">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
        <Paginate />
      </div>
      <Dialog width="800px" v-model="depotsVisible" title="分配" destroy-on-close>
        <depotsTransfer @close="closeDeptos()" :primary="depotsData"></depotsTransfer>
      </Dialog>

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

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

const api = 'manage/miniprogram/index';
const depotsVisible = ref(false)
const depotsData = ref({})
const companylist = ref([])
const miniprogramTypelist = ref([])

const { data, query, search, reset, loading } = useGetList(api);
const { open, close, title, visible, id } = useOpen();
const rolesIdentify = inject('rolesIdentify')
const tableData = computed(() => data.value?.data);
const isShowSecret = computed(() => !rolesIdentify.value.includes('company') && !rolesIdentify.value.includes('optimizer'))

const opendepots = (data) => {
  depotsVisible.value = true
  depotsData.value = data
}
const closeDeptos = () => {
  depotsVisible.value = false
  search()
}
const init = () => {
  manageMiniprogramCompanylist().then(res => {
    companylist.value = res.data
  })
  manageMiniprogramTypelist().then(res => {
    miniprogramTypelist.value = res.data
  })
}

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