<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="品牌方id" prop="breweryId">
        <el-input
          v-model="queryParams.breweryId"
          placeholder="请输入品牌方id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次id" prop="batchId">
        <el-input
          v-model="queryParams.batchId"
          placeholder="请输入批次id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="啤酒id" prop="beerId">
        <el-input
          v-model="queryParams.beerId"
          placeholder="请输入啤酒id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="条码ID" prop="skuSn">
        <el-input
          v-model="queryParams.skuSn"
          placeholder="请输入sku编号"
          clearable
          style="width: 244px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="二维码链接" prop="qrcodeUrl">
        <el-input
          v-model="queryParams.qrcodeUrl"
          placeholder="请输入二维码链接"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核销时间" prop="verifyTime">
        <el-date-picker
          clearable
          v-model="queryParams.verifyTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择核销时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="被扫次数" prop="scanTimes">
        <el-input
          v-model="queryParams.scanTimes"
          placeholder="请输入被扫次数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核销城市" prop="verifyCity">
        <el-input
          v-model="queryParams.verifyCity"
          placeholder="请输入核销城市"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核销ip" prop="verifyIp">
        <el-input
          v-model="queryParams.verifyIp"
          placeholder="请输入核销ip"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="核销状态" prop="qrcodeStatus">
        <el-select
          v-model="queryParams.qrcodeStatus"
          placeholder="请选择状态"
          style="width: 244px"
        >
          <el-option label="全部" value=""></el-option>
          <el-option label="已核销" value="1"></el-option>
          <el-option label="未核销" value="0"></el-option>
          <el-option label="已失效" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['brewery:qrcode:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['brewery:qrcode:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['brewery:qrcode:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['brewery:qrcode:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
   -->
    <el-table
      v-loading="loading"
      :data="qrcodeList"
      class="zd-table"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column
        label="条码ID"
        align="center"
        prop="skuSn"
        show-overflow-tooltip
      />
      <el-table-column
        label="酒款名称"
        align="center"
        prop="beerName"
        show-overflow-tooltip
      />
      <el-table-column label="生产日期" align="center" prop="productionDate" />
      <!-- <el-table-column label="啤酒id" align="center" prop="beerId" /> -->
      <!-- <el-table-column label="sku编号" align="center" prop="skuSn" /> -->
      <!-- <el-table-column
        label="二维码链接"
        align="center"
        prop="qrcodeUrl"
        show-overflow-tooltip
      >
        <template #default="scope">
          <el-image
            style="width: 40px; height: 40px"
            :src="scope.row.qrcodeUrl"
            :zoom-rate="1.2"
            :max-scale="7"
            :min-scale="0.2"
            :preview-src-list="[scope.row.qrcodeUrl]"
            :initial-index="4"
            fit="cover"
            title="点击查看大图"
            preview-teleported
          />
        </template>
      </el-table-column> -->
      <el-table-column label="条码扫码次数" align="center" prop="scanTimes" />
      <el-table-column
        label="核销状态"
        align="center"
        prop="qrcodeStatus"
        width="80px"
      >
        <template #default="scope">
          <el-tag type="success" v-if="scope.row.qrcodeStatus === 1"
            >已核销</el-tag
          >
          <el-tag type="warning" v-else-if="scope.row.qrcodeStatus === 0"
            >未核销</el-tag
          >
          <el-tag type="info" v-else-if="scope.row.qrcodeStatus === 2"
            >已失效</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        label="核销时间"
        align="center"
        prop="verifyTime"
        width="110"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.verifyTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column label="门店ID" align="center" prop="barCode" />
      <el-table-column
        label="核销城市"
        align="center"
        prop="verifyCity"
        show-overflow-tooltip
      />
      <el-table-column
        label="核销啤酒币数量"
        align="center"
        prop="verifyIconAmount"
      />
      <el-table-column label="核销活动ID" align="center" prop="activityId" />
      <!-- <el-table-column label="核销ip" align="center" prop="verifyIp" /> -->
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        fixed="right"
        width="150"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleInfo(scope.row)"
            v-hasPermi="['brewery:qrcode:edit']"
            >查看详情</el-button
          >
          <!-- <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['brewery:qrcode:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改酒品二维码对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="qrcodeRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="品牌方id" prop="breweryId">
          <el-input v-model="form.breweryId" placeholder="请输入品牌方id" />
        </el-form-item>
        <el-form-item label="批次id" prop="batchId">
          <el-input v-model="form.batchId" placeholder="请输入批次id" />
        </el-form-item>
        <el-form-item label="啤酒id" prop="beerId">
          <el-input v-model="form.beerId" placeholder="请输入啤酒id" />
        </el-form-item>
        <!-- <el-form-item label="sku编号" prop="skuSn">
            <el-input v-model="form.skuSn" placeholder="请输入sku编号" />
          </el-form-item> -->
        <el-form-item label="二维码" prop="qrcodeUrl">
          <el-input v-model="form.qrcodeUrl" placeholder="请输入二维码链接" />
        </el-form-item>
        <el-form-item label="核销时间" prop="verifyTime">
          <el-date-picker
            clearable
            v-model="form.verifyTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择核销时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="被扫次数" prop="scanTimes">
          <el-input v-model="form.scanTimes" placeholder="请输入被扫次数" />
        </el-form-item>
        <el-form-item label="核销城市" prop="verifyCity">
          <el-input v-model="form.verifyCity" placeholder="请输入核销城市" />
        </el-form-item>
        <el-form-item label="核销ip" prop="verifyIp">
          <el-input v-model="form.verifyIp" placeholder="请输入核销ip" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Qrcode">
import {
  listQrcode,
  getQrcode,
  delQrcode,
  addQrcode,
  updateQrcode,
} from "@/api/brewery/qrcode";
import { allBeers } from "@/api/brewery/beer";
const { proxy } = getCurrentInstance();
const router = useRouter();
const qrcodeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const allBeersList = ref([]); // 所有啤酒列表
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    breweryId: null,
    batchId: null,
    beerId: null,
    skuSn: null,
    qrcodeUrl: null,
    qrcodeStatus: null,
    verifyTime: null,
    scanTimes: null,
    verifyCity: null,
    verifyIp: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询酒品二维码列表 */
function getList() {
  loading.value = true;
  listQrcode(queryParams.value).then((response) => {
    qrcodeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    breweryId: null,
    batchId: null,
    beerId: null,
    skuSn: null,
    qrcodeUrl: null,
    qrcodeStatus: null,
    verifyTime: null,
    scanTimes: null,
    verifyCity: null,
    verifyIp: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
  };
  proxy.resetForm("qrcodeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加酒品二维码";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getQrcode(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改酒品二维码";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["qrcodeRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateQrcode(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addQrcode(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除酒品二维码编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delQrcode(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "brewery/qrcode/export",
    {
      ...queryParams.value,
    },
    `qrcode_${new Date().getTime()}.xlsx`
  );
}
// 获取所有啤酒列表
function getAllBeersFun() {
  allBeers().then((response) => {
    allBeersList.value = response.data;
  });
}
function handleInfo(row) {
  router.push({
    path: "/product/qrcodeInfo",
    query: {
      id: row.id,
    },
  });
}
// getAllBeersFun();
getList();
</script>
