<template>
  <div class="Table">
    <!-- 按钮 -->
    <AddButton @add="add"></AddButton>
    <!-- 表格 -->
    <Table :list="datas.tableData" @handleDelete="handleDelete" @handleEdit="handleEdit"></Table>
    <!-- 分页 -->
    <Paging :pagingConfig="datas.pagingConfig" @sizeChange="sizeChange" @currentChange="currentChange"></Paging>
    <!-- 其他模板 -->
    <!-- 新增/编辑 -->
    <myDialog ref="myDialogRef" :title="datas.title" @confirm="confirm" width="30%">
      <el-form ref="ruleFormRef" :rules="datas.rules" :model="datas.DialogForm" label-width="80px">
        <el-form-item label="选择用户">
          <el-select v-model="datas.DialogForm.userId" filterable placeholder="请选择选择用户" style="width: 240px">
            <el-option v-for="item in datas.options" :key="item.userId" :label="`${item.nickName}/${item.phone}`"
              :value="item.userId" />
          </el-select>
        </el-form-item>
        <el-form-item label="选择酒品">
          <el-select v-model="datas.DialogForm.shopStorageGoodId" placeholder="请选择选择酒品" style="width: 240px">
            <el-option v-for="item in datas.options2" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="数量">
          <el-col :span="3">
            <el-input-number v-model="datas.DialogForm.num" />
          </el-col>
        </el-form-item>
        <el-form-item label="过期时间">
          <el-input-number v-model="datas.DialogForm.expDay" />
          <!-- <el-date-picker v-model="datas.DialogForm.expDay" type="date" value-format="YYYY-MM-DD" /> -->
        </el-form-item>
      </el-form>
    </myDialog>
    <!-- 选择图片 -->
    <!-- <AddImg ref="addImg" @successEvent="successEvent"></AddImg> -->
    <!-- 查看记录 -->
    <myDialog ref="myDialogRefs" title="记录" @confirm="confirms" width="30%">
      <el-table :data="datas.storingWineList" border style="width: 100%">
        <el-table-column prop="content" align="center" label="记录"> </el-table-column>
        <el-table-column prop="time" align="center" label="操作时间"> </el-table-column>
      </el-table>
    </myDialog>
    <!-- 取酒 -->
    <myDialog ref="myDialogRefqs" title="记录" @confirm="confirmqs" width="30%">
      <el-col :span="3">
        <el-input-number v-model="datas.winereNum" min="0" :max="myDialogRefqsdata.num" />
      </el-col>
    </myDialog>
  </div>
</template>
<script setup>
import AddButton from './component/AddButton.vue'
import Table from './component/Table.vue'
import Paging from '@/components/mycomponents/myPaging.vue'
import myDialog from '@/components/mycomponents/myDialog.vue'
import eventBus from '@/utils/eventBus'
import API from './api'

const datas = reactive({
  tableData: [], // 表格数据
  title: '新增数据',
  pagingConfig: {
    total: 0, // 总数
    pageSize: 10, // 每页数据数量
    pageNumber: 1, // 当前页码
  },
  DialogForm: { // 弹窗表单数据
    num: 1,
    expDay: 1
  },
  // 取酒
  winereNum: 1,
  // 存酒记录
  storingWineList: [],
  options: [],
  options2: [],
  rules: {
    name: [
      { required: true, message: '请输入供应商名称', trigger: 'blur' },
      { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
    ],
  }
})
const myDialogRef = ref(null)
const myDialogRefs = ref(null)
const myDialogRefqs = ref(null)
const myDialogRefqsdata = ref(null)
const ruleFormRef = ref(null)
onMounted(() => {
  getList()
  // 获取店铺用户信息
  getshopUser()
  // 获取酒品列表
  getstorageGood()
  eventBus.on('search', (res) => {
    getList(res)
  })
})
onBeforeUnmount(() => {
  eventBus.off('search')
})
async function getList(data = {}) {
  const res = await API.getPage({ page: datas.pagingConfig.pageNumber, size: datas.pagingConfig.pageSize, ...data })
  datas.tableData = res.records
  datas.pagingConfig.total = res.totalRow
  datas.pagingConfig.pageSize = res.pageSize
  datas.pagingConfig.pageNumber = res.pageNumber
}
async function getshopUser(params) {
  const res = await API.getshopUser({ page: 1, size: 999, ...params })
  datas.options = res.records
}
async function getstorageGood() {
  const res = await API.storageGood({})
  datas.options2 = res.records
}
function add() {
  if (datas.DialogForm.id) {
    rest()
  }
  datas.title = '新增数据'
  myDialogRef.value.open()
}
async function handleEdit(row) {
  // 查看记录
  const res = await API.record(row.id)
  datas.storingWineList = res
  // 有图片
  // datas.DialogForm.goodsImageUrl = res.goodsImageUrl.split(',')
  myDialogRefs.value.open()
}
async function confirm() {
  ruleFormRef.value.validate(async valid => {
    if (valid) {
      let res = null
      if (datas.title == '新增数据') {
        // 图片处理
        // datas.DialogForm.goodsImageUrl = datas.DialogForm.goodsImageUrl.join(',')
        res = await API.add(datas.DialogForm)
      } else {
        // datas.DialogForm.goodsImageUrl = datas.DialogForm.goodsImageUrl.join(',')
        res = await API.update(datas.DialogForm)
      }
      ElMessage({
        message: '成功',
        type: 'success',
      })
      rest()
      getList()
      myDialogRef.value.close()
    }
  })
}
function confirms() {
  myDialogRefs.value.close()
}
async function confirmqs() {
  let res = await API.update({ id: myDialogRefqsdata.value.id, num: -datas.winereNum })
  ElMessage({
    message: '成功',
    type: 'success',
  })
  getList()
  myDialogRefqs.value.close()
}
// 重置
function rest() {
  datas.DialogForm = { sort: 1, images: [] }
}
function handleDelete(item) {
  myDialogRefqsdata.value = item
  myDialogRefqs.value.open()
}

// 图片
const addImg = ref(null)
function addimgEvent() {
  (addImg.value)?.show()
}
function successEvent(d) {
  datas.DialogForm['images'].push(d[0].url);
}
function deleteEvent(d) {
  let index = datas.DialogForm.images.findIndex((ele) => ele == d);
  datas.DialogForm.images.splice(index, 1);
}
// 分页
function sizeChange(val) {
  datas.pagingConfig.pageSize = val
  getList()
}
function currentChange(val) {
  datas.pagingConfig.pageNumber = val
  getList()
}

</script>
<style scoped lang="scss">
.Table {
  padding: 20px;
  background-color: #fff;
  border: 1px solid #e4e7ed;
  margin-top: 20px;
  border-radius: 4px;
}

// 图片库样式
.showStyle:hover>.buttonstyle {
  display: block;
}

.upImgStyle {
  cursor: pointer;
  width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
  border: 1px dashed #ccc;
  border-radius: 1%;
  font-size: 30px;
  color: #ccc;
}

.buttonstyle {
  border-radius: 50%;
  color: #db1616;
  background-color: #fff;
  font-size: 20px;
  display: none;
  position: absolute;
  right: 0px;
  top: -10px;
  z-index: 10;
}
</style>