<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.ordCode"
          placeholder="订单号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="dataForm.phone"
          placeholder="手机号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          value-format="yyyy-MM-dd HH:mm:ss"
          v-model="dataForm.dateTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="getExcel()">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      row-key="orderCode"
      :expand-row-keys="expandRowKeys"
      style="width: 100%"
    >
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-row>
              <el-col :span="12">
                <el-form-item label="订单金额">
                  <span>{{ props.row.payPrice }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="采购金额">
                  <span>{{ props.row.buyOrderPrice }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="酒店渠道">
                  <span>{{ props.row.plantfrom }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="到店时间">
                  <span>{{ props.row.earliestTime }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="采购渠道">
                  <span>{{ props.row.chanelCode }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="订单编号">
                  <span>{{ props.row.orderCode }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="联系电话">
                  <span>{{ props.row.phone }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="订单备注">
                  <div
                    :style="{
                      color:
                        props.row.note ==
                          '注意！直连订单，毛利小于0，取消直连，请人工处理。' ||
                        props.row.note == '注意！人工订单，毛利小于0。'
                          ? '#FF0000'
                          : '#000'
                    }"
                  >
                    {{ props.row.note }}
                  </div>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column
        prop="stName"
        header-align="center"
        align="center"
        label="所属店铺"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="date"
        header-align="center"
        align="center"
        label="预定时间"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="orderType"
        header-align="center"
        align="center"
        label="订单类型"
        :formatter="orderType"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="buySource"
        header-align="center"
        align="center"
        label="采购渠道"
        :formatter="statusSources"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="hotelName"
        header-align="center"
        align="center"
        label="酒店名称"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        :formatter="dateStr"
        header-align="center"
        align="center"
        label="入离时间"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="联系人"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="orderStatus"
        header-align="center"
        align="center"
        label="订单状态"
        :formatter="statusOrder"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="buyStatus"
        header-align="center"
        align="center"
        label="采购状态"
        :formatter="statusBuy"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="username"
        header-align="center"
        align="center"
        label="跟单人"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        label="跟单"
      >
        <template slot-scope="scope">
          <el-button
            :disabled="
              (scope.row.dealOrderPeo == null &&
                scope.row.orderStatus == 2 &&
                scope.row.orderType == 0) ||
              (scope.row.dealOrderPeo == null && scope.row.buyStatus == -1)
                ? false
                : true
            "
            v-if="isAuth('ord:order:lock')"
            size="small"
            type="primary"
            round
            @click="lockOrder(scope.row.id)"
            >跟单</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        label="下单"
      >
        <template slot-scope="scope" :v-model="me">
          <el-button
            :disabled="
              (scope.row.buyStatus == 0 ||
                scope.row.buyStatus == -1 ||
                scope.row.buyStatus == 1) &&
              me == scope.row.dealOrderPeo &&
              scope.row.orderStatus == 2
                ? false
                : true
            "
            v-if="isAuth('ord:order:deal')"
            type="primary"
            round
            size="small"
            @click="dealWithOrder(scope.row)"
            >下单</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        label="短信"
      >
        <template slot-scope="scope">
          <el-button
            :disabled="scope.row.isMsg == 1 ? true : false"
            v-if="isAuth('ord:order:msg')"
            size="small"
            type="primary"
            round
            @click="sendMsg(scope.row.id)"
            >发送</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 入住人详情 -->
    <!-- <peo-info v-if="infoVisible" ref="peoInfo"></peo-info> -->
    <!--弹窗，处理订单-->
    <deal-with v-if="dealWithVisible" ref="dealWith"> </deal-with>
    <!-- 发票详情 -->
    <!-- <invo v-if="invoVisible" ref="invo"> </invo> -->
    <!-- 备注 -->
    <!-- <note v-if="noteVisible" ref="note"></note> -->
  </div>
</template>

<script>
// import PeoInfo from "./ordorder-peo-info"
import DealWith from "./ordorder-dealwith";
import axios from "axios";
// import Invo from "./ordorder-invo"
// import Note from "./ordorder-note"
export default {
  data() {
    return {
      dataForm: {
        ordCode: "",
        dateTime: null,
        phone: ""
      },
      me: 0,
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      peoInfoVisible: false,
      dealWithVisible: false,
      infoVisible: false,
      invoVisible: false,
      noteVisible: false,
      expandRowKeys: []
    };
  },
  components: {
    // "peo-info": PeoInfo,
    "deal-with": DealWith
    // invo: Invo,
    // note: Note
  },
  activated() {
    this.getDataList();
  },
  methods: {
    getExpandRowKeys(dataList) {
      let expandRowKeys = [];
      dataList.forEach(e => {
        if (
          e.note == "注意！直连订单，毛利小于0，取消直连，请人工处理。" ||
          e.note == "注意！人工订单，毛利小于0。"
        ) {
          expandRowKeys.push(e.orderCode);
        }
      });
      this.expandRowKeys = expandRowKeys;
    },
    // 导出订单excel
    getExcel() {
      this.$http({
        responseType: "blob",
        method: "post",
        url: this.$http.adornUrl("/ord/order/excel"),
        // 请求参数，可以为空
        data: {
          dateTime: this.dataForm.dateTime
        }
      }).then(({ data }) => {
        let objectUrl = data;
        const blob = new Blob([data], ["application/vnd.ms-excel"]);
        objectUrl = URL.createObjectURL(blob);
        const a = document.createElement("a");
        a.href = objectUrl;
        //这里可以重新定义文件名，以前端为准
        a.download = "order.xlsx";
        a.click();
        a.remove();
        URL.revokeObjectURL(objectUrl);
      });
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/ord/order/list"),
        method: "post",
        data: this.$http.adornData({
          page: this.pageIndex,
          limit: this.pageSize,
          ordCode: this.dataForm.ordCode,
          dateTime: this.dataForm.dateTime,
          phone: this.dataForm.phone
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
          this.me = data.me;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
        this.$nextTick(() => {
          this.getExpandRowKeys(this.dataList);
        });
      });
    },
    // 采购渠道 1,携程 2，美团 3，人工线下采购 4，其他
    buySource(data) {
      let status = data.buySource;
      return status === 1
        ? "携程"
        : status === 2
        ? "美团"
        : status === 3
        ? "人工线下采购"
        : status === 4
        ? "其他"
        : "";
    },
    // 入住时间段
    dateStr(data) {
      return data.checkInDate + "~" + data.checkOutDate;
    },
    // 订单类型
    orderType(data) {
      let status = data.orderType;
      return status === 0 ? "人工订单" : status === 1 ? "直连订单" : "其他";
    },
    statusSources(data) {
      let status = data.buySource;
      return status === 2
        ? "美团"
        : status === 1
        ? "携程"
        : status === 3
        ? "线下"
        : "";
    },
    // 订单状态判断
    statusOrder(data) {
      let status = data.orderStatus;
      return status === 1
        ? "未支付"
        : status === 2
        ? "已支付"
        : status === 3
        ? "已发货"
        : status === 4
        ? "已入住"
        : status === -1
        ? "申请退款"
        : status === -2
        ? "已退款"
        : status === -3
        ? "关单"
        : status === -4
        ? "已入住"
        : status === -5
        ? "申请取消订单"
        : "其他";
    },
    // 采购状态判断
    // 0未采购，1已受理（已提交）2已采购 -1 采购失败 -2 已退单
    statusBuy(data) {
      let status = data.buyStatus;
      return status === 0
        ? "未采购"
        : status === 1
        ? "已受理"
        : status === 2
        ? "采购中"
        : status === 3
        ? "已采购"
        : status === -1
        ? "采购失败"
        : status === -2
        ? "退款中"
        : status === -3
        ? "已退款"
        : status === -4
        ? "退款失败"
        : status === -5
        ? "订单取消中"
        : status === -6
        ? "已取消"
        : status === -7
        ? "取消失败"
        : status === -8
        ? "已入住"
        : "其他";
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 入住人详情页
    infoHandle(row) {
      this.infoVisible = true;
      this.$nextTick(() => {
        this.$refs.peoInfo.init(row.orderCode);
      });
    },
    // 备注页
    noteHandle(id) {
      this.noteVisible = true;
      this.$nextTick(() => {
        this.$refs.note.init(id);
      });
    },
    // 处理订单
    dealWithOrder(val) {
      this.$http({
        url: this.$http.adornUrl("/ord/order/deal"),
        method: "get",
        params: this.$http.adornParams({
          dealOrderPeo: val.dealOrderPeo,
          id: val.id,
          buyStatus: val.buyStatus,
          orderStatus: val.orderStatus
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dealWithVisible = true;
          this.$nextTick(() => {
            this.$refs.dealWith.init(val.id);
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    // 价格更新
    updateRates(val) {
      this.$confirm(`确定进行价格更新操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/ord/order/update"),
          method: "get",
          params: this.$http.adornParams({
            hotelCode: val.hotelCode
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.getDataList();
            this.$message({
              message: data.msg,
              type: "success",
              duration: 1500
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    // 退款
    sendMsg(id) {
      this.$confirm(`确定发送退款短信操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/ord/order/sendMessage"),
          method: "get",
          params: this.$http.adornParams({
            id: id
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.getDataList();
            this.$message({
              message: data.msg,
              type: "success",
              duration: 1500
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    // 跟单
    lockOrder(id) {
      this.$http({
        url: this.$http.adornUrl("/ord/order/lock"),
        method: "get",
        params: this.$http.adornParams({
          id: id
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1000,
            onClose: () => {
              this.getDataList();
            }
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    // 发票信息
    invo(row) {
      this.invoVisible = true;
      this.$nextTick(() => {
        this.$refs.invo.init(row.orderCode);
      });
    }
  }
};
</script>
<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  /* width: 50%; */
}
</style>
