<template>
  <div class="customerManagementEdit-wrap">
    <a-page-header :ghost="false" :backIcon="false" v-if="model=='page'" class="customerManagementEdit-back">
      <!-- 自定义的二级文字标题 -->
      <template slot="subTitle">
        <a id="customerManagementEdit-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
      </template>
    </a-page-header>
    <a-spin :spinning="spinning" tip="Loading...">
      <a-card size="small" :bordered="false" class="customerManagementEdit-table-page-wrapper">
        <a-form-model
          id="customerManagementEdit-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-row :gutter="15">
            <a-col :span="12">
              <a-form-model-item label="客户名称" prop="customerName" :labelCol="{ span: 6}" :wrapperCol="{ span: 16 }">
                <a-input
                  id="customerManagementEdit-customerName"
                  :maxLength="30"
                  v-model.trim="form.customerName"
                  @change="filterEmpty"
                  placeholder="请输入客户名称(最多30个字符)"
                  allowClear />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="联系电话" prop="contactTel" :labelCol="{ span: 6}" :wrapperCol="{ span: 16 }">
                <a-input id="customerManagementEdit-contactTel" :maxLength="15" v-model.trim="form.contactTel" placeholder="请输入联系电话(最多15个字符)" allowClear />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="联系手机" prop="contactMobile" :labelCol="{ span: 6}" :wrapperCol="{ span: 16 }">
                <a-input id="customerManagementEdit-contactMobile" :maxLength="11" v-model.trim="form.contactMobile" placeholder="请输入联系手机(最多11个字符)" allowClear />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="联系人" prop="contactName" :labelCol="{ span: 6}" :wrapperCol="{ span: 16 }">
                <a-input id="customerManagementEdit-contactName" :maxLength="30" v-model.trim="form.contactName" placeholder="请输入联系人(最多30个字符)" allowClear />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="15">
            <a-col :span="24">
              <a-form-model-item label="客户地址" prop="countySn" :labelCol="{ span: 3 }" :wrapperCol="{ span: 20 }">
                <AreaList changeOnSelect ref="areaList" v-model="areaVal" @change="areaChange"></AreaList>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="详细地址" prop="customerAddr" :labelCol="{ span: 3 }" :wrapperCol="{ span: 20 }">
                <a-input id="customerManagementEdit-customerAddr" :maxLength="60" v-model.trim="form.customerAddr" placeholder="请输入详细地址(最多60个字符)" allowClear />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="15">
            <a-col :span="8">
              <a-form-model-item label="客户传真" prop="fax">
                <a-input id="customerManagementEdit-fax" :maxLength="30" v-model.trim="form.fax" placeholder="请输入客户传真(最多30个字符)" allowClear />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="配送方式" prop="dispatchType">
                <v-select
                  code="DISPATCH_TYPE"
                  id="customerManagementEdit-dispatchType"
                  v-model="form.dispatchType"
                  allowClear
                  placeholder="请选择配送方式"
                ></v-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <!-- <a-form-model-item label="是否卫星仓客户" prop="satelliteFlag">
                <span style="padding-left: 11px;">{{ form.satelliteFlag == 1 ? '是' : '否' }}</span>
              </a-form-model-item> -->
              <!--  <a-form-model-item label="是否为下级" prop="satelliteFlag">
                <span style="padding-left: 11px;">{{ form.satelliteFlag == 1 ? '是' : '否' }}</span>
              </a-form-model-item> -->
              <a-form-model-item label="客户关系" prop="relationTypeDictValue">
                <span style="padding-left: 11px;">{{ form.relationTypeDictValue }}</span>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="15">
            <a-col :span="8">
              <a-form-model-item label="价格类型" ref="priceType" prop="priceType">
                <v-select code="PRICE_TYPE" id="customerManagementEdit-priceType" v-model="form.priceType" allowClear placeholder="请选择价格类型" ></v-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="收款方式" prop="settleStyleSn">
                <a-select id="customerManagementEdit-settleStyleSn" v-model="form.settleStyleSn" allowClear placeholder="请选择收款方式">
                  <a-select-option v-for="item in settleStyleList" :value="item.settleStyleSn" :key="item.settleStyleSn">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="客户类型" prop="customerTypeSn">
                <custType id="customerManagementEdit-customerTypeSn" v-model="form.customerTypeSn" allowClear placeholder="请选择客户类型"></custType>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-form-model-item :wrapper-col="{ span: 12, offset: 6 }" style="text-align: center;margin-top: 30px;">
            <a-button type="primary" @click="handleSubmit" size="large" id="customerManagementEdit-btn-submit" style="padding: 0 60px;">保存</a-button>
          </a-form-model-item>
        </a-form-model>
      </a-card>
    </a-spin>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import custType from '@/views/common/custType.js'
import AreaList from '@/views/common/areaList.js'
import { custSave, custFindById, settleStyleFindAllList } from '@/api/customer'
export default {
  name: 'CustomerManagementEdit',
  components: { STable, VSelect, custType, AreaList },
  mixins: [commonMixin],
  props: {
    model: {
      type: String,
      default: 'page'
    }
  },
  data () {
    return {
      spinning: false,
      formItemLayout: {
        labelCol: { span: 9 },
        wrapperCol: { span: 14 }
      },
      form: {
        id: null,
        customerName: '', // 客户名称
        contactTel: '', //  联系电话
        contactMobile: '', //  联系手机
        contactName: '', //  联系人
        provinceSn: undefined, //  省
        provinceName: '',
        citySn: undefined, // 市
        cityName: '',
        countySn: undefined, // 区
        countyName: '',
        customerAddr: '', //  详细地址
        fax: '', //  客户传真
        dispatchType: '', //  配送方式
        satelliteFlag: 0, //  是否卫星仓客户
        relationTypeDictValue: undefined, // 客户关系
        customerTypeSn: undefined, //  客户类型
        priceType: '', //  价格类型
        settleStyleSn: undefined //  收款方式
      },
      areaVal:[],
      rules: {
        customerName: [{ required: true, message: '请输入客户名称', trigger: 'blur' }],
        contactMobile: [{ required: false, pattern: /^\d{11}$/, message: '请输入正确的手机号', trigger: 'blur' }],
        countySn: [{ required: true, message: '请选择客户地址', trigger: 'change' }],
        priceType: [{ required: true, message: '请选择价格类型', trigger: 'change' }],
        settleStyleSn: [{ required: true, message: '请选择收款方式', trigger: 'change' }]
      },
      addrProvinceList: [], //  省下拉
      addrCityList: [], //  市下拉
      addrDistrictList: [], //  区下拉
      settleStyleList: [] //  收款方式  下拉数据
    }
  },
  methods: {
    filterEmpty () {
      let str = this.form.customerName
      str = str.replace(/\ +/g, '')
      str = str.replace(/[ ]/g, '')
      str = str.replace(/[\r\n]/g, '')
      this.form.customerName = str
    },
    areaChange (val) {
      this.form.provinceSn = val[0] ? val[0] : undefined
      this.form.citySn = val[1] ? val[1] : undefined
      this.form.countySn = val[2] ? val[2] : undefined
    },
    //  获取客户信息
    getDetail () {
      custFindById({ id: this.$route.params.id }).then(res => {
        if (res.status == 200) {
          res.data.satelliteFlag = res.data.dealerFlag
          this.form = Object.assign(this.form, res.data)
          this.areaVal = [res.data.provinceSn,res.data.citySn,res.data.countySn]
        } else {
          this.$refs.ruleForm.resetFields()
        }
      })
    },
    // 收款方式
    getSettleStyleList () {
      settleStyleFindAllList().then(res => {
        if (res.status == 200) {
          this.settleStyleList = res.data
        } else {
          this.settleStyleList = []
        }
      })
    },
    //  保存
    handleSubmit (e) {
      e.preventDefault()
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.stringify(_this.form)
          const formInfo = JSON.parse(form)
          delete formInfo.satelliteFlag
          formInfo.id = this.$route.params.id && this.model == 'page' ? this.$route.params.id : null
          _this.spinning = true
          custSave(formInfo).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              if (_this.model == 'page') {
                setTimeout(() => {
                  _this.handleBack()
                }, 600)
              } else {
                _this.$emit('ok', res.data)
              }
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/customerManagement/customerInfo/list' })
    },
    // 获取基础数据
    getBaseData () {
      this.getSettleStyleList() // 收款方式
      this.$refs.ruleForm.resetFields()
    },
    initPage () {
      this.getBaseData()
      if (this.$route.params.id) {
        //  编辑页
        this.getDetail()
      }
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.initPage()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.initPage()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
.customerManagementEdit-wrap {
  height: 100%;
  >.ant-spin-nested-loading{
    height: calc(100% - 52px);
	overflow-y: auto;
    .ant-spin-container{
      height: 97%;
    }
  }
	.customerManagementEdit-back {
		margin-bottom: 10px;
	}
  .customerManagementEdit-table-page-wrapper{
    height: 100%;
  }
}
</style>
