<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="resources" 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.selectedItems }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品图片">
        <template #default="scope">
          <div>
            <img :src="scope.row.img" 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.resource }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品数量">
        <template #default="scope">
          <div>
            <span style="margin-left: 10px">{{ scope.row.quantity }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品状态">
        <template #default="scope">
          <div>
            <span style="margin-left: 10px">{{ scope.row.state }}</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)">
            {{scope.row.attribute}}
          </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="pageNum"
          :page-size="pageSize"
          :page-sizes="[2, 5, 10, 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.selectedItems" disabled></el-input>
      </el-form-item>
      <el-form-item label="商品图片">
        <div v-if="selectedProduct.img">
          <img :src="selectedProduct.img" 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.resource" 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.quantity" disabled></el-input>
      </el-form-item>
      <el-form-item label="商品状态">
        <el-select v-model="selectedProduct.state" 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.desc" 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.selectedItems"></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.resource" 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.quantity"></el-input>
      </el-form-item>
      <el-form-item label="商品状态">
        <el-select v-model="editForm.state"  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.desc"></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';
const pageNum = ref(0);
const pageSize = ref(0);
const total = ref(0);
const selectedItems = ref([]);
onMounted(() => {
  load();
});
const handleSelectionChange = (row: any) => {
  selectedItems.value = JSON.parse(JSON.stringify(row));
};
const username = ref('');
const detailDialogVisible = ref(false);
const editDialogVisible = ref(false);
const resources = ref('');
const Region = ref('');
let selectedProduct = ref('');
let editForm = ref({
  selectedItems: '',
  img: '',
  name: '',
  price: '',
  resource: '',
  quantity: '',
  state: '',
  attribute:'',
  region: '',
  label: '',
  desc: '',
});
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) => {
  if (row.attribute === '上架') {
    row.state = '上架';
    row.attribute = '下架';
  } else if (row.attribute === '下架') {
    row.state = '下架';
    row.attribute = '上架';
  }
};
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 tableData = ref([
  {
    selectedItems: '001',
    img: 'https://s21.ax1x.com/2024/10/24/pAdzjZ6.png',
    name: '随便起的',
    price: '300rmb',
    resource: '头饰',
    quantity: '10',
    state: '上架',
    region: '是',
    label: '送礼好物',
    desc: '描述',
    attribute:'下架',
  },
]);
const list = {
  region: '',
  label: [],
  desc: '',
};
const reset = () => {
  username.value = '';
  resources.value = '';
  Region.value = '';
};
const saveEdit = () => {
  const indexToUpdate = tableData.findIndex(item => item.selectedItems === editForm.value.selectedItems);
  if (indexToUpdate!== -1) {
    tableData[indexToUpdate] = {
      ...tableData[indexToUpdate],
      ...editForm.value,
    };
    ElMessage({
      message: '编辑成功',
      type: 'success',
    });
    editDialogVisible.value = false;
  };
};

const load = async () => {
  await instance.get('/admin/findAllUsers').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>