<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.company" clearable />
          </el-form-item>
          <el-form-item label="对应剧场名称">
            <el-input v-model="query.play_name" clearable />
          </el-form-item>
        </template>
      </Search>
      <div class="table-default">
        <Operate :show="open" v-action="'manage.MinprogramManage.MinprogramManageAdd'" />
        <el-table :data="tableData" class="mt-3" v-loading="loading">
          <el-table-column prop="id" label="ID" />
          <el-table-column prop="name" label="小程序名称">
            <template #default="scope">
              <el-tooltip placement="top" v-if="scope.row.remark">
                <template #content> {{ scope.row.remark }}<br /> </template>
                <span>{{ scope.row.name }}</span>
              </el-tooltip>
              <span v-else>{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="company" label="所属公司" />
          <el-table-column prop="play_name" label="对应剧场名称" />
          <el-table-column prop="status_name" label="状态" />
          <el-table-column prop="type_name" label="类型" />
          <el-table-column label="操作" width="200">
            <template #default="scope">
              <el-button link type="primary" size="small" @click="opendepots(scope.row)"
                v-action="'manage.MinprogramManage.MinprogramManageAllocation'">分配</el-button>
              <el-button link type="primary" size="small" @click="open(scope.row.id)"
                v-action="'manage.MinprogramManage.MinprogramManageEdit'">编辑</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';

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

const { data, query, search, reset, loading } = useGetList(api);
const { open, close, title, visible, id } = useOpen();

const tableData = computed(() => data.value?.data);


const opendepots = (data) => {
  depotsVisible.value = true
  depotsData.value = data
}
const closeDeptos = () => {
  depotsVisible.value = false
  search()
}

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