<template>
  <el-dialog v-model="props.modelValue" title="选择商品" width="900px" @closed="closeDialog" @open="openDialog">

    <el-form inline :model="queryParams">
      <el-form-item label="商品名称">
        <el-input v-model='queryParams.goodsName' placeholder='请输入商品名称'></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">
          搜索
        </el-button>
      </el-form-item>
    </el-form>

    <!-- 表格渲染  prop="对应的字段"-->
    <el-table v-loading="loading" :data="goodssList" ref="tableRef" border highlight-current-row
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column prop="goodsPicture" width="150" label="图片" align="center">
        <template #default="scope">
          <el-image preview-teleported :hide-on-click-modal="true" lazy class="table-td-thumb" fit="contain"
            :src="scope.row.goodsPicture?.split(',')[0]" :preview-src-list="scope.row.goodsPicture?.split(',')">
            <div><el-icon :size="15">
                <document />
              </el-icon></div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="goodsName" width="450" label="商品名称" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="goodsPriceLowest" label="价格" align="center" />
      <el-table-column prop="goodsTotalInventory" label="库存总量" align="center" />

    </el-table>

    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleEditClick()">添加</el-button>
        <el-button @click="handleResetClick()">重置</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref, watch, nextTick } from 'vue';
import { ElMessage } from 'element-plus'
import { allGoodsList } from '@/api/business/GoodsManager/Goodss/goods.js'

// 业务参数
const loading = ref(false)
// 商品表单列表
const goodsTableData = ref([])
// 选中Id数组
const ids = ref([])


// 基础参数



const tableRef = ref(null);
// 查询参数
const queryParams = reactive({
  goodsName: "",
});

const emits = defineEmits(['update:modelValue']);
const props = defineProps({
  modelValue: Boolean,
  data: Object,
  // done: Function,
});
const formData = ref({
  ...props.data
});

const goodssList = ref([]);
// 打开窗口时运行
const openDialog = async () => {
  await getgoodsListFun()
}

/** 搜索按钮操作 */
function handleQuery() {
  getgoodsListFun()
}

const getgoodsListFun = async () => {
  loading.value = true
  await allGoodsList(queryParams).then((res) => {
    if (res.code == 200) {
      goodssList.value = res.data
      loading.value = false

    }
  })

  // 勾选已选中的
  goodssList.value.forEach(item => {
    props.data.couponApplicableScopeConfig.forEach(e => {
      if (item.goodsId === e.goodsId) {
        tableRef.value.toggleRowSelection(item);
      }
    });
  });
}

watch(props, (v) => {
  formData.value = v.data;
});

// -业务方法


// -基础方法

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.goodsId)
  goodsTableData.value = selection.map((item) => item)

  // console.log(ids.value,"商品的ids");
  // console.log(goodsTableData.value,"商品的表单列表");
}

const closeDialog = () => {
  queryParams.goodsName = ""
  ids.value = ""
  goodsTableData.value = []
  emits('update:modelValue', false);
};

// 提交 
const handleEditClick = async () => {
  if (goodsTableData.value.length !== 0) {

    formData.value.couponApplicableScopeConfig = goodsTableData.value
    formData.value.couponGoodsIds = ids.value
    console.log(formData.value, '外面的表单');
    if (formData.value.couponApplicableScopeConfig.length != 0) {
      // props.done();
      closeDialog();
    }
  } else {
    ElMessage.error("请选择商品")
    return;
  }
};

const handleResetClick = async formEl => {

};
</script>

