<template>
  <view class="all-container">
    <view class="bg"></view>
    <Loading
    	:type="3"
    	:loadStatus="loadStatus"
    	:showText="errorText"
    />
    <zj-page-layout
      v-if="detail"
      :hasFooter="true"
      :isScroll="true"
      :refresherTriggered="refresherTriggered"
      @refresherrefresh="refresherrefresh">
      <view class="content-container">
        <view class="status-container">{{detail.status | statusFilter}}</view>
        <view class="product-container card">
          <view class="title">配件信息</view>
          <view class="product">
            <view class="it" v-for="(item, index) in detail.items" :key="index">
              <view class="name">{{item.name}}</view>
              <view class="num">x{{item.qty}}</view>
            </view>
          </view>
        </view>
        <view class="info-container card">
          <view class="title">申请信息</view>
          <view class="row">
            <view class="label">申请单号</view>
            <view class="value">{{detail.id}}</view>
            <view class="tag" @tap="$copy(detail.id)">复制</view>
          </view>
          <view class="row">
            <view class="label">申请人员</view>
            <view class="value">{{detail.createBy}}</view>
          </view>
          <view class="row">
            <view class="label">申请时间</view>
            <view class="value">{{detail.createTime}}</view>
          </view>
          <view class="row" v-if="detail.status == 'END'">
            <view class="label">反馈人员</view>
            <view class="value">{{detail.confirmBy}} {{detail.confirmMobile}}</view>
          </view>
          <view class="row" v-if="detail.status == 'END'">
            <view class="label">反馈时间</view>
            <view class="value">{{detail.confirmTime}}</view>
          </view>
          <view class="row" v-if="detail.status == 'CANCEL'">
            <view class="label">取消人员</view>
            <view class="value">{{detail.cancelBy}}</view>
          </view>
          <view class="row" v-if="detail.status == 'CANCEL'">
            <view class="label">取消时间</view>
            <view class="value">{{detail.cancelTime}}</view>
          </view>
          <view class="row">
            <view class="label">备注信息</view>
            <view class="value">{{detail.remark}}</view>
          </view>
        </view>
        <view class="info-container card">
          <view class="title">工单信息</view>
          <view class="row">
            <view class="label">关联工单</view>
            <view class="value">{{detail.orderBaseId}}</view>
            <view class="tag" @tap="$copy(detail.orderBaseId)">复制</view>
          </view>
          <view class="row">
            <view class="label">客户电话</view>
            <view class="value">{{detail.userMobile}}</view>
          </view>
          <view class="row">
            <view class="label">所属网点</view>
            <view class="value">{{detail.websitName}}</view>
          </view>
        </view>
      </view>
      <template slot="footer">
        <view class="bottom-container" v-if="detail.status == 'ING'">
          <view class="left"></view>
          <view class="right">
            <u-button text="取消申请" @click="cancelOrder"></u-button>
          </view>
        </view>
      </template>
    </zj-page-layout>
  </view>
</template>

<script>
  export default {
    filters: {
      statusFilter(val) {
        const MAP = {
          ING: '申请中',
          END: '已到货',
          CANCEL: '已取消',
        }
        return MAP[val];
      },
    },

    data() {
      return {
        id: null,
        refresherTriggered: false,
        loadStatus: 0,
				errorText: '',
        detail: null,
      }
    },

    onLoad({id}) {
      this.id = id;
      this.getDetail();
    },

    methods: {
      getDetail() {
        this.$api.post('/material/parts/apply/detail', {
          id: this.id
        }).then(res => {
          this.detail = res.data;
          this.loadStatus = 0;
        }).catch(res => {
      		this.errorText = res.message;
      		this.loadStatus = 2;
      	}).finally(res => {
      		this.refresherTriggered = false;
      	})
      },

      // 下拉刷新
      refresherrefresh() {
        this.refresherTriggered = true;
        this.getDetail();
      },

      // 取消申请
      cancelOrder() {
        this.$modal({
          content: '确认取消申请吗？'
        }).then(() => {
          this.$api.post('/material/parts/apply/cancel', {
            id: this.id
          }).then(res => {
            this.$successToast();
            this.getDetail();
          })
        }).catch(() => {})
      }
    },
  }
</script>

<style lang="scss" scoped>
.all-container {
  position: relative;
}
.bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 300rpx;
  background: linear-gradient(179.48deg, rgba(200,224,251,1) 0.45%,rgba(247,247,247,1) 98.96%);
}
.card {
  @include zj-card;
  margin-top: 30rpx;
  padding: 30rpx;
  .title {
    font-size: 32rpx;
    font-weight: 600;
  }
}
.content-container {
  padding: 20rpx;
}
.status-container {
  font-size: 36rpx;
  font-weight: 500;
  margin-left: 30rpx;
  text {
    margin-left: 12rpx;
  }
}
.product-container {
  .title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .left {
      font-size: 32rpx;
      font-weight: 600;
    }
    .right {
      font-size: 28rpx;
      color: $theme-color;
    }
  }
  .product {
    background: #f5f5f5;
    border-radius: 12rpx;
    padding: 20rpx;
    margin-top: 20rpx;
    .it {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 20rpx;
      &:last-child {
        margin-bottom: 0;
      }
      .name {
        flex: 1;
        font-size: 28rpx;
        font-weight: 500;
      }
      .price {
        margin-left: 30rpx;
        font-size: 28rpx;
        font-weight: 500;
      }
    }
  }
}
.info-container {
  .row {
    display: flex;
    align-items: center;
    margin-top: 20rpx;
    .label {
      font-size: 28rpx;
      color: $sec-font;
      margin-right: 20rpx;
    }
    .value {
      flex: 1;
      font-size: 28rpx;
      text {
        color: $minor-color;
      }
      &.price {
        color: $theme-color;
      }
    }
    .tag {
      font-size: 28rpx;
      color: $theme-color;
    }
  }
}
.bottom-container {
  padding: 20rpx;
  display: flex;
  justify-content: space-between;
  .right {
    display: flex;
    ::v-deep .u-button {
      margin-left: 20rpx;
    }
  }
}
</style>
