<template>
  <el-dialog
    :title="!dataForm.id ? this.$i18n.t('crud.addTitle') : this.$i18n.t('temp.modify')"
    :close-on-click-modal="false"
    width="760px"
    v-if="visible"
    @close="closeDialog"
    :visible.sync="visible">
    <el-form :model="dataForm" size="small" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="订单号" prop="orderNumber">
            <el-input id="scanInput" v-model="dataForm.orderNumber" @change="scanMatchPrintOrder" placeholder="可扫描条形码"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-button style="margin-left: 10px" type="text" @click="openOrderInfo(dataForm.orderNumber)">查看</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-select v-model="dataForm.status">
              <el-option label="待联系" :value="0"></el-option>
              <el-option label="联系异常" :value="1"></el-option>
              <el-option label="已联系待重发" :value="2"></el-option>
              <el-option label="已重发" :value="3"></el-option>
              <el-option label="地址异常" :value="4"></el-option>
              <el-option label="无需处理" :value="5"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="补交邮费" prop="status">
            <el-select v-model="dataForm.isPayed" clearable>
              <el-option key="1" label="已补交" :value="1"></el-option>
              <el-option key="0" label="未补交" :value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="原运单号" prop="deliveryNo">
            <el-input  v-model="dataForm.deliveryNo"  type="text" ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="新运单号" prop="newDeliveryNo">
            <el-input v-model="dataForm.newDeliveryNo"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="原收件人信息" prop="receiverInfo">
        <el-input type="textarea" rows="4" v-model="dataForm.receiverInfo"></el-input>
      </el-form-item>
      <el-form-item label="新收件人信息" prop="newReceiverInfo">
        <el-input type="textarea" rows="4" v-model="dataForm.newReceiverInfo"></el-input>
      </el-form-item>
      <el-form-item label="退件原因" prop="reason">
        <el-input type="textarea" rows="2" v-model="dataForm.reason"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" rows="2" v-model="dataForm.remark"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button class="default-btn" @click="visible = false">{{$t("crud.filter.cancelBtn")}}</el-button>
      <el-button class="default-btn primary-btn" type="primary" @click="dataFormSubmit()">{{$t("crud.filter.submitBtn")}}</el-button>
    </span>
    <order-info ref="orderInfo"></order-info>
  </el-dialog>
</template>

<script>
  import OrderInfo from "../order/orderInfo";
export default {
  components: {
    OrderInfo
  },
  data () {
    return {
      visible: false,
      scanDeliveryNo: null,
      dataForm: {
        id: null,
        orderNumber: null,
        deliveryNo: null,
        receiverInfo: null,
        newDeliveryNo: null,
        newReceiverInfo: null,
        status: 0,
        remark: null,
        isPayed: 0,
        reason: null
      },
      lastInputStr: null,
      dataRule: {
      }
    }
  },
  methods: {
    openOrderInfo(orderNumber){
      this.$refs.orderInfo.init({
        orderNumber: orderNumber
      })
    },
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl('/order/returnDelivery/info/' + this.dataForm.id),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            this.dataForm = data
          })
        }
        let $input = document.getElementById("scanInput");
        $input.focus();
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$http({
        url: this.$http.adornUrl('/order/returnDelivery'),
        method: this.dataForm.id ? 'put' : 'post',
        data: this.$http.adornData(this.dataForm)
      }).then(({data}) => {
        this.$message({
          message: this.$i18n.t('publics.operation'),
          type: 'success'
        });
        this.$emit("refreshDataList", null);
        this.visible = false;
      })
    },
    //扫描运单号获取订单信息
    scanMatchPrintOrder(value){
      let substr;
      if(this.lastInputStr != null){
        substr = value.replace(new RegExp(this.lastInputStr), "");
      }else{
        substr = value;
      }
      if(substr){
        this.dataForm.orderNumber = substr;
        this.$http({
          url: this.$http.adornUrl('/order/returnDelivery/findReturnOrder/' + this.dataForm.orderNumber),
          method: 'get',
        }).then(({data}) => {
          if(data){
            this.dataForm.deliveryNo = data.deliveryNo;
            this.dataForm.receiverInfo = "收货人：" + data.receiver + "\n" + "联系电话：" + data.mobile + "\n" + "收货地址：" + data.address;
            this.dataFormSubmit();
            this.lastInputStr = substr;
          }else{
            this.$message.error("订单不存在");
          }
        }).catch((error) => {
          this.$message.error("运单号查询异常");
        })
      }
    },
    closeDialog(){
      this.visible = false;
      this.scanDeliveryNo = null;
      this.$emit("refreshDataList", null);
      this.lastInputStr = null;
    },
  }
}
</script>
