<template>
  <a-card :bordered="false">
    <a-tabs v-model="tabPosTypeId" @change="tabPosTypeChange">
      <a-tab-pane
        :key="item.id"
        :tab="item.name"
        v-for="item in posTypeList"
      ></a-tab-pane>
    </a-tabs>
    <div style="display: flex; min-height: 500px">
      <a-card style="width: 50%">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">
              <a-col :span="8">
                <a-form-item label="">
                  <j-input
                    placeholder="取餐号"
                    v-model="queryParam.tableNo"
                  ></j-input>
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item label="">
                  <a-range-picker
                    format="YYYY-MM-DD"
                    :placeholder="['开始日期', '结束日期']"
                    @change="onChange"
                    v-model="datetime"
                  />
                </a-form-item>
              </a-col>
              <a-col :span="4">
                <span>
                  <a-button type="primary" @click="searchQuery" icon="search"
                    >查询</a-button
                  >
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <!-- 查询区域-END -->

        <!-- table区域-begin -->
        <div>
          <a-table
            ref="table"
            size="middle"
            :scroll="{ x: true }"
            bordered
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            :customRow="rowClick"
            class="j-table-force-nowrap"
            @change="handleTableChange"
          >
            <template slot="avatar" slot-scope="text">
              <a-avatar v-if="text" :src="text" />
              <a-avatar v-else icon="user" />
            </template>
            <template slot="imgSlot" slot-scope="text, record">
              <span v-if="!text" style="font-size: 12px; font-style: italic"
                >无图片</span
              >
              <img
                v-else
                :src="getImgView(text)"
                :preview="record.id"
                height="25px"
                alt=""
                style="max-width: 80px; font-size: 12px; font-style: italic"
              />
            </template>
            <template slot="fileSlot" slot-scope="text">
              <span v-if="!text" style="font-size: 12px; font-style: italic"
                >无文件</span
              >
              <a-button
                v-else
                :ghost="true"
                type="primary"
                icon="download"
                size="small"
                @click="downloadFile(text)"
              >
                下载
              </a-button>
            </template>

            <span slot="action" slot-scope="text, record">
              <a @click="handleEdit(record)">编辑</a>

              <a-divider type="vertical" />
              <a-dropdown>
                <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a @click="handleDetail(record)">详情</a>
                  </a-menu-item>
                  <a-menu-item>
                    <a-popconfirm
                      title="确定删除吗?"
                      @confirm="() => handleDelete(record.id)"
                    >
                      <a>删除</a>
                    </a-popconfirm>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
          </a-table>
        </div>
      </a-card>
      <a-card style="width: 50%">
        <p>订单详情</p>
        <a-table
          ref="table"
          size="middle"
          :scroll="{ y: 300 }"
          bordered
          rowKey="id"
          :columns="columns2"
          :dataSource="orderGoodsDetailList"
          :loading="loading"
        >
          <template slot="avatar" slot-scope="text">
            <a-avatar v-if="text" :src="text" />
            <a-avatar v-else icon="user" />
          </template>
          <template slot="imgSlot" slot-scope="text, record">
            <span v-if="!text" style="font-size: 12px; font-style: italic"
              >无图片</span
            >
            <img
              v-else
              :src="getImgView(text)"
              :preview="record.id"
              height="25px"
              alt=""
              style="max-width: 80px; font-size: 12px; font-style: italic"
            />
          </template>
          <template slot="fileSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px; font-style: italic"
              >无文件</span
            >
            <a-button
              v-else
              :ghost="true"
              type="primary"
              icon="download"
              size="small"
              @click="downloadFile(text)"
            >
              下载
            </a-button>
          </template>

          <span slot="action" slot-scope="text, record">
            <a @click="handleEdit(record)">编辑</a>

            <a-divider type="vertical" />
            <a-dropdown>
              <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
              <a-menu slot="overlay">
                <a-menu-item>
                  <a @click="handleDetail(record)">详情</a>
                </a-menu-item>
                <a-menu-item>
                  <a-popconfirm
                    title="确定删除吗?"
                    @confirm="() => handleDelete(record.id)"
                  >
                    <a>删除</a>
                  </a-popconfirm>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </span>
        </a-table>
        <a-row :gutter="[16, 16]" v-if="selectOrderInfo && selectOrderInfo.id">
          <a-col :span="8">
            <div>桌台/取餐号：{{ selectOrderInfo.tableNo }}</div>
          </a-col>
          <a-col :span="8">
            <div>数量合计：{{ sum }}</div>
          </a-col>
          <a-col :span="8">
            <div>消费金额：{{ selectOrderInfo.consumeMoney.toFixed(2) }}</div>
          </a-col>
          <a-col :span="8">
            <div>优惠金额：{{ selectOrderInfo.couponMoney.toFixed(2) }}</div>
          </a-col>
          <a-col :span="8">
            <div>结账优惠：{{ selectOrderInfo.couponMoney.toFixed(2) }}</div>
          </a-col>
          <a-col :span="8">
            <div>
              订单状态：{{ selectOrderInfo.stats === 0 ? "未结账" : "已结账" }}
            </div>
          </a-col>
          <a-col :span="8">
            <div>应收金额：{{ selectOrderInfo.money.toFixed(2) }}</div>
          </a-col>
          <a-col :span="8">
            <div>支付信息：{{ selectOrderInfo.payInfo }}</div>
          </a-col>
          <a-col :span="8">
            <div>下单时间：{{ selectOrderInfo.createTime }}</div>
          </a-col>
          <a-col :span="8">
            <div>结账时间：{{ selectOrderInfo.settleTime }}</div>
          </a-col>
        </a-row>
        <div>
          <template v-if="selectOrderInfo && selectOrderInfo.status === 0">
            <a-button type="danger" @click="handleDelete">删除</a-button>
            <a-button
              type="danger"
              @click="handlePayment"
              style="margin-left: 15px"
              >结账</a-button
            ></template
          >
          <template v-if="selectOrderInfo && selectOrderInfo.status === 1">
            <a-button type="danger" @click="handleCancelSettle"
              >撤销</a-button
            ></template
          >
        </div>
      </a-card>
    </div>
    <payment-modal ref="modalPaymentForm" @ok="modalFormOk"></payment-modal>
  </a-card>
</template>

<script>
import "@/assets/less/TableExpand.less";
import { mixinDevice } from "@/utils/mixin";
import { JeecgListMixin } from "@/mixins/JeecgListMixin";
import { httpAction, getAction, deleteAction } from "@/api/manage";
import PaymentModal from "./modules/PaymentModal.vue";
export default {
  name: "BusMemberCardList",
  mixins: [JeecgListMixin, mixinDevice],
  props: {
    couponsId: {
      type: String,
      default: "",
    },
    couponsType: {
      type: Number,
      default: 1,
    },
  },
  components: { PaymentModal },
  data() {
    const hotelInfo = JSON.parse(localStorage.getItem("storeInfo"));
    return {
      datetime: [],
      hotelId: "",
      num: 1,
      description: "PickingGoodsOrder",
      // 表头
      columns: [
        {
          title: "单据号",
          align: "center",
          dataIndex: "code",
        },
        {
          title: "pos类型",
          align: "center",
          dataIndex: "posTypeName",
        },
        {
          title: "桌号/取餐号",
          align: "center",
          dataIndex: "tableNo",
        },
        {
          title: "金额",
          align: "center",
          dataIndex: "consumeMoney",
        },
        {
          title: "结账状态",
          align: "center",
          dataIndex: "status",
          customRender(text) {
            return text == 0 ? "未结账" : "已结账";
          },
        },
      ],
      columns2: [
        {
          title: "商品名称",
          align: "center",
          dataIndex: "goodsName",
        },
        {
          title: "数量",
          align: "center",
          dataIndex: "num",
        },
        {
          title: "原价",
          align: "center",
          dataIndex: "money",
          customRender(text, record) {
            return text.toFixed(2);
          },
        },
        {
          title: "优惠价",
          align: "center",
          dataIndex: "couponMoney",
          customRender(text, record) {
            return text.toFixed(2);
          },
        },
        {
          title: "优惠金额",
          align: "center",
          dataIndex: "id",
          customRender(text, record) {
            return record.money - record.couponMoney;
          },
        },
        {
          title: "小计",
          align: "center",
          dataIndex: "payMoney",
          customRender(text, record) {
            return (text * record.num).toFixed(2);
          },
        },
      ],
      url: {
        list: "/pos/posOrderGoods/list",
        delete: "/business/busMemberCard/delete",
        deleteBatch: "/business/busMemberCard/deleteBatch",
        exportXlsUrl: "/business/busMemberCard/exportXls",
        importExcelUrl: "business/busMemberCard/importExcel",
      },
      dictOptions: {},
      superFieldList: [],
      orderGoodsDetailList: [],
      selectOrderInfo: {},
      posTypeList: [],
      tabPosTypeId: "",
    };
  },
  created() {
    getAction("/pos/posType/list", { pageNo: 1, pageSize: 99 }).then((res) => {
      if (res.success) {
        this.posTypeList = res.result.records;
        if (this.posTypeList && this.posTypeList.length > 0) {
          this.tabPosTypeId = this.posTypeList[0].id;
        }
      }
    });
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG["domianURL"]}/${this.url.importExcelUrl}`;
    },
    sum() {
      return this.orderGoodsDetailList.reduce(function (total, item) {
        return total + item.num;
      }, 0);
    },
  },
  methods: {
    modalFormOk() {
      this.selectOrderInfo = {};
      this.orderGoodsDetailList = [];
      this.loadData();
    },
    handleDelete() {
      var that = this;
      deleteAction(
        "/pos/posOrderGoods/deleteOrder?code=" + this.selectOrderInfo.code
      ).then((res) => {
        if (res.success) {
          that.$message.success(res.message);
          that.modalFormOk();
        } else {
          that.$message.warning(res.message);
        }
      });
    },
    handleCancelSettle() {
      var that = this;
      httpAction(
        "/pos/posOrderGoods/cancelSettle?code=" + this.selectOrderInfo.code,
        {},
        "post"
      ).then((res) => {
        if (res.success) {
          that.$message.success(res.message);
          that.modalFormOk();
        } else {
          that.$message.warning(res.message);
        }
      });
    },
    handlePayment() {
      var amount = this.orderGoodsDetailList.reduce(function (total, item) {
        return total + item.payMoney * item.num;
      }, 0);
      this.$refs.modalPaymentForm.edit({
        billAmount: amount,
        deposit: 0,
        roomFee: amount,
        subjectType: 5,
        feeType: 2,
        preferentialType: 1,
        couponFirstAmount: 0,
        discount: 9,
        orderCode: this.selectOrderInfo.code,
      });
      this.$refs.modalPaymentForm.title = "POS结账";
      this.$refs.modalPaymentForm.disableSubmit = false;
    },
    tabPosTypeChange(e) {
      this.queryParam.posType = e;
      this.loadData(1);
    },
    submitForm() {
      if (this.orderGoodsDetailList.length <= 0) {
        this.$message.warning("请先选择订单");
        return;
      }
      this.$emit("ok", {
        orderInfo: this.selectOrderInfo,
        orderDetailList: this.orderGoodsDetailList,
      });
    },
    rowClick(row) {
      return {
        on: {
          click: (e) => {
            console.log(row);
            this.selectOrderInfo = row;
            getAction("/pos/posOrderGoodsDetail/list", {
              pageNo: 1,
              pageSize: 99,
              orderId: row.code,
            }).then((res) => {
              if (res.success) {
                this.orderGoodsDetailList = res.result.records;
              }
            });
          },
        },
      };
    },
    onChange(e, dateString) {
      this.queryParam.startTime = dateString[0];
      this.queryParam.endTime = dateString[1];
    },
    handleAdd() {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning("请选择一条记录！");
        return;
      }
      if (this.num <= 0) {
        this.$message.warning("发放数量不能小于0！");
        return;
      }
      var that = this;
      that.confirmLoading = true;
      var url = "/business/busMarketCouponsUsed/gainCouponsUsedToMember";
      if (this.couponsType === 2) {
        url = "/business/busMarketCouponsCashUsed/gainCouponsCashUsedToMember";
      }
      httpAction(
        url,
        {
          couponsId: this.couponsId,
          num: this.num,
          memberIds: this.selectedRowKeys,
        },
        "post"
      )
        .then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.$emit("ok");
          } else {
            that.$message.warning(res.message);
          }
        })
        .finally(() => {
          that.confirmLoading = false;
        });
    },
  },
};
</script>
<style scoped>
@import "~@assets/less/common.less";
/deep/ .ant-input-search-button {
  background-color: #ff4d4f;
  border-color: #ff4d4f;
}
/deep/ .ant-input-search-button[disabled]:hover {
  opacity: 0.7;
  background-color: #ff4d4f;
  border-color: #ff4d4f;
}
/deep/ .ant-input-search-button[disabled] {
  opacity: 0.7;
  color: #ffffff;
}
</style>