<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <headerTitle title="收货信息"></headerTitle>
      <el-row>
        <el-col :span="12">
          <el-form-item
            label="选择订单"
            prop="orderNo"
            style="margin-bottom: 22px"
          >
            <el-input
              clearable
              v-model="form.orderNo"
              @click.native="handleOrderBtn"
              placeholder="请输入"
            />
          </el-form-item>
          <el-form-item
            label="供应商名称"
            prop="supplierName"
            style="margin-bottom: 22px"
          >
            <span>{{ form.supplierName }}</span>
          </el-form-item>
          <el-form-item
            label="供应商联系人"
            prop="linkName"
            style="margin-bottom: 22px"
          >
            <el-input clearable v-model="form.linkName" placeholder="请输入" />
          </el-form-item>

          <el-form-item prop="receiveFiles" label="附件">
            <fileUpload
              v-model="form.receiveFiles"
              module="main"
              :showLib="false"
              :limit="10"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item
            label="收货单类型"
            prop="receiveType"
            style="margin-bottom: 22px"
          >
            <DictSelection
              dictName="采购收货单类型"
              clearable
              v-model="form.receiveType"
            >
            </DictSelection>
          </el-form-item>
          <el-form-item
            label="供应商车辆号"
            prop="carNo"
            style="margin-bottom: 22px"
          >
            <el-input clearable v-model="form.carNo" placeholder="请输入" />
          </el-form-item>

          <!-- <el-form-item
              label="订单编码"
              prop="orderNo"
              style="margin-bottom: 22px"
            >
            <span>{{form.orderNo}}</span>
              
            </el-form-item> -->

          <el-form-item
            label="供应商电话"
            prop="linkPhone"
            style="margin-bottom: 22px"
          >
            <el-input clearable v-model="form.linkPhone" placeholder="请输入" />
          </el-form-item>
          <el-form-item
            label="送货单号"
            prop="sendNoteNo"
            style="margin-bottom: 22px"
          >
            <el-input
              clearable
              v-model="form.sendNoteNo"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <headerTitle title="产品清单" style="margin-top: 30px"></headerTitle>
    <inventoryTable
      ref="inventoryTableref"
      :orderId="form.orderId"
    ></inventoryTable>

    <orderListDialog
      ref="orderListDialogRef"
      @changeParent="changeOrder"
    ></orderListDialog>
  </div>
</template>

<script>
  import fileUpload from '@/components/upload/fileUpload';
  import dictMixins from '@/mixins/dictMixins';
  import { getpurchaseorderDetail } from '@/api/bpm/components/purchasingManage/purchaseOrder';

  import { getReceiveSaleOrderrecordDetail } from '@/api/bpm/components/purchasingManage/purchaseorderreceive';
  import inventoryTable from './inventoryTable.vue';
  import orderListDialog from './orderListDialog.vue';
  import { copyObj } from '@/utils/util';

  export default {
    mixins: [dictMixins],
    components: {
      fileUpload,
      inventoryTable,
      orderListDialog
    },
    props: {
      businessId: {
        default: ''
      }
    },
    data() {
      let formDef = {
        id: '',
        contractId: '',
        receiveFiles: [],
        contractName: '',
        carNo: '',
        carId: '',
        linkName: '',
        linkPhone: '',
        orderNo: '',
        orderId: '',
        sendNoteNo: '',
        receiveType: ''
      };

      return {
        detailData: {},
        payWayOptions: [],
        delDetailIds: [],
        linkNameOptions: [],
        visible: false,
        title: '',
        row: {},
        activeName: 'base',
        formDef,
        form: copyObj(formDef),
        tableBankData: [],
        tableLinkData: [],
        // 组织机构树形结构数据
        rules: {
          orderNo: [
            { required: true, message: '请选择采购订单', trigger: 'change' }
          ],
          receiveType: [
            { required: true, message: '请选择采购单类型', trigger: 'change' }
          ]
        },

        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },
    computed: {
      allcountAmount() {
        return this.$store.state.order.allcountAmount;
      }
    },

    created() {
      this.getReceiveSaleOrderDetail(this.businessId);
    },
    methods: {
      //选择订单回调
      changeOrder(obj) {
        this.form = Object.assign({}, this.form, {
          orderId: obj.id,
          orderNo: obj.orderNo
        });
        this.getPurchaseorder(obj.id);
      },
      //获取订单详情
      async getPurchaseorder(id) {
        this.loading = true;
        const data = await getpurchaseorderDetail(id);
        this.loading = false;
        if (data) {
          this.$nextTick(() => {
            let {
              partbId,
              partbName,
              partbTel,
              orderNo,
              id,
              partbLinkName,
              productList
            } = data;
            if (productList && productList.length > 0) {
              productList.forEach((v) => {
                v.orderTotalCount = v.orderTotalCount || v.totalCount;
              });
            }
            this.form = Object.assign({}, this.form, {
              supplierId: partbId,
              supplierName: partbName,
              linkName: partbLinkName,
              linkPhone: partbTel,
              orderNo,
              orderId: id,
              productList
            });
            this.$refs.inventoryTableref &&
              this.$refs.inventoryTableref.putTableValue(productList);
          });
        }
      },

      //发货单详情
      async getReceiveSaleOrderDetail(id) {
        this.loading = true;
        const data = await getReceiveSaleOrderrecordDetail(id);
        this.loading = false;
        if (data) {
          this.$nextTick(() => {
            this.form = data;
            this.$refs.inventoryTableref &&
              this.$refs.inventoryTableref.putTableValue(data.productList);
          });
        }
      },

      handHead() {
        let item = {
          id: this.form.partbLinkId
        };
        this.$refs.headRef.open(item);
      },

      //选择订单弹框
      handleOrderBtn() {
        let item = {
          id: this.form.orderId
        };
        this.$refs.orderListDialogRef.open(item);
      },

      getValidate() {
        return Promise.all([
          new Promise((resolve, reject) => {
            this.$refs.form.validate((valid) => {
              if (!valid) {
                reject(false);
              } else {
                resolve(true);
              }
            });
          }),
          new Promise((resolve, reject) => {
            this.$refs.inventoryTableref.validateForm((valid) => {
              if (!valid) {
                reject(false);
              } else {
                resolve(true);
              }
            });
          })
        ]);
      },
      async getTableValue() {
        try {
          await this.getValidate();
          this.form.receiveTypeName = this.getDictValue(
            '采购收货单类型',
            this.form.receiveType
          );
          // 表单验证通过，执行保存操作
          this.loading = true;
          let commitData = Object.assign({}, this.form, {
            productList: this.$refs.inventoryTableref.getTableValue()
          });
          return commitData;
        } catch (error) {
          console.log(error);
          // 表单验证未通过，不执行保存操作
        }
      }
    }
  };
</script>
<style scoped lang="scss">
  .TotalAmount {
    font-size: 16px;
    padding-right: 30px;
  }

  .headbox {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    .amount {
      font-size: 14px;
      font-weight: bold;
      margin-right: 20px;
    }
  }
</style>
