<template>
  <div class="Table">
    <!-- 按钮 -->
    <AddButton @add="add"></AddButton>
    <!-- 表格 -->
    <Table :list="datas.tableData" @handleDelete="handleDelete" @handleEdit="handleEdit"
      @handleStatusChange="handleStatusChange"></Table>
    <!-- 分页 -->
    <Paging :pagingConfig="datas.pagingConfig" @sizeChange="sizeChange" @currentChange="currentChange"></Paging>
    <!-- 其他模板 -->
    <!-- 新增/编辑 -->
    <myDialog ref="myDialogRef" :title="datas.title" @confirm="confirm" width="30%">
      <el-form ref="ruleFormRef" :model="datas.DialogForm" label-width="120px">
        <el-form-item label="上传弹窗广告" prop="imgUrl">
          <div style="display: flex; flex-wrap: wrap">
            <div v-for="(item, index) in datas.DialogForm.imgUrl" :key="index" style="position: relative"
              class="showStyle">
              <el-icon class="buttonstyle" @click="deleteEvent(item)">
                <DeleteFilled />
              </el-icon>
              <img style="width: 148px; height: 148px; margin-right: 6px" class="imgStyle" :src="item" alt="" />
            </div>
            <div class="upImgStyle" @click="addimgEvent">+</div>
          </div>
        </el-form-item>
        <el-form-item label="圆角设置">
          <div class="slider-demo-block">
            <el-slider v-model="datas.DialogForm.borderRadius" style="width: 300px;" />
          </div>
        </el-form-item>
        <el-form-item label="弹窗位置">
          <el-radio-group v-model="datas.DialogForm.showPosition">
            <el-radio label="首页" value="home" />
            <el-radio label="点餐页" value="make_order" />
          </el-radio-group>
        </el-form-item>
        <el-form-item label="弹窗频率">
          <el-radio-group v-model="datas.DialogForm.frequency">
            <el-radio label="仅首次展示" value="only_one" />
            <el-radio label="每次打开都展示" value="every_show" />
            <el-radio label="每天展示一次" value="every_day" />
            <el-radio label="每三天展示一次" value="three_day" />
            <el-radio label="每七天展示一次" value="seven_day" />
            <el-radio label="每30天展示一次" value="thirty_day" />
          </el-radio-group>
        </el-form-item>
        <el-form-item label="跳转页面">
          <el-select v-model="datas.DialogForm.linkPath" placeholder="跳转页面" clearable>
            <el-option :label="item.name" :value="item.id" v-for="item in datas.linkPathList" />
          </el-select>
        </el-form-item>
      </el-form>
    </myDialog>
    <!-- 选择图片 -->
    <AddImg ref="addImg" @successEvent="successEvent"></AddImg>
  </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: { // 弹窗表单数据
    linkPath: "",
    imgUrl: [],
    borderRadius: 0,
    showPosition: "home",
    frequency: "only_one",
    status: 0
  },
  // 跳转页面列表
  linkPathList: [
  ],

  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 ruleFormRef = ref(null)
onMounted(() => {
  getList()
  // 获取小程序跳转页面
  getxcxlist()
  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
  datas.pagingConfig.total = res.totalRow
  datas.pagingConfig.pageSize = res.pageSize
  datas.pagingConfig.pageNumber = res.pageNumber
}
async function getxcxlist() {
  const res = await API.miniAppPagesPage({ page: datas.pagingConfig.pageNumber, size: datas.pagingConfig.pageSize, })
  datas.linkPathList = res.records
}
function add() {
  if (datas.DialogForm.id) {
    rest()
  }
  datas.title = '新增数据'
  myDialogRef.value.open()
}
async function handleEdit(row) {
  datas.title = '编辑数据'
  const res = await API.getinfo(row.id)
  datas.DialogForm = res
  // 有图片
  datas.DialogForm.imgUrl = res.imgUrl.split(',')
  myDialogRef.value.open()
}
async function handleStatusChange(d) {
  datas.title = '编辑数据'
  let res = await API.update(d)
  if (res) {
    ElMessage({
      message: '成功',
      type: 'success',
    })
    rest()
    getList()
  }
}

async function confirm() {
  ruleFormRef.value.validate(async valid => {
    if (valid) {
      let res = null
      if (datas.title == '新增数据') {
        // 图片处理
        datas.DialogForm.imgUrl = datas.DialogForm.imgUrl.join(',')
        res = await API.add(datas.DialogForm)
      } else {
        datas.DialogForm.imgUrl = datas.DialogForm.imgUrl.join(',')
        res = await API.update(datas.DialogForm)
      }
      if (res) {
        ElMessage({
          message: '成功',
          type: 'success',
        })
        rest()
        getList()
        myDialogRef.value.close()
      }
    }
  })
}
// 重置
function rest() {
  datas.DialogForm = { sort: 1, imgUrl: [] }
}
async function handleDelete(id) {
  ElMessageBox.confirm("是否删除数据项?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    async () => {
      let res = await API.deleteByIds(id)
      if (res) {
        ElMessage({
          message: '删除成功',
          type: 'success',
        })
        getList()
      }
    }
  );
}

// 图片
const addImg = ref(null)
function addimgEvent() {
  (addImg.value)?.show()
}
function successEvent(d) {
  datas.DialogForm['imgUrl'].push(d[0].url);
}
function deleteEvent(d) {
  let index = datas.DialogForm.imgUrl.findIndex((ele) => ele == d);
  datas.DialogForm.imgUrl.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;
}

// 图片库样式
.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;
}

.showStyle:hover>.buttonstyle {
  display: block;
}

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