<template>
  <el-form :model="formCallback" label-width="120px" ref="form" v-loading="loading" class="pr-4">
    <el-form-item label="列表名称" prop="ids" :rules="[{ required: false, message: '列表名称必须填写' }]" label-width="120px">
      <el-input v-model="props.primary.type_str" disabled size="default" clearable></el-input>
    </el-form-item>
    <el-form-item label="短剧" prop="duanjus" :rules="[{ required: true, message: '选择短剧' }]">
      <div>
        <div class="flex flex-wrap mb-5" v-if="formCallback.duanjus.length > 0">
          <div v-for="(item, index) in formCallback.duanjus" :key="item.id" class="mt-3 mr-3">
            <el-input type="number" :disabled="false" min="1" v-model.number="sortValues[index]" placeholder="排序"
              size="default" style="width: 115px;"></el-input>
            <el-input v-model="item.name" :disabled="true" placeholder="短剧名称" style="width:300px;"
              class="input-with-select">
            </el-input>
            <el-icon class="ml-3 cursor-pointer" @click="chooseDel(item, index)">
              <Delete />
            </el-icon>
          </div>
        </div>
        <div>
          <el-button type="primary" size="default" @click="chooseShort">选择短剧</el-button>
        </div>
      </div>
    </el-form-item>
    <div class="flex justify-end">
      <el-button type="primary" @click="submitForm(form)">{{
        $t('system.confirm')
      }}</el-button>
    </div>
  </el-form>
  <Dialog v-model="videoVisible" title="选择短剧" width="90%" :alignCenter="true" destroy-on-close>
    <videoList @confirm="confirm" :primary="formCallback.duanjus"></videoList>
  </Dialog>
</template>

<script lang="ts" setup>
import videoList from './videoList.vue';
import { Close, Delete } from '@element-plus/icons-vue';
import { FormInstance } from 'element-plus';
import { operationManageFirstPageSetConfig } from '@/api/pageLayout/homePage/index';
import { useRouter, useRoute } from 'vue-router'
const props = defineProps({
  primary: Object,
});
const sortValues = ref([])
const router = useRouter()
const route = useRoute()
const emit = defineEmits(['close']);
const loading = ref(false)
const videoVisible = ref(false)
const promotion = ref('')
const form = ref()
const formCallback = ref({
  duanjus: []
})


const chooseShort = () => {
  videoVisible.value = true
}
const validateData = () => {
  const sortSet = new Set();
  const nameSet = new Set();

  for (const item of formCallback.value.duanjus) {
    if (item.sort === null || item.sort === undefined) {
      ElMessage.error(`${item.name}的排序不能为空`)
      return false;
    }
    if (sortSet.has(item.sort)) {
      ElMessage.error(`${item.name}的排序重复`)
      return false;
    }
    sortSet.add(item.sort);
    if (nameSet.has(item.name)) {
      ElMessage.error(`${item.name}的排序重复`)
      return false;
    }

    nameSet.add(item.name);
  }

  return true;
};
const confirm = (e) => {
  formCallback.value.duanjus = e
  if (formCallback.value.duanjus.length > 0) {
    sortValues.value = formCallback.value.duanjus.map(el => el.sort)
  }
  videoVisible.value = false
}
const chooseDel = (row: object, idx: number) => {
  formCallback.value.duanjus.splice(idx, 1);
}
// 提交回传配置
const submitForm = (formEl: FormInstance | undefined) => {
  console.log(formCallback.value, 'formCallbackformCallback');
  formCallback.value.duanjus = formCallback.value.duanjus.map((el, index) => {
    return {
      id: el.id,
      sort: sortValues.value[index],
      name: el.name
    }
  })
  if (!formEl) return;
  if (!validateData()) return
  loading.value = true;
  formEl
    .validate(valid => {
      if (valid) {
        operationManageFirstPageSetConfig(formCallback.value).then(res => {
          ElMessage.success(res.message)
          emit('close')
        })
        loading.value = false;
      } else {
        loading.value = false;
      }
    })
    .then(() => { });
}

if (props.primary) {
  console.log(props.primary, 'props.primaryprops.primary');
  formCallback.value.id = props.primary.id
  formCallback.value.duanjus = JSON.parse(JSON.stringify(props.primary.duanjus))
  if (formCallback.value.duanjus.length > 0) {
    sortValues.value = formCallback.value.duanjus.map(el => el.sort)
  }
}
onMounted(() => {
});
</script>

<style lang="scss" scoped>
:deep(.input-with-select .el-input-group__prepend) {
  background-color: #FFFFFF;
  padding: 0;
  border: 1px solid #dcdfe6;
}
</style>
