<template>
  <Page name="change-goods" flank>
    <template #content>
      <u-form :model="form" ref="uForm" labelWidth="100">
        <view class="card">
          <u-form-item label="货物名称" required @click="choseGoodsName">
            <u-input v-model="form.goodsName" border="none" suffix-icon="arrow-right" placeholder="请选择" disabledColor="#ffffff" disabled />
          </u-form-item>
          <u-form-item v-if="showGoodsValue" label="货物价值" required>
            <u-input v-model="form.goodsValue" border="none" type="number" placeholder="请输入">
              <template slot="suffix">元</template>
            </u-input>
          </u-form-item>
          <u-form-item label="货物件数">
            <u-input v-model="form.goodsPiece" border="none" type="number" placeholder="请输入，选填" />
          </u-form-item>
          <u-form-item label="货物吨数" required>
            <u-input v-model="form.goodsWeight" border="none" type="number" placeholder="请输入" />
          </u-form-item>
          <u-form-item label="货物方数">
            <u-input v-model="form.goodsVolume" border="none" type="number" placeholder="请输入，选填" />
          </u-form-item>
        </view>
      </u-form>
    </template>
    <template #footer>
      <u-button type="primary" @tap="submit">确认修改</u-button>
    </template>
  </Page>
</template>

<script>
import { urlParam } from '@/utils/param';
export default {
  name: 'change-goods',
  data() {
    return {
      form: {
        goodsName: '',
        goodsValue: '',
        goodsPiece: '',
        goodsWeight: '',
        goodsVolume: '',
      },
      item: {},
      currentFreight: {},
    };
  },
  onLoad(options) {
    if (options.code) {
      this.initData(options.code);
    }
  },
  computed: {
    showGoodsValue() {
      return this.currentFreight ? this.currentFreight.driverSecurityServiceFlag || this.currentFreight.securityServiceFlag : false;
    },
  },
  methods: {
    initData(code) {
      uni.$u.api.freightOrder.currentFreight({}).then(en => {
        this.currentFreight = en?.result || {};
      });
      uni.$u.api.freightOrder.getDetail({ code }).then(res => {
        this.item = res.result || {};
        this.form = {
          goodsName: this.item.goodsName,
          goodsValue: this.item.goodsValue,
          goodsPiece: this.item.goodsPiece,
          goodsWeight: this.item.goodsWeight,
          goodsVolume: this.item.goodsVolume,
        };
      });
    },
    choseGoodsName() {
      uni.$once('select-common', option => {
        this.form.goodsName = option.name;
      });
      uni.navigateTo({
        url: `/pages/global/search-common${urlParam({
          mode: 'select',
          url: 'goodsName',
          title: '货物名称',
          label: 'name',
          value: '',
        })}`,
      });
    },
    submit() {
      uni.$u.api.freightOrder.modifyGoods({ code: this.item.code, ...this.form }).then(res => {
        if (res.success) {
          uni.showToast({ title: '操作成功', icon: 'none' });
          this.getOpenerEventChannel().emit('refreshData');
          setTimeout(() => uni.navigateBack(), 500);
        }
      });
    },
  },
};
</script>

<style lang="scss">
.page-change-goods {
  &__footer {
    background: $color-white;
  }
  .card {
    color: #999999;
    background-color: $color-white;
    border-radius: $radius-md;
    box-shadow: $shadow-normal;
    .u-form-item {
      padding-left: $padding-md !important;
      padding-right: $padding-sm !important;
    }
    .u-form-item + .u-form-item {
      border-top: 1px solid #f6f6f6;
    }
  }
}
</style>
