<template>
  <view class="page-raw-receipt">
    <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">{{ $t('message.rawMenu_virtual_Handover') }}</view>
      <view class="right"></view>
    </view>
    <u-form class="form" ref="form" :model="form" label-width="120rpx">
      <u-row>
        <!-- MRP -->
        <u-col :span="12">
          <u-form-item :required="true" label-width="120rpx" label="MRP" prop="mrpScope">
            <u-input v-model="form.mrpScope" :placeholder="this.$t('message.po_PleaseInput')" />
          </u-form-item>
        </u-col>
        <!-- <u-col :span="6">
          <u-form-item label-width="120rpx" :label="this.$t('message.Summary_Contain')">
            <u-input
              v-model="form.mrpContain"
              :placeholder="this.$t('message.po_PleaseInput')"
              style="margin-left: 12px"
            />
          </u-form-item>
        </u-col> -->
        <!-- <u-col :span="6">
          <u-form-item label-width="120rpx" :label="this.$t('message.Summary_Exclude')">
            <u-input
              v-model="form.mrpReduce"
              :placeholder="this.$t('message.po_PleaseInput')"
              style="margin-left: 12px"
            />
          </u-form-item>
        </u-col> -->
        <!-- 工厂 -->
        <u-col :span="12">
          <u-form-item
            label-width="120rpx"
            :label="this.$t('message.Pi_factory')"
            prop="sapFactoryCode"
            :required="true"
          >
            <u-input
              v-model="form.sapFactoryCode"
              :placeholder="this.$t('message.po_PleaseInput')"
            />
          </u-form-item>
        </u-col>
        <!-- 订单号 -->
        <u-col :span="6">
          <u-form-item label-width="120rpx" :label="this.$t('message.Pi_OrderNo')">
            <u-input v-model="form.orderNoStart" :placeholder="this.$t('message.po_PleaseInput')" />
          </u-form-item>
        </u-col>
        <u-col :span="6">
          <u-form-item label-width="120rpx" :label="this.$t('message.Summary_To')">
            <u-input v-model="form.orderNoEnd" :placeholder="this.$t('message.po_PleaseInput')" />
          </u-form-item>
        </u-col>
        <!-- <u-col :span="6">
          <u-form-item label-width="120rpx" :label="this.$t('message.Summary_DemandDate')">
            <u-input
              v-model="form.requestTime"
              type="select"
              @click="requestTimeSelect = true"
              :placeholder="this.$t('message.po_PleaseInput')"
            />
            <u-calendar
              v-model="requestTimeSelect"
              mode="range"
              max-date="2099-12-31"
              @change="
                (e) =>
                  (form.requestTime = Array.from(
                    new Set([e.startDate.replaceAll('-', ''), e.endDate.replaceAll('-', '')]),
                  ).join('-'))
              "
            ></u-calendar>
          </u-form-item>
        </u-col>
        <u-col :span="6">
          <u-form-item label-width="120rpx" :label="this.$t('message.Summary_ProductionDate')">
            <u-input
              v-model="form.productDate"
              type="select"
              @click="productDateSelect = true"
              :placeholder="this.$t('message.po_PleaseInput')"
            />
            <u-calendar
              v-model="productDateSelect"
              mode="range"
              max-date="2099-12-31"
              @change="
                (e) =>
                  (form.productDate = Array.from(
                    new Set([e.startDate.replaceAll('-', ''), e.endDate.replaceAll('-', '')]),
                  ).join('-'))
              "
            ></u-calendar>
          </u-form-item>
        </u-col>
        <u-col :span="6">
          <u-form-item label-width="120rpx" :label="this.$t('message.Pi_Station')">
            <u-input v-model="form.sendSpotList" :placeholder="this.$t('message.po_PleaseInput')" />
          </u-form-item>
        </u-col>
        <u-col :span="6">
          <u-form-item label-width="120rpx" :label="this.$t('message.po_Location')">
            <u-input v-model="form.wkposCode" :placeholder="this.$t('message.po_PleaseInput')" />
          </u-form-item>
        </u-col>
        <u-col :span="6">
          <u-form-item label-width="120rpx" :label="this.$t('message.Summary_BatchNo')">
            <u-input
              v-model="form.batchNumberList"
              :placeholder="this.$t('message.po_PleaseInput')"
            />
          </u-form-item>
        </u-col> -->
        <!-- 物料号 -->
        <u-col :span="12">
          <u-form-item label-width="120rpx" :label="this.$t('message.po_MaterielNo')">
            <u-input
              v-model="form.materialCodeList"
              :placeholder="this.$t('message.po_PleaseInput')"
            />
          </u-form-item>
        </u-col>
        <!-- 表 -->
        <u-col :span="12">
          <view class="table-wrapper">
            <wyb-table
              class="table"
              ref="table"
              width="100%"
              enable-check="multiple"
              show-left-and-right-border
              :headers="headers"
              :contents="model.proOrderList"
              :show-vert-border="false"
            ></wyb-table>
          </view>
        </u-col>
      </u-row>
    </u-form>
    <view class="bottom-bar">
      <u-row class="button-bar">
        <u-col :span="6">
          <u-button type="primary" @click="onQuery">{{ $t('message.Pi_order') }}</u-button>
        </u-col>
        <u-col :span="3">
          <u-button type="success" @click="onOk">{{ $t('message.dn_Confirm') }}</u-button>
        </u-col>
        <u-col :span="3">
          <u-button type="error" @click="uni.navigateBack()">{{
            $t('message.po_Return')
          }}</u-button>
        </u-col>
      </u-row>
    </view>
  </view>
</template>

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

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

  @Watch('form', { immediate: true, deep: true })
  onPersonChanged() {
    this.isFormChange = true;
  }

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

  /**
   * 表头
   */
  headers = headers;

  /**
   * 表单数据
   */
  form = {
    mrpScope: '',
    mrpContain: '',
    mrpReduce: '',
    orderNoStart: '',
    orderNoEnd: '',
    sapFactoryCode: '',
    wkposCode: '',
    batchNumberList: '',
    materialCodeList: '',
    sendSpotList: '',
  };

  /**
   * 表单是否改变
   */
  isFormChange = false;

  /**
   * 选择项显示
   */
  requestTimeSelect = false;
  productDateSelect = false;

  /**
   * 表单校验
   */
  rules = {
    mrpScope: [{ required: true, message: this.$t('message.Pi_InputMrp') as string }],
    sapFactoryCode: [{ required: true, message: this.$t('message.Pi_InputFactory') as string }],
  };

  /**
   * 生成表单参数
   */
  get formParams() {
    const params: any = {
      ...this.form,
      factoryCode: session.factoryCode,
      loginName: session.loginName,
    };
    if (this.form.batchNumberList) {
      params.batchNumberList = this.form.batchNumberList.split(',');
    } else {
      delete params.batchNumberList;
    }
    if (this.form.materialCodeList) {
      params.materialCodeList = this.form.materialCodeList.split(',');
    } else {
      delete params.materialCodeList;
    }
    if (this.form.mrpContain) {
      params.mrpContain = this.form.mrpContain.split(',');
    } else {
      delete params.mrpContain;
    }
    if (this.form.mrpReduce) {
      params.mrpReduce = this.form.mrpReduce.split(',');
    } else {
      delete params.mrpReduce;
    }
    if (this.form.sendSpotList) {
      params.sendSpotList = this.form.sendSpotList.split(',');
    } else {
      delete params.sendSpotList;
    }
    return params;
  }

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

  /**
   * 确认
   */
  onOk() {
    this.$form.validate(async (valid: boolean) => {
      if (!valid) return;
      let orderNoList = [];
      let materialCodeList = []
      if (this.isFormChange || this.model.proOrderList.length === 0) {
        await model.queryProOrder(this.formParams);
        orderNoList = model.proOrderList.map((_: any) => _.prdOrder);
        materialCodeList = model.proOrderList.map((_: any) => _.materialCode);
      } else {
        orderNoList = this.model.checkedProOrderList.map((_: any) => _.prdOrder);
        materialCodeList = this.model.checkedProOrderList.map((_: any) => _.materialCode);
      }
      if (orderNoList.length === 0 && this.model.proOrderList.length > 0) {
        this.customToast(this.$t('message.Pi_OrderNoNotSelected') as string);
        return;
      } else if (orderNoList.length === 0) {
        this.customToast(this.$t('message.Pi_NoDataFound') as string);
        return;
      }
      const params = {
        ...this.formParams,
        orderNoList: orderNoList,
        materialCodeList:materialCodeList,
      };
      await this.model.queryProOrderResult(params);
      this.model.setSapFactoryCode({ sapFactoryCode: this.form.sapFactoryCode });
      this.toPage(this.page.raw.handover.virtual.result);
    });
  }

  /**
   * 定单查询
   */
  onQuery() {
    this.$form.validate(async (valid: boolean) => {
      if (!valid) return;
      this.model.queryProOrder(this.formParams);
      this.isFormChange = false;
    });
  }
}
</script>

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

  .header {
    position: fixed;
    top: 36rpx;
    left: 0;
    right: 0;
    z-index: 99;
    display: flex;
    padding-top: var(--status-bar-height);
    height: calc(var(--status-bar-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: 30rpx;
    border-radius: 10rpx;
    box-shadow: 0 0 20rpx 0 rgba(128, 128, 128, 0.2);

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

  .table-wrapper {
    padding: 10rpx 0;
  }

  .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>
