<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="订单编号" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="商品id" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入商品id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="用户id" prop="userId">
          <el-input
            v-model="queryParams.userId"
            placeholder="请输入用户id"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item> -->
      <!-- <el-form-item label="品牌啤酒币兑换数" prop="beerCoinNum">
        <el-input
          v-model="queryParams.beerCoinNum"
          placeholder="请输入品牌啤酒币兑换数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="平台啤酒币兑换数" prop="platformCoinNum">
        <el-input
          v-model="queryParams.platformCoinNum"
          placeholder="请输入平台啤酒币兑换数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="收货地址" prop="address">
          <el-input
            v-model="queryParams.address"
            placeholder="请输入收货地址"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item> -->
      <!-- <el-form-item label="联系人" prop="contactPersion">
        <el-input
          v-model="queryParams.contactPersion"
          placeholder="请输入联系人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="支付时间" prop="paidTime">
        <el-date-picker
          clearable
          v-model="queryParams.paidTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择支付时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="发货时间" prop="shippingTime">
        <el-date-picker
          clearable
          v-model="queryParams.shippingTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择发货时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="快递单号" prop="trackingNumber">
        <el-input
          v-model="queryParams.trackingNumber"
          placeholder="请输入快递单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </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:redeemOrder:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['brewery:redeemOrder:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['brewery:redeemOrder:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['brewery:redeemOrder:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="redeemOrderList"
      class="zd-table"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <!-- <el-table-column label="品牌方id" align="center" prop="breweryId" /> -->
      <el-table-column
        label="兑付单号"
        align="center"
        prop="orderSn"
        show-overflow-tooltip
      />
      <!-- <el-table-column label="商品id" align="center" prop="goodsId" /> -->
      <el-table-column
        label="支付时间"
        align="center"
        prop="paidTime"
        width="120"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.paidTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="商品名称"
        align="center"
        prop="goodsName"
        show-overflow-tooltip
      />
      <el-table-column label="兑付类型" align="center" prop="orderType" />
      <!-- <el-table-column label="商品封面图" align="center" prop="goodsCover">
        <template #default="scope">
          <el-image
            style="width: 40px; height: 40px"
            :src="scope.row.goodsCover"
            :zoom-rate="1.2"
            :max-scale="7"
            :min-scale="0.2"
            :preview-src-list="[scope.row.goodsCover]"
            :initial-index="4"
            fit="cover"
            title="点击查看大图"
            preview-teleported
          />
        </template>
      </el-table-column> -->

      <!-- <el-table-column label="用户id" align="center" prop="userId" /> -->
      <el-table-column label="门店" align="center" prop="barName" />
      <el-table-column
        label="品牌啤酒币兑换数"
        align="center"
        prop="beerCoinNum"
      />
      <el-table-column
        label="平台啤酒币兑换数"
        align="center"
        prop="platformCoinNum"
      />
      <el-table-column
        label="收货地址"
        align="center"
        prop="address"
        show-overflow-tooltip
      />
      <!-- <el-table-column label="联系人" align="center" prop="contactPersion" /> -->
      <!-- <el-table-column label="联系电话" align="center" prop="contactPhone" /> -->
      <el-table-column label="兑付状态" align="center" prop="orderStatus" />
      <el-table-column label="物流状态" align="center" prop="shippingStatus" />

      <el-table-column
        label="发货时间"
        align="center"
        prop="shippingTime"
        width="120"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.shippingTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="快递单号" align="center" prop="trackingNumber" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="150"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleDelivery(scope.row)"
            v-hasPermi="['brewery:redeemOrder:edit']"
            >发货</el-button
          >
          <el-button
            link
            type="primary"
            @click="handleRemark(scope.row)"
            v-hasPermi="['brewery:redeemOrder:edit']"
            >备注</el-button
          >
          <!-- <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['brewery:redeemOrder: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="redeemOrderRef"
        :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="订单编号" prop="orderSn">
          <el-input v-model="form.orderSn" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="商品id" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="品牌啤酒币兑换数" prop="beerCoinNum">
          <el-input
            v-model="form.beerCoinNum"
            placeholder="请输入品牌啤酒币兑换数"
          />
        </el-form-item>
        <el-form-item label="平台啤酒币兑换数" prop="platformCoinNum">
          <el-input
            v-model="form.platformCoinNum"
            placeholder="请输入平台啤酒币兑换数"
          />
        </el-form-item>
        <el-form-item label="收货地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入收货地址" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactPersion">
          <el-input v-model="form.contactPersion" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="支付时间" prop="paidTime">
          <el-date-picker
            clearable
            v-model="form.paidTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择支付时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发货时间" prop="shippingTime">
          <el-date-picker
            clearable
            v-model="form.shippingTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择发货时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="快递单号" prop="trackingNumber">
          <el-input
            v-model="form.trackingNumber"
            placeholder="请输入快递单号"
          />
        </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>

    <el-dialog
      title="订单发货"
      v-model="deliveryOpen"
      width="500px"
      append-to-body
    >
      <el-form
        ref="deliveryRef"
        :model="deliveryForm"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="商品信息">
          <div
            style="
              display: flex;
              justify-content: flex-start;
              align-items: center;
              gap: 20px;
              width: 100%;
            "
          >
            <el-image
              style="width: 60px; height: 60px"
              :src="currentRow.goodsCover"
              :zoom-rate="1.2"
              :max-scale="7"
              :min-scale="0.2"
              :preview-src-list="[currentRow.goodsCover]"
              :initial-index="4"
              fit="cover"
              title="点击查看大图"
              preview-teleported
            />
            <div>
              <div>{{ currentRow.goodsName }}</div>
              <!-- <div>{{ currentRow.goodsName }}</div> -->
            </div>
            <div>x {{ currentRow.redeemNum }}</div>
          </div>
        </el-form-item>
        <el-form-item label="收货信息">
          <div>联系方式：{{ currentRow.contactPhone }}</div>
          <div>收货地址：{{ currentRow.address }}</div>
        </el-form-item>
        <el-form-item label="发货方式" prop="dispatchMethod">
          <el-radio-group v-model="deliveryForm.dispatchMethod">
            <el-radio :value="0">自己配送</el-radio>
            <el-radio :value="1">快递</el-radio>
          </el-radio-group>
        </el-form-item>
        <template v-if="deliveryForm.dispatchMethod == 1">
          <el-form-item label="快递公司" prop="expressCompany">
            <el-input
              v-model="deliveryForm.expressCompany"
              placeholder="请输入快递公司"
            />
          </el-form-item>
          <el-form-item label="快递单号" prop="trackingNumber">
            <el-input
              v-model="deliveryForm.trackingNumber"
              placeholder="请输入快递单号"
            />
          </el-form-item>
        </template>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDelivery">确 定</el-button>
          <el-button @click="cancelDelivery">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog
      title="订单备注"
      v-model="openRemark"
      width="500px"
      append-to-body
    >
      <el-form
        ref="remarkRef"
        :model="remarkForm"
        :rules="rulesRemark"
        label-width="80px"
      >
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="remarkForm.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitRemark">确 定</el-button>
          <el-button @click="cancelRemark">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="RedeemOrder">
import {
  listRedeemOrder,
  getRedeemOrder,
  delRedeemOrder,
  addRedeemOrder,
  updateRedeemOrder,
  deliveryRedeemOrder,
  activityRedeemOrder,
  getOrderInfo,
} from "@/api/brewery/redeemOrder";

const { proxy } = getCurrentInstance();

const redeemOrderList = 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 data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    breweryId: null,
    orderSn: null,
    goodsId: null,
    goodsName: null,
    userId: null,
    beerCoinNum: null,
    platformCoinNum: null,
    address: null,
    contactPersion: null,
    contactPhone: null,
    orderStatus: null,
    shippingStatus: null,
    paidTime: null,
    shippingTime: null,
    trackingNumber: null,
  },
  rules: {
    breweryId: [
      { required: true, message: "品牌方id不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);
const deliveryOpen = ref(false); // 发货弹窗
const deliveryForm = ref({ dispatchMethod: 0 });
/** 查询兑换订单列表 */
function getList() {
  loading.value = true;
  activityRedeemOrder(queryParams.value).then((response) => {
    redeemOrderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

// 表单重置
function reset() {
  form.value = {
    id: null,
    breweryId: null,
    orderSn: null,
    goodsId: null,
    goodsName: null,
    userId: null,
    beerCoinNum: null,
    platformCoinNum: null,
    address: null,
    contactPersion: null,
    contactPhone: null,
    orderStatus: null,
    shippingStatus: null,
    paidTime: null,
    shippingTime: null,
    trackingNumber: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
  };
  proxy.resetForm("redeemOrderRef");
}

/** 搜索按钮操作 */
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;
  getOrderInfo(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改兑换订单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["redeemOrderRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateRedeemOrder(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRedeemOrder(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 delRedeemOrder(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "brewery/redeemOrder/export",
    {
      ...queryParams.value,
    },
    `redeemOrder_${new Date().getTime()}.xlsx`
  );
}
const currentRow = ref({});
/** 发货按钮操作 */
function handleDelivery(row) {
  currentRow.value = row;
  resetDelivery();
  const _id = row.id || ids.value;
  getOrderInfo(_id).then((response) => {
    deliveryForm.value = response.data;
    deliveryOpen.value = true;
  });
}
// 发货按钮
function submitDelivery() {
  proxy.$refs["deliveryRef"].validate((valid) => {
    if (valid) {
      deliveryForm.value.orderId = currentRow.value.id;
      deliveryRedeemOrder(deliveryForm.value).then((response) => {
        proxy.$modal.msgSuccess("发货成功");
        deliveryOpen.value = false;
        getList();
      });
    }
  });
}

// 取消按钮
function cancelDelivery() {
  deliveryOpen.value = false;
  resetDelivery();
}

function resetDelivery() {
  deliveryForm.value = { dispatchMethod: 0 };
  proxy.resetForm("deliveryRef");
}

const remarkForm = ref({});
const openRemark = ref(false); // 备注弹窗
const rulesRemark = ref({
  remark: [{ required: true, message: "备注不能为空", trigger: "blur" }],
});
// 备注

function handleRemark(row) {
  const _id = row.id || ids.value;
  currentRow.value = row;
  // getRedeemOrder(_id).then((response) => {
  openRemark.value = true;
  // });
}
function submitRemark() {
  proxy.$refs["remarkRef"].validate((valid) => {
    if (valid) {
      remarkForm.value.id = currentRow.value.id;
      updateRedeemOrder(remarkForm.value).then((response) => {
        proxy.$modal.msgSuccess("备注成功");
        openRemark.value = false;
        getList();
      });
    }
  });
}

// 取消按钮
function cancelRemark() {
  openRemark.value = false;
}

getList();
</script>
