/*
 *@description:查看
 *@author: hudw
 *@date: 2022-04-26 16:41:46
*/
<template>
  <p-card class="full-card-panel" :bordered="false" :loading="loadingPage">
    <a-space slot="extra" v-show="!loadingPage">
      <p-button type="primary" :icon="load ? 'loading' : ''" @clickSync="$refs.form.save(0)">保存</p-button>
      <p-button type="primary" @clickSync="$refs.form.save(1)">保存并核算</p-button>
      <p-button @click="$refs.form.back(record && record.id, false)">取消</p-button>
    </a-space>
    <a-space slot="title">
      <a-icon
        type="arrow-left"
        @click="
          $refs.form.back(record && record.id, false)
        "
        :style="{ color: '#08c' }"
      />
      {{ title }}
    </a-space>
    <formContent ref="form" v-bind="$attrs"  v-on="$listeners" :st="st" :formRecord="dataView" @loading="(v) => (load = v)"></formContent>
    <slot></slot>
  </p-card>
</template>
<script>
import formContent from '../formDetails/formContent.vue'
import { mainViewUrl } from '../api.js'

const titleArr = ['', '查看', '新建', '修改', '复制']
const pageName = {
  main: '订单信息',
  children: '商品信息',
}
export default {
  inheritAttrs: false,
  components: { formContent },
  data() {

    return {
      ...this.$attrs,
      load: false,
      loadingPage: false,
      dataView: null,
      shipbillId: '',
    }
  },
  props: {
    url: {
      type: String,
      default: mainViewUrl
    },
    record: {
      type: null,
    },
    st: {
      type: Number,
      default: 0,
    },
  },
  mounted() {

  },
  watch: {
    st(value) {
      this.dataView = null
      if (value === 2) {
        //新增
        this.$refs.form.init(2, null)
      }
    },
    record(value) {
      if (this.$el.style.display == 'none') return

      if (this.st > 0) {
        if (!value) {
          this.shipbillId = ''
          this.$refs.form.init(1, value)
          return
        }
        this.getView(value.id)
      }
    },
  },
  computed: {
    title() {
      let { formName } = this.$attrs
      return titleArr[this.st] + pageName[formName]
    },
  },
  methods: {
    loading(is) {
      this.load = is
    },
    form() {
      let { st, formName } = this.$attrs
      if (this.$refs.form.$refs[formName]) {
        return this.$refs.form.$refs[formName].form
      }
      return undefined
    },
    getView(shipbillId) {
      this.loadingPage = true
      let _this = this
      return this.axios({ url: this.url, method: 'post', params: { id: shipbillId } })
        .then((response) => {
          if (response.success) {
            this.shipbillId = shipbillId
            this.dataView = response.data
          }
        })
        .finally(() => {
          this.loadingPage = false
        })
    },
    reload(args) {
      return this.$refs.form.$refs.table.reload(args)
    },

  },
}
</script>
