<template>
  <a-modal
    centered
    class="marketingDivisionSetEdit-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancel="isShow=false"
    :width="500">
    <!-- 表单 -->
    <a-form-model
      ref="ruleForm"
      :model="form"
      :rules="zoneRules"
      :label-col="formItemLayout.labelCol"
      :wrapper-col="formItemLayout.wrapperCol"
    >
      <a-form-model-item label="所属区域">
        {{ parentData&&parentData.subareaName }}
      </a-form-model-item>
      <a-form-model-item label="分区名称" prop="subareaAreaName">
        <a-input
          :maxLength="30"
          v-model.trim="form.subareaAreaName"
          @change="filterEmpty"
          placeholder="请输入分区名称(最多30个字符)"
          allowClear />
      </a-form-model-item>
    </a-form-model>
    <div class="btn-cont">
      <a-button id="marketingDivisionSetEdit-modal-back" @click="isShow = false" >取消</a-button>
      <a-button type="primary" style="margin-left: 15px;" id="marketingDivisionSetEdit-modal-save" @click="handleSave">保存</a-button>
    </div>
  </a-modal>
</template>

<script>
import { subareaSaveSubareaArea, subareaQuerySubareaArea } from '@/api/subarea'
export default {
  name: 'MarketingDivisionSetEditModal',
  components: { },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    parentData: {
      type: Object,
      default: function(){
        return null
      }
    }
  },
  computed: {
    modalTit () {
      return (this.parentData&&this.parentData.subareaAreaSn ? '编辑' : '新增') + '分区'
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      form: {
        subareaAreaName: '' // 分区名称
      },
      zoneRules: {
        subareaAreaName: [
          { required: true, message: '请输入分区名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    filterEmpty () {
      let str = this.form.subareaAreaName
      str = str.replace(/\ +/g, '')
      str = str.replace(/[ ]/g, '')
      str = str.replace(/[\r\n]/g, '')
      this.form.subareaAreaName = str
    },
    //  保存
    handleSave () {
      const _this = this
      _this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(_this.form))
          // 编辑
          if (this.parentData.subareaAreaSn) {
            formData.subareaAreaSn = this.parentData.subareaAreaSn
          }
          formData.subareaSn = this.parentData.subareaSn
          // console.log(formData, 'formData')
          _this.spinning = true
          subareaSaveSubareaArea(formData).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('ok')
              _this.isShow = false
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        } else {
          return false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
        this.form.subareaAreaName = ''
      }else{
        if (this.parentData.subareaAreaSn) {
          this.form.subareaAreaName = this.parentData.subareaAreaName
        }
      }
    }
  }
}
</script>

<style lang="less">
  .marketingDivisionSetEdit-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
    .tree-node{
      height: 300px;
      overflow: auto;
      .tree-box{
        .tree-parent{
          display: flex;
          align-items: center;
          border-bottom: 1px solid #eee;
          padding: 0 10px;
          > span{
            margin:0 5px;
            &:first-child{
              flex-grow: 1;
            }
            &:last-child{
              cursor: pointer;
            }
          }
        }
        .tree-child{
          padding: 10px 10px 10px 35px;
          display: flex;
          align-items: center;
          flex-wrap:wrap ;
          > div{
            margin:0 5px;
          }
        }
      }

    }
  }
</style>
