<template>
  <view class="page-raw-returning">
    <view class="header" :style="{ backgroundColor: `rgba(23, 83, 234, ${scrollTop / 100})` }">
      <view class="left">
        <u-icon class="icon" name="arrow-left" @click="uni.navigateBack()" />
      </view>
      <view class="title">内销出库<text v-if="isBH === '1'">备货模式</text></view>
      <view class="right"></view>
    </view>
    <u-form class="form" ref="form" :model="form" :error-type="['toast']" label-width="120rpx">
      <u-form-item label="月台">
        <u-input type="select" disabled />
      </u-form-item>
      <u-row gutter="0">
        <u-col :span="7">
          <u-form-item label="单据号" prop="orderNo">
            <u-input v-model="form.orderNo" @confirm="onPickupOrder('TD')" />
          </u-form-item>
        </u-col>
        <u-col :span="3">
          <u-form-item prop="orderType">
            <u-input v-model="orderTypeText" type="select" @click="orderTypeSelect = true" />
            <u-select
              :confirm-text="this.$t('message.product_Confirm')"
              :cancel-text="this.$t('message.product_Cancel')"
              v-model="orderTypeSelect"
              :list="model.orderTypeList"
              @confirm="
                (v) => {
                  form.orderType = v[0].value;
                  orderTypeText = v[0].label;
                }
              "
            ></u-select>
          </u-form-item>
        </u-col>
        <u-col :span="2" text-align="right">
          <u-button size="mini" @click="onPickupOrder">提单</u-button>
        </u-col>
      </u-row>
      <u-row gutter="0">
        <u-col :span="10">
          <u-form-item label="车次" prop="carBatchno">
            <u-input v-model="form.carBatchno" @confirm="onCarBatchNoConfirm" />
          </u-form-item>
        </u-col>
        <u-col :span="2" text-align="right">
          <u-button size="mini" @click="onSelectOrder">选择</u-button>
        </u-col>
      </u-row>
      <u-row gutter="0">
        <u-col :span="4">
          <u-form-item label="单据总数" prop="orderQty">
            <u-input v-model="model.orderQty" />
          </u-form-item>
        </u-col>
        <u-col :span="4">
          <u-form-item label="已分配" prop="ygcj" label-width="95rpx" label-align="right">
            <u-input v-model="model.ygcj" />
          </u-form-item>
        </u-col>
        <u-col :span="4">
          <u-form-item label="已扫描" prop="yicj" label-width="95rpx" label-align="right">
            <u-input v-model="model.yicj" />
          </u-form-item>
        </u-col>
      </u-row>
      <u-row gutter="0">
        <u-col :span="10">
          <u-form-item label="物料" prop="productInfo.productCode">
            <u-input v-model="model.productInfo.productCode" @confirm="onProductCodeConfirm" />
          </u-form-item>
        </u-col>
        <u-col :span="2" text-align="right">
          <u-button size="mini" @click="onProductCodeSelect">选择</u-button>
        </u-col>
      </u-row>
      <u-row gutter="0">
        <u-col :span="10">
          <u-form-item label="物料描述">
            <u-input v-model="model.productInfo.productDesc" disabled />
          </u-form-item>
        </u-col>
        <u-col :span="2" text-align="right">
          <u-button size="mini" disabled>库存</u-button>
        </u-col>
      </u-row>
      <u-row gutter="0">
        <u-col :span="5">
          <u-form-item label="物料总数">
            <u-input v-model="model.productInfo.ygcj" disabled />
          </u-form-item>
        </u-col>
        <u-col :span="4">
          <u-form-item label="已扫" label-width="70rpx" label-align="right">
            <u-input v-model="model.productInfo.yicj" disabled />
          </u-form-item>
        </u-col>
        <u-col :span="3" text-align="right">
          <u-button size="mini">拣货明细</u-button>
        </u-col>
      </u-row>
      <u-row gutter="0">
        <u-col :span="10">
          <u-form-item label="托盘">
            <u-input disabled />
          </u-form-item>
        </u-col>
        <u-col :span="2" text-align="right">
          <u-button size="mini" disabled>确认</u-button>
        </u-col>
      </u-row>
      <u-row gutter="0">
        <u-col :span="10">
          <u-form-item label="条码" prop="sapFactoryCode">
            <u-input v-model="form.sapFactoryCode" />
          </u-form-item>
        </u-col>
        <u-col :span="2" text-align="right" v-if="isBH === '1'">
          <u-button size="mini">记账</u-button>
        </u-col>
      </u-row>
    </u-form>
    <view class="bottom-bar">
      <u-row class="button-bar">
        <u-col :span="3">
          <u-button type="warning" @click="$form.resetFields()">清空</u-button>
        </u-col>
        <u-col :span="3">
          <u-button v-if="isBH === '1'" type="error">备货取消</u-button>
        </u-col>
        <u-col :span="3">
          <u-button v-if="isBH === '0'" type="primary" @click="onSubmit">记账</u-button>
          <u-button v-else-if="isBH === '1'" type="primary" @click="onSubmit">备货完成</u-button>
        </u-col>
        <u-col :span="3">
          <u-button type="success" @click="onPickupOrder('CXTD')">重提</u-button>
        </u-col>
      </u-row>
    </view>
  </view>
</template>

<script lang="ts">
import { Component, Ref } from 'vue-property-decorator';
import { BasePage } from '@/components/base/page';
import { VForm, VFormRules } from 'vue/types/form';
import model from './model';

@Component
export default class internalDom extends BasePage {
  /**
   * 表单引用
   */
  @Ref('form') readonly $form!: VForm;

  /**
   * 是否备货
   */
  isBH = '0';

  /**
   * 页面Module
   */
  model = model;

  /**
   * 表单数据
   */
  form = {
    orderNo: null,
    orderType: model.orderTypeList[0].value,
    carBatchno: null,
  };

  /**
   * 订单类型选择
   */
  orderTypeSelect = false;
  orderTypeText = model.orderTypeList[0].label;

  /**
   * 表单验证规则
   */
  rules: VFormRules<any> = {
    orderNo: [{ required: true, message: '单据号不能为空！' }],
  };

  get guest(): boolean {
    return true;
  }

  /**
   * 提单/重新提单
   * @param tdflag TD / CXTD
   */
  onPickupOrder(tdflag: string) {
    this.$form.validate(async (valid: boolean) => {
      if (!valid) return;
      this.model.outStorageLoadProduct({
        orderNo: this.form.orderNo,
        orderType: this.form.orderType,
        tdflag,
        isBH: this.isBH,
      });
    });
  }

  /**
   * 选择单据
   */
  onSelectOrder() {
    if (this.model.odsProOrderOutList.length === 0) {
      uni.showToast({ icon: 'none', title: '请先输入车次号，按回车加载单据' });
      return;
    }
  }

  /**
   * 车次回车
   */
  onCarBatchNoConfirm() {
    if (!this.form.carBatchno) {
      uni.showToast({ icon: 'none', title: '请输入车次号' });
      return;
    }
    this.model.outStorageLoadDocumentByCarBatchno({
      carBatchno: this.form.carBatchno,
    });
  }

  /**
   * 物料回车
   */
  onProductCodeConfirm() {
    if (this.model.ygcj === null) {
      uni.showToast({ icon: 'none', title: '请先提单' });
      return;
    }
  }

  /**
   * 物料选择
   */
  onProductCodeSelect() {
    if (this.model.ygcj === null) {
      uni.showToast({ icon: 'none', title: '请先提单' });
      return;
    }
  }

  // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
  async onReady() {
    this.$form.setRules(this.rules);
  }

  /**
   * 记账
   */
  onSubmit() {
    this.$form.validate(async (valid: boolean) => {
      if (!valid) return;
      if (this.model.ygcj === null) {
        uni.showToast({ icon: 'none', title: '请先提单' });
        return;
      }
      const text = this.isBH === '1' ? '备货完成' : '记账';
      uni.showModal({
        title: '提示',
        content: `确认要${text}？`,
        success: async () => {
          await this.model.orderPost({
            orderNo: this.form.orderNo,
            orderType: this.form.orderType,
            isBH: this.isBH,
          });
          const title = text + '成功';
          uni.showToast({ icon: 'none', title });
        },
      });
    });
  }
}
</script>

<style lang="scss" scoped>
.page-raw-returning {
  background: #f2f2f2 linear-gradient(0deg, #f2f2f2 0%, #4a78ea 51%, #1753ea 100%) no-repeat;
  background-size: 100% 600rpx;
  padding: 118rpx 30rpx 162rpx;
  min-height: 100%;

  .header {
    position: fixed;
    top: 36rpx;
    left: 0;
    right: 0;
    z-index: 99;
    display: flex;
    height: 88rpx;
    line-height: 88rpx;
    color: #fff;
    font-size: 34rpx;
    font-weight: 500;
    text-align: center;
    .title {
      flex: 3;
    }
    .left,
    .right {
      flex: 1;
    }
    .icon {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 88rpx;
      height: 88rpx;
    }
  }

  .form {
    background-color: #fff;
    padding: 40rpx;
    border-radius: 10rpx;
    box-shadow: 0 0 20rpx 0 rgba(128, 128, 128, 0.2);

    .u-form-item {
      padding: 30rpx 0;
      line-height: 35rpx;
    }
  }

  .bottom-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 99;
    background: #ffffff;
    box-shadow: 0 1rpx 20rpx 0 rgba(128, 128, 128, 0.2);
    padding: 20rpx;
  }

  .button-bar {
  }
}
</style>
