<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">
        <view class="scroll-content">
          <wk-form
            ref="form"
            :fields="fieldArr"
            :batch-id="batchId"
            @change="handleValueChange" />
            
          <view class="empty-box" />
        </view>
      </view>
      
      <!-- #ifdef MP-WEIXIN -->
      <view class="footer-btn-group">
        <button class="button" @click="handleSave">
          保存
        </button>
      </view>
      <!-- #endif -->
    </view>
  </view>
</template>

<script>
  import {
    QueryFieldList,
    AddCustomer,
    UpdateCustomer
  } from 'API/crm/customer'
  
  import CreateMixins from '../mixins/create.js'
  
  export default {
    name: 'CreateCustomer',
    mixins: [CreateMixins],
    data() {
      return {
        moduleType: 'customer',
      }
    },
    onLoad() {
      this.getFieldList()
    },
    methods: {
      getFieldList() {
        const params = { type: 1 }
        if (this.id) params.id = this.id
        QueryFieldList(params)
          .then(res => {
            res.forEach(field => {
              // 自动编号为非必填
              if (field.autoGeneNumber === 1) {
                field.isNull = 0
              }
              
              if (!this.$isEmpty(field.authLevel)) {
                // 判断字段权限，如果没有权限则直接禁止修改
                field.disabled = this.getDisabledStatusByAuth(field)
                if (field.disabled) {
                  if (field.sys_config) {
                    field.sys_config.disabledMsg = ''
                  }
                }
              }
              
              field.value = this.mixinsFormatFieldValue(field)
            })
            
            this.fieldArr = res
            this.setForm()
          })
          .catch(() => {
          })
      },
      
      /**
       * 表单值发生改变
       * @param {Object} data
       */
      handleValueChange(data) {
        // console.log('value change: ', data)
      },
      
      /**
       * 保存
       */
      handleSave() {
        this.loading = true
        this.$refs.form.getForm().then(async form => {
          // if (this.id == form.entity.superiorCustomerId) {
          //   this.$toast('上级客户不能为当前客户')
          //   this.loading = false
          //   return
          // }
          
          this.baseFormatSaveData(form)
          console.log('save: ', form)
          // this.loading = false
          // return

          const request = this.id ? UpdateCustomer : AddCustomer
          request(form).then(() => {
            this.$toast(this.id ? '修改成功' : '添加成功')
            this.$refreshAndToPrev(this)
          }).catch(() => {
            this.loading = false
          })
        }).catch(() => {
          this.loading = false
        })
      }
    }
  }
</script>

<style scoped lang="scss">
  @import '../style/create.scss';
</style>
