<template>
  <div class="manualDelivery">
    <Form
      ref="formInline"
      :model="formInline"
      :rules="ruleValidate"
      :label-width="70"
      inline
    >
      <div class="formRow">
        <FormItem
          prop="orderCode"
          label="订单编号"
        >
          <Input
            v-model="formInline.orderCode"
            style="width: 240px;"
            type="text"
            disabled
          />
        </FormItem>
        <FormItem
          prop="storeName"
          label="店铺名称"
        >
          <Input
            v-model="formInline.storeName"
            type="text"
            style="width: 240px;"
            disabled
          />
        </FormItem>
      </div>
      <div class="formRow">
        <FormItem
          prop="orderCode"
          label="物流公司"
        >
          <Select
            v-model="formInline.expressName"
            clearable
            style="width:240px"
          >
            <Option
              v-for="item in expressList"
              :key="item.value"
              :value="item.value"
            >
              {{ item.label }}
            </Option>
          </Select>
        </FormItem>
        <FormItem
          prop="expressCode"
          label="物流单号"
        >
          <Input
            v-model="formInline.expressCode"
            type="text"
            style="width: 240px;"
          />
        </FormItem>
      </div>
    </Form>
    <div
      class="settingBtnBox"
      style="width: calc(100% + 32px);margin-left: -16px;padding-right: 16px;text-align: center;padding-top: 15px;border-top: 1px solid #DEDEDE;"
    >
      <Button
        size="small"
        class="cancelBtn"
        ghost
        @click="cancel"
      >
        取消
      </Button>
      <Button
        type="primary"
        size="small"
        class="sureBtn"
        :loading="loading"
        @click="saveManualDelivery()"
      >
        确定
      </Button>
    </div>
  </div>
</template>

<script>
  const R3 = window.R3;

  const { network } = R3;
  export default {
    components: {},
    props: {
      selectRowData: {// 获取ID用于多选,获取勾选的明细对应的row内的数据
        type: [Array, Object],
        default: () => {}
      }
    },
    data() {
      return {
        formInline: {
          orderCode: '',
          storeName: '',
          expressName: '',
          expressCode: ''
        },
        ruleValidate: {
          expressCode: [
            { required: true, message: '请填写物流单号', trigger: 'blur' }
          ],
        },
        expressList: [],
        loading: false,
      };
    },
    created() {
      this.getExpressList();
      this.getDetail();
    },
    methods: {
      getExpressList() {
        let params = { 
          table: 'T_V_OMSLOGISTICSCOMPANY',
          range: 1000,
          fixedcolumns: { IsActive: '=Y' } 
        };
        network.post('/p/cs/list/query', params).then((res) => {
          if (res.status == 200) {
            let data = res.data.rows;
            data.forEach((item) => {
              let obj = {
                label: item.NAME.val,
                value: item.ID.val
              };
              this.expressList.push(obj);
            });
          }
        });
      },
      getDetail() {
        this.formInline.orderCode = this.selectRowData[0].ID.val;
        this.formInline.storeName = this.selectRowData[0].ORDERSHOPID.val;
        this.formInline.expressName = String(this.selectRowData[0].SHIPPERID.refobjid);
        this.formInline.expressCode = this.selectRowData[0].LOGISTICSNUMBER.val;
      },
      cancel() {
        this.$emit('closeActionDialog', true);
      },
      saveManualDelivery() {
        if (!this.formInline.expressCode) {
          this.$Message.info('请填写物流单号');
          return;
        }
        this.loading = true;
        // 更换手工发货接口
        let params = {
          operateType: 1,
          orderId: this.formInline.orderCode,
          shipperId: this.formInline.expressName,
          logisticsNumber: this.formInline.expressCode,
        };
        window.parent.R3.network.post('/p/cs/order/ship', params).then((res) => {
          if (res.data.code == 0) {
            if (res.data.code == 0) {
              this.$Message.success('执行手工发货成功');
              this.loading = false;
              // 成功关闭弹框
              this.$emit('cancel');
            } else {
              this.loading = false;
            }
          } else {
            this.loading = false;
          }
        });
        // let params = {
        //   ids: [this.formInline.orderCode],
        //   tableName: 'T_V_OMSPRINTORDER',
        //   actionName: 'T_V_OMSPRINTORDER_执行手工发货',
        //   param: { LOGISTICSID: this.formInline.expressName, LOGISTICSNO: this.formInline.expressCode },
        //   isContainsParam: true
        // };
        // network.post('/p/cs/exeAction', params).then((res) => {
        //   if (res.data.code == 0) {
        //     this.$Message.success('执行手工发货成功');
        //     this.loading = false;
        //     // 成功关闭弹框
        //     this.$emit('cancel');
        //   } else {
        //     this.loading = false;
        //   }
        // });
      }
    },
  };
</script>
<style lang='less'>
  .manualDelivery .ark-form .ark-form-item {
    margin-bottom: 0px;
  }
  .manualDelivery .ark-form .ark-form-item .ark-input {
    height: 32px;
  }
  .manualDelivery .ark-select-single .ark-select-selection {
    height: 32px;
  }
  .manualDelivery .ark-select-single .ark-select-selection .ark-select-placeholder, .manualDelivery .ark-select-single .ark-select-selection .ark-select-selected-value {
    height: 30px;
    line-height: 30px;
  }
</style>
<style lang='less' scoped>
  .manualDelivery {
    width: 740px;
    .formRow {
      display: flex;
      justify-content: space-between;
      padding: 24px 0px 24px 8px;
      border-bottom: 1px solid #EBE7E7;
    }
    .formRow:first-child {
      padding-top: 14px;
    }
    .formRow:last-child {
      border-bottom: none;
    }
  }
  .cancelBtn {
    width: 72px;
    height: 32px;
    border: 1px solid #DADADA;
    color: #909399;
    margin-right: 10px;
  }
  .sureBtn {
    width: 72px;
    height: 32px;
  }
</style>
