<template>
  <div>
    <div>
      <el-input style="width: 200px; height: 30px; margin-right: 10px;" suffix-icon="Search" placeholder="请输入商品名称" v-model="username"></el-input>
      <el-form-item label="商品类型" style="width: 200px; margin-right: 10px;display: inline-block">
        <el-select v-model="types" placeholder="请选择">
          <el-option label="材料包" value="材料包" />
          <el-option label="手持物" value="手持物" />
          <el-option label="头饰" value="头饰" />
          <el-option label="定制" value="定制" />
          <el-option label="积分兑换" value="积分兑换" />
        </el-select>
      </el-form-item>
      <el-form-item label="商品状态" style="width: 200px;margin-right: 10px;display: inline-block">
        <el-select v-model="Region" placeholder="请选择">
          <el-option label="已上架" value="上架" />
          <el-option label="已下架" value="下架" />
        </el-select>
      </el-form-item>
      <el-button class="ml-5" type="primary" @click="load" style="height: 25px;">搜索</el-button>
      <el-button type="warning" @click="reset" style="height:25px">重置</el-button>
    </div>
    <div style="margin: 15px 0">
      <el-popconfirm
          class="ml-5"
          confirm-button-text='确定'
          cancel-button-text='取消'
          icon="InfoFilled"
          icon-color="red"
          title="您确定批量删除这些数据吗？"
          @confirm="delBatch"
          width=180
      >
        <template #reference>
          <el-button style="height: 25px" class="ml-5" type="danger">批量删除 <el-icon style="margin-left: 5px;"><Remove /></el-icon></el-button>
        </template>
      </el-popconfirm>
    </div>
    <!-- 数据展示 -->
    <el-table :data="tableData" border stripe header-cell-class-name="headerBg" :cell-style="{textAlign: 'center'}"
              @selection-change="handleSelectionChange" :header-cell-style="{'text-align': 'center'}"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="商品编号" width="80">
        <template #default="scope">
          <div>
            <span style="align-items: center">{{ scope.row.id }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="goodImg" label="商品图片">
        <template #default="scope">
          <div>
            <img :src="scope.row.goodImg" alt="" style="height: 50px;">
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品名称">
        <template #default="scope">
          <div>
            <span style="margin-left: 10px">{{ scope.row.name }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品价格" width="180">
        <template #default="scope">
          <div>
            <span style="margin-left: 10px">{{ scope.row.price }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品类型">
        <template #default="scope">
          <div>
            <span style="margin-left: 10px">{{ scope.row.type }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品数量">
        <template #default="scope">
          <div>
            <span style="margin-left: 10px">{{ scope.row.inventory }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品状态">
        <template #default="scope">
          <div>
            <span style="margin-left: 10px" v-if="scope.row.isShelves==1">上架</span>
            <span style="margin-left: 10px" v-if="scope.row.isShelves==0">下架</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250px">
        <template #default="scope">
          <el-button size="small" @click="handleView(scope.$index, scope.row)">
            详情
          </el-button>
          <el-button size="small" @click="handleRevise(scope.$index, scope.row)">
            编辑
          </el-button>
          <el-button size="small"  @click="handleOff(scope.$index, scope.row)">
            <div v-if="scope.row.isShelves==1">下架 </div>
            <div v-if="scope.row.isShelves==0">上架 </div>
          </el-button>
          <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
          >
            删除
          </el-button>
          <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
          >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="searchParams.current"
          :page-size="searchParams.pageSize"
          :page-sizes="[5, 10, 15, 20]"
          :small="null"
          :disabled="null"
          :background="null"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      />
    </div>
  </div>
  <el-dialog v-model="detailDialogVisible" title="商品详情">
    <el-form label-width="100px">
      <el-form-item label="商品编号">
        <el-input v-model="selectedProduct.id" disabled></el-input>
      </el-form-item>
      <el-form-item label="商品图片">
        <div v-if="selectedProduct.goodImg">
          <img :src="selectedProduct.goodImg" alt="类别图标" class="thumbnail" style="width: 100px; height: 100px;"/>
        </div>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input v-model="selectedProduct.name" disabled></el-input>
      </el-form-item>
      <el-form-item label="商品价格">
        <el-input v-model="selectedProduct.price" disabled></el-input>
      </el-form-item>
      <el-form-item label="商品类型">
        <el-select v-model="selectedProduct.type" placeholder="请选择" disabled>
          <el-option label="材料包" value="材料包" />
          <el-option label="手持物" value="手持物" />
          <el-option label="头饰" value="头饰" />
          <el-option label="定制" value="定制" />
          <el-option label="积分兑换" value="积分兑换" />
        </el-select>
      </el-form-item>
      <el-form-item label="商品数量">
        <el-input v-model="selectedProduct.inventory" disabled></el-input>
      </el-form-item>
      <el-form-item label="商品状态">
        <el-select v-model="selectedProduct.isShelves" placeholder="请选择" disabled>
          <el-option label="上架" value="上架" />
          <el-option label="下架" value="下架" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否为限定产品">
        <el-select v-model="selectedProduct.region" placeholder="请选择" disabled>
          <el-option label="是" value="是" />
          <el-option label="否" value="否" />
        </el-select>
      </el-form-item>
      <el-form-item label="商品标签">
        <el-input v-model="selectedProduct.label" disabled></el-input>
      </el-form-item>
      <el-form-item label="商品描述">
        <el-input type="textarea" v-model="selectedProduct.introDetailintroDetail" disabled></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="detailDialogVisible = false">取消</el-button>
      </span>
    </template>
  </el-dialog>

  <el-dialog v-model="editDialogVisible" title="商品编辑">
    <el-form label-width="100px">
      <el-form-item label="商品编号">
        <el-input v-model="editForm.id"></el-input>
      </el-form-item>
      <el-form-item label="商品图片">
        <el-upload action="#" list-type="picture" :auto-upload="false">
          <el-icon><Plus /></el-icon>
          <template #file="{ file }">
            <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
          </template>
        </el-upload>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input v-model="editForm.name"></el-input>
      </el-form-item>
      <el-form-item label="商品价格">
        <el-input v-model="editForm.price"></el-input>
      </el-form-item>
      <el-form-item label="商品类型">
        <el-select v-model="editForm.type" placeholder="请选择">
          <el-option label="材料包" value="材料包" />
          <el-option label="手持物" value="手持物" />
          <el-option label="头饰" value="头饰" />
          <el-option label="定制" value="定制" />
          <el-option label="积分兑换" value="定制" />
        </el-select>
      </el-form-item>
      <el-form-item label="商品数量">
        <el-input v-model="editForm.inventory"></el-input>
      </el-form-item>
      <el-form-item label="商品状态">
        <el-select v-model="editForm.isShelves"  disabled>
          <el-option label="上架" value="上架" />
          <el-option label="下架" value="下架" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否为限定产品">
        <el-select v-model="editForm.region" placeholder="请选择">
          <el-option label="是" value="是" />
          <el-option label="否" value="否" />
        </el-select>
      </el-form-item>
      <el-form-item label="商品标签">
        <el-input v-model="editForm.label"></el-input>
      </el-form-item>
      <el-form-item label="商品描述">
        <el-input type="textarea" v-model="editForm.introDetail"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveEdit">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts"> 
import { ElMessage } from 'element-plus';
import { ref, onMounted } from 'vue';
import myAxios from "@/api/myAxios";
//总页数
const total = ref(0);
const id = ref([]);
//封装分页
const searchParams: any = ref({
  //当前页码
  current: 1,
  //每页显示条数
  pageSize: 5
})
const username = ref('');
const detailDialogVisible = ref(false);
const editDialogVisible = ref(false);
const types = ref('');
const Region = ref('');
let selectedProduct = ref('');
const loading = ref(false)
const tableData = ref([]);
//商品状态
const status = ref(0)
let editForm = ref({
  id: '',
  goodImg: '',
  name: '',
  price: '',
  type: '',
  inventory: '',
  isShelves: '',
  attribute:'',
  region: '',
  label: '',
  introDetail: '',
});
//页面加载时获取商品列表
onMounted(() => {
  getGoodList()
})
const handleSelectionChange = (row: any) => {
  id.value = JSON.parse(JSON.stringify(row));
};
const handleView = (index: number, row: any) => {
  selectedProduct = row;
  detailDialogVisible.value = true;
};
const handleRevise = (index: number, row: any) => {
  selectedProduct = row;
  editDialogVisible.value = true;
  editForm.value = row;
};
const handleOff = (index: number, row: any) => {
  console.log()
  if (row.isShelves === 0) {
    row.isShelves = 1;//0下架，1上架
    console.log(row.isShelves)
  } else if (row.isShelves ===1) {
    row.isShelves = 0;
  }
}

const handleDelete = (index: number, row: any) => {
  const confirmDelete = window.confirm('您确定要删除该商品吗？');
  if (confirmDelete) {
    tableData.value = tableData.value.filter((item, i) => i!== index);
    ElMessage({
      message: '删除成功',
      type: 'success',
    });
  }
};

const getGoodList = async () => {
  loading.value = true;
  try {
    const res = await myAxios.post('/goods/list/page', {...searchParams.value });
    console.log(res.data)
    if (res.data.code === 1) {
      tableData.value = res.data.data.records;
      total.value = parseInt(res.data.data.total)//总数据量，用于分页
    } else {
      ElMessage({
        message: '获取数据失败',
      });
    }
  } catch (error) {
    ElMessage({
      message: '发生错误',
    });
  }
}
//处理行数大小变化
const handleSizeChange =(newSize : any)=>{
  searchParams.value.pageSize = newSize //新的页面数
  getGoodList()
}
//处理当前页面变化
const handleCurrentChange = (Current : any) => {
  searchParams.value.current = Current 
  getGoodList()
}
const reset = () => {
  username.value = '';
  types.value = '';
  Region.value = '';
};
// const saveEdit = () => {
//   const indexToUpdate = tableData.findIndex(item => item.id === editForm.value.id);
//   if (indexToUpdate!== -1) {
//     tableData[indexToUpdate] = {
//       ...tableData[indexToUpdate],
//       ...editForm.value,
//     };
//     ElMessage({
//       message: '编辑成功',
//       type: 'success',
//     });
//     editDialogVisible.value = false;
//   };
// };

// const getListPage = async () => {
//   await instance.post('/goods/list/page').then(res => {
//     console.log(res.data.data);
//     res.data.data.forEach(item => {
//       console.log(item);
//     });
//     tableData.value = res.data.data;
//   });
// };
const delBatchSuccess = () => {
  ElMessage({
    message: '批量删除成功',
    type: 'success',
  });
};
// const delBatch = async () => {
//   await instance.post('').then(res => {
//     if (res.data.code === '200') {
//       load();
//     } else {
//       ElMessage({
//         message: '批量删除失败',
//         type: 'error',
//       });
//     }
//   });
// };
</script>

<style scoped>
.headerBg {
  background-color: #eee!important;
}
</style>