<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="" style="width: 308px">
          <el-date-picker
            v-model="daterangeCreateTime"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </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="orderNo">
          <el-input
            v-model="queryParams.orderNo"
            placeholder="请输入订单号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="支付方式" prop="paymentWay">
          <el-select v-model="queryParams.paymentWay" placeholder="请选择支付方式" clearable>
            <el-option
              v-for="dict in order_payment_way"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否支付" prop="isPay">
          <el-select v-model="queryParams.isPay" placeholder="请选择是否支付" clearable>
            <el-option
              v-for="dict in sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
            <el-option
              v-for="dict in order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="交易编号" prop="transactionId">
          <el-input
            v-model="queryParams.transactionId"
            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="['order:order:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['order:order:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['order:order:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['order:order:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="订单号" align="center" prop="orderNo" />
        <el-table-column label="支付方式" align="center" prop="paymentWay">
          <template #default="scope">
            <dict-tag :options="order_payment_way" :value="scope.row.paymentWay"/>
          </template>
        </el-table-column>
        <el-table-column label="是否支付" align="center" prop="isPay">
          <template #default="scope">
            <dict-tag :options="sys_yes_no" :value="scope.row.isPay"/>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="order_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="市场价" align="center" prop="salesPrice" />
        <el-table-column label="支付价" align="center" prop="paymentPrice" />
        <el-table-column label="支付时间" align="center" prop="paymentTime" width="180" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['order:order:edit']">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['order:order: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="80%" append-to-body>
        <el-form ref="orderRef" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="" prop="delFlag">
            <el-radio-group v-model="form.delFlag">
              <el-radio
                v-for="dict in sys_del_flag"
                :key="dict.value"
                :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" 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="orderNo">
            <el-input v-model="form.orderNo" placeholder="请输入订单号" />
          </el-form-item>
          <el-form-item label="支付方式" prop="paymentWay">
            <el-radio-group v-model="form.paymentWay">
              <el-radio
                v-for="dict in order_payment_way"
                :key="dict.value"
                :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否支付" prop="isPay">
            <el-radio-group v-model="form.isPay">
              <el-radio
                v-for="dict in sys_yes_no"
                :key="dict.value"
                :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in order_status"
                :key="dict.value"
                :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="运费" prop="freightPrice">
            <el-input v-model="form.freightPrice" placeholder="请输入运费" />
          </el-form-item>
          <el-form-item label="市场价" prop="salesPrice">
            <el-input v-model="form.salesPrice" placeholder="请输入市场价" />
          </el-form-item>
          <el-form-item label="支付价" prop="paymentPrice">
            <el-input v-model="form.paymentPrice" placeholder="请输入支付价" />
          </el-form-item>
          <el-form-item label="支付时间" prop="paymentTime">
            <el-date-picker clearable
              v-model="form.paymentTime"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择支付时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="发货时间" prop="deliveryTime">
            <el-date-picker clearable
              v-model="form.deliveryTime"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择发货时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="收货时间" prop="receiverTime">
            <el-date-picker clearable
              v-model="form.receiverTime"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择收货时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="完成时间" prop="closingTime">
            <el-date-picker clearable
              v-model="form.closingTime"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择完成时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="用户留言" prop="userMessage">
            <el-input v-model="form.userMessage" placeholder="请输入用户留言" />
          </el-form-item>
          <el-form-item label="交易编号" prop="transactionId">
            <el-input v-model="form.transactionId" placeholder="请输入交易编号" />
          </el-form-item>
          <el-divider content-position="center">商品信息</el-divider>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" icon="Plus" @click="handleAddOrderItems">添加</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" icon="Delete" @click="handleDeleteOrderItems">删除</el-button>
            </el-col>
          </el-row>
          <el-table :data="orderItemsList" :row-class-name="rowOrderItemsIndex" @selection-change="handleOrderItemsSelectionChange" ref="orderItems">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="SPU_ID" prop="spuId" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.spuId" placeholder="请输入SPU_ID" />
            </template>
          </el-table-column>
          <el-table-column label="SPU名称 " prop="spuName" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.spuName" placeholder="请输入SPU名称 " />
            </template>
          </el-table-column>
          <el-table-column label="预览" prop="pic" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.pic" placeholder="请输入预览" />
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="num" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.num" placeholder="请输入数量" />
            </template>
          </el-table-column>
          <el-table-column label="售价" prop="salesPrice" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.salesPrice" placeholder="请输入售价" />
            </template>
          </el-table-column>
          <el-table-column label="运费" prop="freightPrice" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.freightPrice" placeholder="请输入运费" />
            </template>
          </el-table-column>
          <el-table-column label="支付价格" prop="paymentPrice" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.paymentPrice" placeholder="请输入支付价格" />
            </template>
          </el-table-column>
          <el-table-column label="退款状态" prop="status" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.status" placeholder="请选择退款状态">
                <el-option
                  v-for="dict in order_refund_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="是否退款" prop="isRefund" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.isRefund" placeholder="请输入是否退款" />
            </template>
          </el-table-column>
        </el-table>
        </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="Order">
  import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/order/order";
  
  const { proxy } = getCurrentInstance();
  const { order_status, order_payment_way, sys_del_flag, sys_yes_no } = proxy.useDict('order_status', 'order_payment_way', 'sys_del_flag', 'sys_yes_no');
  
  const orderList = ref([]);
  const orderItemsList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const checkedOrderItems = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");
  const daterangeCreateTime = ref([]);
  
  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      createTime: null,
      userId: null,
      orderNo: null,
      paymentWay: null,
      isPay: null,
      status: null,
      transactionId: null
    },
    rules: {
      createTime: [
        { required: true, message: "不能为空", trigger: "blur" }
      ],
      updateTime: [
        { required: true, message: "不能为空", trigger: "blur" }
      ],
      delFlag: [
        { required: true, message: "不能为空", trigger: "change" }
      ],
      userId: [
        { required: true, message: "用户ID不能为空", trigger: "blur" }
      ],
      orderNo: [
        { required: true, message: "订单号不能为空", trigger: "blur" }
      ],
      paymentWay: [
        { required: true, message: "支付方式不能为空", trigger: "change" }
      ],
      isPay: [
        { required: true, message: "是否支付不能为空", trigger: "change" }
      ],
      freightPrice: [
        { required: true, message: "运费不能为空", trigger: "blur" }
      ],
      salesPrice: [
        { required: true, message: "市场价不能为空", trigger: "blur" }
      ],
      paymentPrice: [
        { required: true, message: "支付价不能为空", trigger: "blur" }
      ],
    }
  });
  
  const { queryParams, form, rules } = toRefs(data);
  
  /** 查询订单列表 */
  function getList() {
    loading.value = true;
    queryParams.value.params = {};
    if (null != daterangeCreateTime && '' != daterangeCreateTime) {
      queryParams.value.params["beginCreateTime"] = daterangeCreateTime.value[0];
      queryParams.value.params["endCreateTime"] = daterangeCreateTime.value[1];
    }
    listOrder(queryParams.value).then(response => {
      orderList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }
  
  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }
  
  // 表单重置
  function reset() {
    form.value = {
      id: null,
      createBy: null,
      createTime: null,
      updateBy: null,
      updateTime: null,
      delFlag: null,
      remark: null,
      userId: null,
      orderNo: null,
      paymentWay: null,
      isPay: null,
      status: null,
      freightPrice: null,
      salesPrice: null,
      paymentPrice: null,
      paymentTime: null,
      deliveryTime: null,
      receiverTime: null,
      closingTime: null,
      userMessage: null,
      transactionId: null
    };
    orderItemsList.value = [];
    proxy.resetForm("orderRef");
  }
  
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }
  
  /** 重置按钮操作 */
  function resetQuery() {
    daterangeCreateTime.value = [];
    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
    getOrder(_id).then(response => {
      form.value = response.data;
      orderItemsList.value = response.data.orderItemsList;
      open.value = true;
      title.value = "修改订单";
    });
  }
  
  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["orderRef"].validate(valid => {
      if (valid) {
        form.value.orderItemsList = orderItemsList.value;
        if (form.value.id != null) {
          updateOrder(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          });
        } else {
          addOrder(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 delOrder(_ids);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }
  
  /** 商品序号 */
  function rowOrderItemsIndex({ row, rowIndex }) {
    row.index = rowIndex + 1;
  }
  
  /** 商品添加按钮操作 */
  function handleAddOrderItems() {
    let obj = {};
    obj.remark = "";
    obj.spuId = "";
    obj.spuName = "";
    obj.pic = "";
    obj.num = "";
    obj.salesPrice = "";
    obj.freightPrice = "";
    obj.paymentPrice = "";
    obj.status = "";
    obj.isRefund = "";
    orderItemsList.value.push(obj);
  }
  
  /** 商品删除按钮操作 */
  function handleDeleteOrderItems() {
    if (checkedOrderItems.value.length == 0) {
      proxy.$modal.msgError("请先选择要删除的商品数据");
    } else {
      const orderItemss = orderItemsList.value;
      const checkedOrderItemss = checkedOrderItems.value;
      orderItemsList.value = orderItemss.filter(function(item) {
        return checkedOrderItemss.indexOf(item.index) == -1
      });
    }
  }
  
  /** 复选框选中数据 */
  function handleOrderItemsSelectionChange(selection) {
    checkedOrderItems.value = selection.map(item => item.index)
  }
  
  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('order/order/export', {
      ...queryParams.value
    }, `order_${new Date().getTime()}.xlsx`)
  }
  
  getList();
  </script>
  