<template>
  <div class="right">
    <div class="showTitle">
      <el-icon>
        <document />
      </el-icon>
      <h2>我的订单</h2>
    </div>
    <div id="topNav">
      <h2 @click="orderChoose($event, '全部')">全部</h2>
      <h2 @click="orderChoose($event, '未支付')">未支付</h2>
      <h2 @click="orderChoose($event, '服务中')">服务中</h2>
      <h2 @click="orderChoose($event, '已完成')">已完成</h2>
      <h2 @click="orderChoose($event, '已取消')">已取消</h2>
    </div>
    <hr id="hr1" />
    <div id="choseMsg" class="statusMsg" v-for="item in this.list" :key="item.id">
      <div id="topContent">
        <label>订单号:</label>
        <label class="orderId">{{ item.id }}</label>
        <label>创建时间:</label>
        <label class="orderId">{{ item.createTime }}</label>
        <img id="statusImg" src="../../img/status.png" />
        <label id="statusSpan">{{ item.status }}</label>
      </div>
      <hr class="hr tophr" />
      <span>家政人员:</span>
      <input disabled v-model="item.serverName" class="theIn" type="text" />

      <span class="rightSpan">分类范围:</span>
      <input disabled v-model="item.categoryName" class="theIn" type="text" />
      <br />
      <span>联系方式:</span>
      <input disabled v-model="item.serverPhone" class="theIn" type="text" />
      <span class="rightSpan">聘请类别:</span>
      <input disabled v-model="item.serverType" class="theIn" type="text" />
      <br />
      <span id="totalPrice" class="rightSpan">总价:</span>
      <input disabled v-model="item.totalPrice" class="theIn" type="text" />

      <span class="rightSpan">服务时间</span>
      <input disabled v-model="item.serverTime" class="theIn" type="text" />
      <br />
      <span id="note">备注：</span>
      <textarea disabled v-model="item.treatment"></textarea>
      <div v-if="item.status == this.unpaid">
        <hr class="hr" />
        <input class="firstBotton" type="button" @click="convertMsg($event, item)" value="去支付" />
        <input class="secondBotton" type="button" @click="convertMsg($event, item)" value="取消订单" />
      </div>

      <div v-if="item.status == this.serving">
        <hr class="hr" />
        <input class="threeBotton" type="button" @click="convertMsg($event, item)" value="服务完成" />
      </div>
      <div v-if="item.status == this.finished">
        <el-input v-if="compare(item.comment)" class="commentInput" v-model="item.comment" disabled="true"></el-input>
        <el-input v-else class="commentInput" v-model="queryList.content" placeholder="请输入评价"></el-input>
        <input class="commentButton" v-if="compare(item.comment)" type="button" value="已完成" />
        <input class="commentButton" v-else type="button" @click="addComment(item.id)" value="发表评价" />
      </div>
    </div>
    <nav v-if="this.getTotal * 1" style="text-align: -webkit-center">
      <el-pagination :page-size="this.getCurrentPageSize" style="width: max-content" :current-page="this.getCurrentPage"
        @current-change="handleCurrentChange" layout="prev, pager, next" :total="this.getTotal * 1" />
    </nav>
    <div v-if="!(this.getTotal * 1)">
      <img id="noImg" src="./img/noContent.png" />
      <h4>没有订单信息....</h4>
    </div>
  </div>
  <div class="clear"></div>
</template>

<script>
import {
  getBossOrder,
  cancelOrderById,
  Comment,
  finishOrder,
  toPay,
} from "@/api";
import {
  Menu as IconMenu,
  ArrowDownBold,
  ArrowUpBold,
} from "@element-plus/icons-vue";
import { mapMutations,mapGetters } from 'vuex'
import popmessage from "@/components/tip/index.js";
export default {
  name: "MyOrder",
  data() {
    return {
      queryList: {
        orderId: '',
        userId: '',
        content: ''
      },
      commentList: [],
      vaule1: '',
      unpaid: "未支付",
      serving: "服务中",
      finished: "已完成",
      list: [],
    };
  },
  computed: {
    ...mapGetters("OrderStore", ['getCurrentPageSize', 'getCurrentPage', 'getStatus', 'getTotal'])
  },
  methods: {
    ...mapMutations("OrderStore", ['changePageNum', 'setTotal', 'setStatus']),
    //筛选订单
    orderChoose(e, s) {
      for (
        let index = 0;
        index < e.currentTarget.parentNode.children.length;
        index++
      ) {
        e.currentTarget.parentNode.children[index].style.color = "black";
      }
      e.currentTarget.style.color = "#409eff";
      this.setStatus(s)
      this.init()
    },
    addComment(id) {
      // 实现评论接口
      let params = {
        orderId: id,
        userId: this.queryList.userId,
        content: this.queryList.content
      }
      Comment(params).then(res => {
        if (res.data.code === 200) {
          popmessage({ type: "success", str: "发表成功" });
          this.init()
        }
      })
      this.queryList.orderId = ''
    },
    // 查看是否有评价字段
    compare(comment) {
      if (comment !== undefined) {
        return true 
      } else {
        return false
      }
    },
    // 确定订单状态
    convertMsg(e, list) {
      this.queryList.orderId = list.id
      let msg = e.currentTarget.value
      if (msg === '去支付') {
        // 跳转支付页面
        let parmas = {
          orderId: list.id
        }
        toPay(parmas).then(response => {
          if (response.data.code === 200) {
            const payNo = response.data.data
            const url =
              "http://jzuserweb.aiyangniu.net/user/pay/requestPay?payNo=" +
              payNo;
            window.open(url);
            this.$router.go(0)
          }
        })
      } else if (msg === '取消订单') {
        let params = {
          orderId: list.id
        }
        cancelOrderById(params).then(res => {
          if (res.data.code === 200) {
            popmessage({ type: "success", str: "取消成功" });
            this.init()
          } else if (res.data.code === 611) {
            popmessage({ type: "error", str: "取消失败，请联系管理员" });
          }
        })
      } else if (msg === '服务完成') {
        let params = {
          orderId: this.queryList.orderId
        }
        finishOrder(params).then(response => {
          if (response.data.code === 200) {
            popmessage({ type: "success", str: "完成服务" });
            this.init()
          }
        })
      }
    },
    ...mapMutations("PerCent", ["convertIndex"]),
    handleCurrentChange(val) {
      this.changePageNum(val)
      this.init()
    },
    init() {
      const parmas = {
        orderStatus: this.getStatus,
        pageNum: this.getCurrentPage,
        pageSize: this.getCurrentPageSize,
      };
      getBossOrder(parmas).then((response) => {
        if (response.data.code === 200) {
          this.list = response.data.data.rows;
          this.setTotal(parseInt(response.data.data.total))
          this.queryList.userId = response.data.data.rows[0].userId
        }
      });
    }
  },
  created() {
    this.init()
  },
  components: {
    IconMenu,
    ArrowUpBold,
    ArrowDownBold,
  },
};
</script>
<style scoped>
.commentInput[disabled]{
  font-size: 30dp;
  font-weight: bold;
  color: black;
}
.right {
  width: 59rem;
  background-color: rgb(255, 255, 255);
  /* top: 1rem; */
  /* left: 24rem; */
  border: 1px solid gray;
  border-radius: 10px;
  display: inline-block;
  margin: 0;
  margin-left: 25rem;
  float: left;
  margin-top: -43.5rem;
}

.showTitle {
  position: relative;
  width: 100%;
  height: 3rem;
  border-bottom: 1px solid rgb(165, 162, 162);
  box-sizing: border-box;
  text-align: start;
  padding-left: 2rem;
}

.showTitle h2 {
  display: inline-block;
  font-size: 1.3rem;
  text-align: left;
}

#topNav {
  text-align: left;
}

#hr1 {
  border-width: 0.15rem;
}

#topNav h2 {
  display: inline-block;
  font-size: 1.2rem;
  font-weight: 400;
  padding: 0;
  margin: 0;
  margin-left: 2rem;
  margin-top: 1rem;
  margin-bottom: 1rem;
  cursor: pointer;
}

#topNav :first-child {
  color: #409eff;
}

#serveArea {
  margin-bottom: 2rem;
}

#tipsMsg {
  transition: all 0.4s ease;
  display: none;
  height: auto;
  padding-left: 2rem;
  padding-bottom: 1rem;
  padding-top: 1.5rem;
}

#arrow {
  padding-left: 46rem !important;
}

/* 填写信息样式
 */
#choseMsg {
  width: 90%;
  border: 1px solid rgb(112, 110, 110);
  border-radius: 2rem;
  text-align: start;
  margin: 0 auto;
  margin-top: 2rem;
  padding-left: 2.5rem;
  margin-bottom: 1rem;
}

#choseMsg span {
  display: inline-block;
  font-size: 1.2rem;
  font-weight: 400;
  padding-right: 1rem;
  margin-top: 1rem;
}

#choseMsg select {
  width: 7rem;
  height: 1.7em;
  font-size: 1rem;
  margin-top: 1rem;
  margin-right: 3rem;
}

#choseMsg .theIn {
  width: 17rem;
  height: 1.6rem;
  border: none;
  font-size: 1.1rem;
}

#choseMsg input {
  height: 1rem;
  margin-top: 1rem;
}

#emType {
  margin-left: 5.5rem;
}

#contact {
  margin-left: 2rem;
}

#residence {
  margin-bottom: 1rem;
}

#salaryIn {
  width: 3rem;
  margin-left: -1rem;
  margin-right: 0.5rem;
}

#salary {
  margin-left: 4.4rem;
}

/* 
我的状态 */
#myStatus {
  padding: 0;
  margin: 0;
  text-align: start;
  margin-top: 2rem;
}

#myStatus #statusTitle {
  margin-left: 3rem;
  font-size: 1.2rem;
  font-weight: 700;
}

#statusRight {
  display: inline-block;
  height: 3rem;
  width: auto;
  overflow: hidden;
  background-color: aqua;
  margin: 0 auto;
  margin-bottom: 0;
  margin-left: 9rem;
}

#statusRight img {}

#msgStatus {
  margin-left: 1rem;
  font-weight: 600;
}

#private {
  margin-left: 42rem;
  font-weight: 500;
  display: inline-block;
  padding-right: 1rem;
  margin-bottom: 2rem;
}

::v-deep .el-cascader .el-input {
  display: flex;
  cursor: pointer;
  border: 1px solid black;
}

::v-deep .el-input__wrapper {
  display: inline-flex;
  flex-grow: 1;
  align-items: center;
  justify-content: center;
  padding: 1px 11px;
  background-color: var(--el-input-bg-color, var(--el-fill-color-blank));
  background-image: none;
  border-radius: 0;
  transition: var(--el-transition-box-shadow);
  box-shadow: 0 0 0 1px var(--el-input-border-color, var(--el-border-color)) inset;
}

#areaStyle {
  width: 15rem;
  margin-left: 0.1rem;
  height: 1.3rem !important;
  vertical-align: inherit;
}

::v-deep .el-input__wrapper {
  display: inline-flex;
  flex-grow: 1;
  align-items: center;
  justify-content: center;
  padding: 1px 11px;
  background-image: none;
  border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
  transition: var(--el-transition-box-shadow);
}

#topContent {
  margin: 0;
  padding: 0;
  text-align: left;
}

#topContent label {
  font-size: 1.1rem;
  margin-top: 1rem;
}

.hr {
  position: absolute;
  left: 29rem;
  width: 55rem;
}

/* 备注 */
textarea {
  resize: none;
  width: 82%;
  height: 4rem;
  border: none;
  /* outline: none; */
  color: rgb(63, 62, 62);
  font-size: 0.8rem;
  font-weight: bold;
  margin-top: -1rem;
  /* border: none; */
  margin-left: 6.3rem;
  margin-bottom: 1rem;
}

/* 状态图片 */
#statusImg {
  margin: 0;
  padding: 0;
  width: 3%;
  height: auto;
}

/* 订单头部 */
.orderId {
  width: 18rem;
  text-align: left;
  padding: 0.2rem;
  display: inline-block;
}

/* 雇主 */
#employer {
  margin-right: 2.2rem;
}

.rightSpan {
  margin-left: 3.5rem;
  text-align: start;
}

/* 
总价 */
#totalPrice {
  margin-right: 2.5rem;
  margin-left: 0rem;
}

/* 备注 */
#note {
  position: relative;
  top: 0.1rem;
  left: 0.2rem;
}

#statusSpan {
  vertical-align: text-bottom;
  margin-left: 0.3rem;
}

/* 按钮 */
.firstBotton {
  margin-left: 38rem;
}

input[type="button"] {
  margin-top: 0.2rem;
  background-color: #409eff;
  width: 6rem;
  height: 2rem !important;
  border-radius: 1rem;
  display: inline-block;
  right: 2rem;
  border: none;
  margin-right: 1rem;
  color: white;
  letter-spacing: 0.1rem;
  cursor: pointer;
  margin-bottom: 0.7rem;
  font-weight: 500;
}

.secondBotton {
  background-color: white !important;
  border: 1px solid black !important;
  color: black !important;
}

.threeBotton {
  margin-left: 45rem !important;
}

.commentButton {
  margin-left: 45rem;
}

.refund {
  margin-left: 45rem;
}

.tophr {
  margin-bottom: 1rem;
}

/* 缺省页 */
#noImg {
  width: 25rem;
}

/* 
清除浮动 */
.clear {
  clear: both;
}

/* 分页按钮 */
#pagination {
  margin-bottom: 1rem;
}
</style>
