/*
 *@description:
 *@author: hudw
 *@date: 2022-03-31 17:14:12
*/
<template>
  <p-card class="full-card-panel" v-show="st > 0" :bordered="false">
    <!-- v-action:saveSubmit -->
    <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 selfUpdate :ref="formName" :class="st === 1 && 'p-grid-view'"
        :items="formItems" :span="6" :labelCol="{ span: 6 }" :wrapperCol="{ span: 16 }">
    </p-grid-form>
    <childrenTable ref='childrenTable' style="margin-top: 20px"  :record="record" :st="st" />
  </p-card>
</template>
<script>
const titleArr = ['','查看' ,'新建', '修改','复制']

import {setFormList } from './config.js';

import { mainSaveData, childrenSaveData } from '../../api';
import childrenTable from '../childrenTable/index.childrenTable.vue' /**明细表 */

const pageName = {
  main: '站点',
  children: '发货商'
}
export default {
  components: {childrenTable },

  props: {
    options: {//字典
      type: Object
    },
    record: {
      //当前行数据
      type: Object,
    },
    mainId: {
      //主表ID
      type: null,
    },
    formName: { //主表：main，表细表：children
      type: null,
      default:'main'
    },
    st: {
      type: Number,
      default: 0  /**0=主页,1=查看,2=新建,3=修改,4=复制 */
    },
     formExtendedField: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      vm: this,
      confirmLoading: false,
      formItems: setFormList(this),
      startingStationDTOList: [],//始发站点

    }
  },
  watch: {
    st(value) {
      let form = this.form()

      form.resetFields();
      if (this.record) {
        const { administrativeAreas, provinceName, city, cityName, district, districtName } = this.record;
        if (value === 3) {
          //修改

        } else if (value === 1) {
          //查看
        }
        form.setFieldsValue({ ...this.record})
      }
    },
    // formExtendedField(value) {
    //   if (value.length > 0) {
    //     this.formItems = setFormItems(this, value)
    //   }
    // },
  },
  computed: {
    formList() {
      return this[this.formName + 'GetFormList']
    },
    title() {
      return titleArr[this.st] + pageName[this.formName]
    }
  },
  mounted() {


  },
  methods: {

    getFieldsValue() {
      return this.$refs[this.formName].form.getFieldsValue()
    },
    form() {
      return this.$refs[this.formName].form
    },
    save() {
      let form = this.form()
      form.validateFields((error, value) => {
        if (error) return

      //  value.startingStationDTOList = this.$refs.childrenTable.getData();
      //  value.administrativeAreas = this.administrativeAreas.join(",")
    //    value.administrativeAreasFather=this.administrativeAreasFather.join(",")
        value = {...value,...this.$refs.childrenTable.getData()}


        mainSaveData(this, value, this.st).then((res) => {
          if (res.success) {
            setTimeout(() => {
              this.back(res.data || value.id,true)
            }, 1000)
          }

        })
      })
    },
    back(id, isReload = false) {
      this.$refs.childrenTable.clearAll()

      this.$emit('back', this.formName, id,isReload)
    },
  },
};
</script>
