<!--
 * @Descripttion: (礼品申领表/gift_claim)
 * @Author: (admin)
 * @Date: (2025-07-30)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入联系方式" />
      </el-form-item>
      <el-form-item label="工作单位" prop="company">
        <el-input v-model="queryParams.company" placeholder="请输入工作单位" />
      </el-form-item>
      <el-form-item label="收货地址" prop="address">
        <el-input v-model="queryParams.address" placeholder="请输入收货地址" />
      </el-form-item>
      <el-form-item label="产品型号" prop="productModel">
        <el-input v-model="queryParams.productModel" placeholder="请输入产品型号" />
      </el-form-item>
      <el-form-item label="出品编号" prop="productSerialNumber">
        <el-input v-model="queryParams.productSerialNumber" placeholder="请输入出品编号" />
      </el-form-item>
      <el-form-item label="微信openId" prop="userWxOpenId">
        <el-input v-model="queryParams.userWxOpenId" placeholder="请输入微信openId" />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select clearable v-model="queryParams.status" placeholder="请选择审核状态">
          <el-option v-for="item in options.gift_request_claim" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
            <span class="fl">{{ item.dictLabel }}</span>
            <span class="fr" style="color: var(--el-text-color-secondary)">{{ item.dictValue }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['giftclaim:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="download" @click="handleExport" v-hasPermi="['giftclaim:export']">
          {{ $t('btn.export') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table
      :data="dataList"
      v-loading="loading"
      ref="table"
      border
      header-cell-class-name="el-table-header-cell"
      highlight-current-row
      @sort-change="sortChange">
      <el-table-column prop="id" label="序号" align="center" width="80">
        <template #default="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="id" label="id" align="center" width="70" v-if="columns.showColumn('id')"> </el-table-column>
      <el-table-column prop="userId" label="用户信息" align="center" width="300">
        <template #default="scope">
          <div class="user-info-container">
            <div class="user-info-item">
              <span class="info-label">openId：</span>
              <span class="info-value">{{ scope.row.userWxOpenId }}</span>
            </div>
            <div class="user-info-item">
              <span class="info-label">姓名：</span>
              <span class="info-value">{{ scope.row.name }}</span>
            </div>
            <div class="user-info-item">
              <span class="info-label">联系方式：</span>
              <span class="info-value">{{ scope.row.phone }}</span>
            </div>
            <div class="user-info-item">
              <span class="info-label">工作单位：</span>
              <span class="info-value">{{ scope.row.company }}</span>
            </div>
            <div class="user-info-item">
              <span class="info-label">收货地址：</span>
              <span class="info-value">{{ scope.row.address }}</span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="productModel" label="产品信息" align="center" width="400" v-if="columns.showColumn('productModel')">
        <template #default="scope">
          <div class="product-info-container">
            <div class="product-image">
              <ImagePreview style="width: 80px; height: 80px; border-radius: 8px" :src="scope.row.productImage"></ImagePreview>
            </div>
            <div class="product-details">
              <div class="product-info-item">
                <span class="info-label">产品型号：</span>
                <span class="info-value">{{ scope.row.productModel }}</span>
              </div>
              <div class="product-info-item">
                <span class="info-label">出品编号：</span>
                <span class="info-value">{{ scope.row.productSerialNumber }}</span>
              </div>
              <div class="product-info-item">
                <span class="info-label">出品年月：</span>
                <span class="info-value">{{ scope.row.productDate }}</span>
              </div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="geocodedAddress" label="位置信息" :show-overflow-tooltip="true" width="220">
        <template #default="scope">
          <div class="user-info-container">
            <div class="user-info-item">
              <span class="info-label" style="font-size: 12px; min-width: 50px">经度：</span>
              <span class="info-value" style="font-size: 12px">{{ scope.row.longitude }}</span>
            </div>
            <div class="user-info-item">
              <span class="info-label" style="font-size: 12px; min-width: 50px">经纬度：</span>
              <span class="info-value" style="font-size: 12px">{{ scope.row.latitude }}</span>
            </div>
            <div class="user-info-item">
              <span class="info-label" style="font-size: 12px; min-width: 50px">位置：</span>
              <div>
                <span class="info-value" style="font-size: 12px; white-space: break-spaces">{{ scope.row.geocodedAddress }}</span>
              </div>
            </div>
            <div class="user-info-item">
              <span class="info-label" style="font-size: 12px; min-width: 50px">ip</span>
              <span class="info-value" style="font-size: 12px">{{ scope.row.ip }}</span>
            </div>
            <div class="user-info-item">
              <span class="info-label" style="font-size: 12px; min-width: 50px">ip位置</span>
              <span class="info-value" style="font-size: 12px">{{ scope.row.ipAddress }}</span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="createdAt" label="时间信息" :show-overflow-tooltip="true" width="220">
        <template #default="scope">
          <div class="user-info-container">
            <div class="user-info-item">
              <span class="info-label" style="font-size: 12px; min-width: 50px">申请时间：</span>
              <span class="info-value" style="font-size: 12px">{{ scope.row.createdAt }}</span>
            </div>
            <div class="user-info-item">
              <span class="info-label" style="font-size: 12px; min-width: 50px">审核时间：</span>
              <span class="info-value" style="font-size: 12px">{{ scope.row.reviewAt }}</span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="审核状态" align="center" width="150">
        <template #default="scope">
          <div class="status-column">
            <div class="status-tag-wrapper">
              <dict-tag :options="options.gift_request_claim" :value="scope.row.status" />
            </div>
            <div class="audit-buttons" v-if="scope.row.status === 0">
              <el-button size="small" type="success" plain @click="handleAudit(scope.row, 1)" v-hasPermi="['giftclaim:audit']"> 通过 </el-button>
              <el-button size="small" type="danger" plain @click="handleAudit(scope.row, 2)" v-hasPermi="['giftclaim:audit']"> 拒绝 </el-button>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button
            type="success"
            size="small"
            icon="edit"
            title="编辑"
            v-hasPermi="['giftclaim:edit']"
            @click="handleUpdate(scope.row)"></el-button>
          <el-button
            type="danger"
            size="small"
            icon="delete"
            title="删除"
            v-hasPermi="['giftclaim:delete']"
            @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" :lock-scroll="false" v-model="open">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :lg="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="联系方式" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系方式" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="工作单位" prop="company">
              <el-input v-model="form.company" placeholder="请输入工作单位" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="收货地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入收货地址" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="产品型号" prop="productModel">
              <el-input v-model="form.productModel" placeholder="请输入产品型号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="出品编号" prop="productSerialNumber">
              <el-input v-model="form.productSerialNumber" placeholder="请输入出品编号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="出品年月" prop="productDate">
              <el-input v-model="form.productDate" placeholder="请输入出品年月" />
            </el-form-item>
          </el-col>

          <el-col :lg="24">
            <el-form-item label="出品图片" prop="productImage">
              <UploadImage v-model="form.productImage" :data="{ uploadType: 1 }" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="提交时间" prop="createdAt">
              <el-date-picker v-model="form.createdAt" type="datetime" placeholder="选择日期时间" value-format="YYYY-MM-DD HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="审核状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择审核状态">
                <el-option
                  v-for="item in options.gift_request_claim"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="parseInt(item.dictValue)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" :loading="state.submitLoading" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="giftclaim">
import { listGiftClaim, addGiftClaim, delGiftClaim, updateGiftClaim, getGiftClaim, updateGiftClaimStatus } from '@/api/business/giftclaim.js'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: 'Id',
  sortType: 'desc',
  name: undefined,
  phone: undefined,
  company: undefined,
  address: undefined,
  productModel: undefined,
  productSerialNumber: undefined,
  userWxOpenId: undefined,
  status: undefined
})
const columns = ref([
  { visible: true, align: 'center', type: '', prop: 'id', label: '序号' },
  { visible: true, align: 'center', type: '', prop: 'id', label: 'id' },
  { visible: true, align: 'center', type: '', prop: 'userId', label: '用户信息' },
  { visible: true, align: 'center', type: '', prop: 'productModel', label: '产品信息', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'geocodedAddress', label: '位置信息', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'createdAt', label: '申请时间', showOverflowTooltip: true },
  { visible: true, align: 'center', type: 'dict', prop: 'status', label: '审核状态', dictType: 'gift_request_claim', showOverflowTooltip: true }
  //{ visible: false, prop: 'actions', label: '操作', type: 'slot', width: '160' }
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])

var dictParams = ['gift_request_claim']

proxy.getDicts(dictParams).then((response) => {
  response.data.forEach((element) => {
    state.options[element.dictType] = element.list
  })
})

function getList() {
  loading.value = true
  listGiftClaim(queryParams).then((res) => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order
  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

// 审核按钮操作
async function handleAudit(row, status) {
  console.log(row, status)
  var id = row.id
  const res = await updateGiftClaimStatus(id, status)
  if (res.code == 200) {
    proxy.$modal.msgSuccess(status == 1 ? '审核成功' : '审核不通过')
    getList()
  }
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  submitLoading: false,
  form: {},
  rules: {
    userId: [{ required: true, message: '用户ID不能为空', trigger: 'blur', type: 'number' }],
    name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
    phone: [{ required: true, message: '联系方式不能为空', trigger: 'blur' }],
    address: [{ required: true, message: '收货地址不能为空', trigger: 'blur' }],
    productModel: [{ required: true, message: '产品型号不能为空', trigger: 'blur' }],
    productSerialNumber: [{ required: true, message: '出品编号不能为空', trigger: 'blur' }],
    productDate: [{ required: true, message: '出品年月不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '审核状态不能为空', trigger: 'change', type: 'number' }]
  },
  options: {
    // 审核状态 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
    gift_request_claim: []
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    id: null,
    userId: null,
    name: null,
    phone: null,
    company: null,
    address: null,
    productModel: null,
    productSerialNumber: null,
    productDate: null,
    productImage: null,
    createdAt: null,
    status: null
  }
  proxy.resetForm('formRef')
}

// 添加按钮操作
function handleAdd() {
  reset()
  open.value = true
  state.submitLoading = false
  title.value = '添加礼品申领表'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  state.submitLoading = false
  const id = row.id || ids.value
  getGiftClaim(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '修改礼品申领表'
      opertype.value = 2

      form.value = {
        ...data
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs['formRef'].validate((valid) => {
    if (valid) {
      state.submitLoading = true

      if (form.value.id != undefined && opertype.value === 2) {
        updateGiftClaim(form.value).then((res) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addGiftClaim(form.value)
          .then((res) => {
            proxy.$modal.msgSuccess('新增成功')
            open.value = false
            getList()
          })
          .finally(() => {
            setTimeout(() => {
              state.submitLoading = false
            }, 800)
          })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.id || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', '警告', {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: 'warning'
    })
    .then(function () {
      return delGiftClaim(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
}

// 导出按钮操作
function handleExport() {
  proxy
    .$confirm('是否确认导出礼品申领表数据项?', '警告', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    .then(async () => {
      await proxy.downFile('/business/GiftClaim/export', { ...queryParams })
    })
}

handleQuery()
</script>

<style scoped>
/* 用户信息样式 */
.user-info-container {
  padding: 8px;
  text-align: left;
}

.user-info-item {
  margin-bottom: 6px;
  display: flex;
  align-items: flex-start;
  line-height: 1.4;
}

.user-info-item:last-child {
  margin-bottom: 0;
}

/* 产品信息样式 */
.product-info-container {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 8px;
  text-align: left;
}

.product-image {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.product-details {
  flex: 1;
  min-width: 0;
}

.product-info-item {
  margin-bottom: 6px;
  display: flex;
  align-items: flex-start;
  line-height: 1.4;
}

.product-info-item:last-child {
  margin-bottom: 0;
}

/* 通用信息样式 */
.info-label {
  font-weight: 500;
  color: #606266;
  white-space: nowrap;
  margin-right: 4px;
  min-width: 70px;
}

.info-value {
  color: #303133;
  word-break: break-all;
  flex: 1;
}

/* 审核状态列样式 */
.status-column {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px; /* 标签和按钮之间的间距 */
  flex-wrap: wrap; /* 允许换行 */
  padding: 4px;
}

.status-tag-wrapper {
  flex-shrink: 0; /* 防止标签收缩 */
  margin-bottom: 4px;
}

.audit-buttons {
  display: flex;
  gap: 6px; /* 按钮之间的间距 */
  flex-wrap: wrap; /* 小屏幕时允许按钮换行 */
}

.audit-buttons .el-button {
  padding: 4px 8px; /* 紧凑的按钮内边距 */
  font-size: 12px; /* 稍小的字体 */
  border-radius: 4px;
  transition: all 0.2s ease;
}

.audit-buttons .el-button:hover {
  transform: translateY(-1px); /* 悬停时轻微上移效果 */
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

/* 响应式调整 */
@media (max-width: 768px) {
  .product-info-container {
    flex-direction: column;
    align-items: center;
    gap: 8px;
  }

  .product-image {
    align-self: center;
  }

  .user-info-container,
  .product-details {
    font-size: 12px;
  }

  .info-label {
    min-width: 60px;
    font-size: 12px;
  }

  .status-column {
    flex-direction: column; /* 小屏幕时垂直排列 */
    gap: 6px;
  }

  .audit-buttons {
    gap: 4px;
  }

  .audit-buttons .el-button {
    padding: 2px 6px;
    font-size: 11px;
  }

  /* 调整表格列宽度 */
  .el-table-column[prop='status'] {
    min-width: 140px;
  }
}
</style>
