/*
 *@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" v-action:saveSubmit>保存</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>
    <div v-for="(item, key) in mainGetFormList" :key="key">
      <p-grid-form selfUpdate :class="st === 1 && 'p-grid-view'"  v-show="formName === key" :ref="key" :formName="key"
        :items="item.formItems(vm,key==='main'&&formExtendedField)" :span="6" :labelCol="{ span: 6 }" :wrapperCol="{ span: 16 }"></p-grid-form>
    </div>
  </p-card>
</template>
<script>
const titleArr = ['','查看' ,'新建', '修改','复制']

const findStr = 'pickUpSign,autoPickUpSign,pickUpPicture,deliverySign,autoDeliverySign,deliveryPicture'
import { mainGetFormList } from './config.js';

import { mainSaveData, childrenSaveData } from '../../api';

const pageName = {
  main: '供应商',
  children: '发货商'
}
export default {

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

      let form = this.form()
      form.resetFields();
      let provinceCityDistrict = []
      if (value === 3) {
        form.setFieldsValue({ provinceCityDistrict })
      }
      if (this.record) {
        const { province, provinceName, city, cityName, district, districtName } = this.record;
        provinceCityDistrict = [province, city, district]

        if (value === 3) {
          //修改
        } else if (value === 1) {
          //查看
          provinceCityDistrict = [provinceName, cityName, districtName].filter((item) => item && item);
          provinceCityDistrict = provinceCityDistrict.join('-');
        } else if (value === 4) {
          this.record.id = ''
          this.record.code = ''
          this.record.name = ''
          this.record.shortName = ''
        }
        form.setFieldsValue({ ...this.record, provinceCityDistrict})
      }

    },
    // 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][0].form.getFieldsValue()
    },
    form() {
      return this.$refs[this.formName][0].form
    },
    save() {
      let form = this.form()
      form.validateFields((error, value) => {
        if (error) return
        delete value.provinceCityDistrict

        let fn = {
          main: mainSaveData,
          children: childrenSaveData
        }
        fn[this.formName](this, value, this.st).then((res) => {
          if (res.success) {
            setTimeout(() => {
              this.back(res.data || value.id,true)
            }, 1000)
          }

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