<template>
  <view class="uni-app">
    <view class="status-bar" />
    <view class="main-container">
      <wk-nav-bar :title="navTitle">
        <!-- #ifndef MP-WEIXIN -->
        <button class="button white-btn" @click="handleSave">
          确定
        </button>
        <!-- #endif -->
      </wk-nav-bar>

      <view class="container">
        <wk-form
          ref="form"
          :fields="fieldArr" />
      </view>

      <!-- #ifdef MP-WEIXIN -->
      <view class="footer-btn-group">
        <button class="button" @click="handleSave">
          确定
        </button>
      </view>
      <!-- #endif -->
    </view>
  </view>
</template>

<script>
  import {
    ResetInvoiceStatus,
    UpdateInvoiceStatus
  } from '@/api/crm/invoice.js'

  import Fields from '@/utils/fields.js'

  export default {
    name: 'UpdateStatus',
    data() {
      return {
        loading: true,
        routerQuery: {},

        fieldArr: [
          new Fields({ fieldName: 'invoiceNumber', name: '发票号码', formType: 'text' }),
          new Fields({ fieldName: 'realInvoiceDate', name: '实际开票日期', formType: 'date' }),
          new Fields({ fieldName: 'logisticsNumber', name: '物流单号', formType: 'text' })
        ]
      }
    },
    computed: {
      navTitle() {
        if (
          this.routerQuery &&
          Number(this.routerQuery.status) === 1
        ) {
          return '重置开票信息'
        }
        return '标记为已开票'
      }
    },
    onLoad(options) {
      this.routerQuery = options || {}
    },
    methods: {
      handleSave() {
        this.$refs.form.getForm().then(data => {
          console.log('save: ', data.entity)
          const params = data.entity || {}
          params.invoiceId = this.routerQuery.id

          const status = Number(this.routerQuery.status)
          const request = status === 1 ? ResetInvoiceStatus : UpdateInvoiceStatus
          request(params).then(res => {
            this.$refreshAndToPrev(this)
          }).catch(() => {})
        }).catch(() => {})
      }
    }
  }
</script>

<style scoped lang="scss">
</style>
