<template>
  <el-form :model="form" label-width="130px" ref="ruleForm" :rules="rules" v-loading="loading" class="pr-4">
    <div class="flex flex-row justify-between">
      <div class="w-full">
        <el-form-item label="小程序名称" prop="email">
          <el-input v-model="form.name" disabled placeholder="请填写登录账号" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch v-model="form.status" disabled :active-value="1" :inactive-value="0" />
        </el-form-item>
        <el-form-item label="分配至投手公司" prop="username">
          <div>
            <el-transfer v-model="form.depotsValue" :props="{
              key: 'id',
              label: 'username',
            }" :titles="['未分配', '已分配']" filterable :left-default-checked="leftChecked"
              :right-default-checked="rightChecked" @right-check-change="transferChange($event, 'right')"
              @left-check-change="transferChange($event, 'left')" :filter-method="filterMethod" filter-placeholder="搜索"
              :data="dataObj" />
          </div>
        </el-form-item>
      </div>
    </div>
    <div class="flex justify-end">
      <el-button type="primary" size="default" @click="doSave">确定</el-button>
    </div>
  </el-form>
</template>

<script lang="ts" setup>
import { manageMiniprogramAllocationDetail, manageMiniprogramShow, manageMiniprogramAllocation } from '@/api/applet/index'

const props = defineProps({
  primary: Object,
});
const loading = ref(false)
const rules = reactive({
  miniProgramIds: [{ required: true, message: '请选择小程序' }],
});
const form = ref({})
const dataObj = ref([])
const leftChecked = ref([])
const rightChecked = ref([])
const channels = ref([])
const emit = defineEmits(['close']);
const params = ref({})
const transferChange = (e, type) => {
  console.log(e, 'transferChangetransferChange');
  let arr = JSON.parse(JSON.stringify(dataObj.value))
  const checkArr = arr.filter(el => e.includes(el.id)).map(el => el.id)
  channels.value = checkArr
  params.value = { uids: channels.value.join(','), action: type == 'left' ? 'off' : 'on' }
  console.log(params.value, channels.value, 'checkArrcheckArr');
}
const filterMethod = (query, item) => {
  return item.username.includes(query)
}
const doSave = () => {
  console.log(form.value.depotsValue, 'channels.value', toRaw(channels.value));
  if (toRaw(channels.value).length <= 0) {
    ElMessage.warning('请选择投手公司')
  } else {
    manageMiniprogramAllocation(props.primary.id, params.value).then(res => {
      console.log(res);
      ElMessage.success(res.message)
      emit('close', false)
    })
  }
}


onMounted(() => {
  manageMiniprogramAllocationDetail(props.primary.id).then(res => {
    console.log(res, 'manageMiniprogramAllocationDetail');
    dataObj.value = res.data.all
    let timer = setTimeout(() => {
      if (res.data.on?.length > 0) {
        form.value.depotsValue = res.data.on.map(el => el.id)
        rightChecked.value = res.data.on.map(el => el.id)
        clearTimeout(timer)
      }
    }, 10);
  });
  manageMiniprogramShow(props.primary.id).then(res => {
    console.log(res, 'props.primary');
    form.value = res.data
  })

})

</script>

<style lang="scss" scoped>
::v-deep(.el-input__wrapper) {
  display: flex;
  width: 180px;
  flex: none;
}

.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}
</style>
