/*
 *@description:
 *@author: hudw
 *@date: 2022-03-31 17:14:12
*/
<template>
  <p-card class="full-card-panel" v-show="st > 0" :bordered="false">
    <a-space slot="extra" v-if="st !== 1">
      <p-button type="primary" :icon="confirmLoading ? 'loading' : ''" @click="save">保存</p-button>
      <p-button @click="back">取消</p-button>
    </a-space>
    <a-space slot="title">
      <a-icon type="arrow-left" @click="back(record && record.id)" :style="{ color: '#08c' }" />
      {{ title }}
    </a-space>
    <p-grid-form ref="grid" selfUpdate :items="formItems" :span="6"> </p-grid-form>
  </p-card>
</template>
<script>
const titleArr = ['', '查看', '新建', '修改', '复制']

import { setFormItems } from './config.js'

import { mainSaveData } from '../../api'

export default {
  props: {
    options: {
      //字典
      type: Object,
    },
    record: {
      //当前行数据
      type: Object,
    },
    mainId: {
      //主表ID
      type: null,
    },
    formName: {
      //主表：main，表细表：children
      type: 'main',
    },
    formExtendedField: {
      type: Array,
      default: () => [],
    },
    st: {
      type: Number,
      default: 0 /**0=不显示,1=查看,2=新建,3=修改,4=复制 */,
    },
  },
  data() {
    return {
      affiliationType: 1,

      vm: this,
      confirmLoading: false,
      list: [],
      formItems: [],
    }
  },
  watch: {
    st(value) {
      /**0=不显示,1=查看,2=新建,3=修改,4=复制 */
      let form = this.form()
      form.resetFields()

      if (this.record) {
        const { province, provinceName, city, cityName, district, belongTo } = this.record
        this.record.belongTo = ~~belongTo

        if (value === 1) {

        } else if (value === 2) {
          //新建
        } else if (value === 4) {
          //复制
          return form.setFieldsValue({ ...this.record})
        }
        let department = this.record.departmentId.split(',')
        department= department.map((value, index) => {
          return { value, label: this.record.departmentName.split(',')[index] }
        }, this)
        form.setFieldsValue({ ...this.record, department })
      }
    },
    formExtendedField(value) {
      if (value.length > 0) {
        this.formItems = setFormItems(this, value)
      }
    },
  },
  computed: {
    formList() {
      return this[this.formName + 'GetFormList']
    },
    title() {
      return titleArr[this.st] + this.$route.meta.title
    },
  },
  mounted() {
    this.formItems = setFormItems(this, this.formExtendedField)
  },
  methods: {
    getFieldsValue() {
      return this.form().getFieldsValue()
    },
    form() {
      return this.$refs.grid.form
    },
    save() {
      let form = this.form()
      form.validateFields((error, value) => {
        if (error) return
        return mainSaveData(this, value, this.st).then((res) => {
          if (res.success) {
            setTimeout(() => {
              this.back(res.data || value.cargoOwnerId, true)
            }, 1000)
          }
        })
      })
    },
    back(id, isReload = false) {
      this.$emit('back', id, isReload)
    },
  },
}
</script>
