<template>
  <a-modal
    centered
    class="addZone-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancel="isShow=false"
    :width="500">
    <!-- 表单 -->
    <a-form-model
      ref="ruleForm"
      :model="form"
      :rules="zoneRules"
      id="addZone-modal-form"
      :label-col="formItemLayout.labelCol"
      :wrapper-col="formItemLayout.wrapperCol"
    >
      <a-form-model-item label="所属区域" prop="subareaSn">
        <subarea id="chooseDealer-subarea" ref="subarea" showLeve="1" v-model="subareaSn" @change="subareaChange"></subarea>
      </a-form-model-item>
      <a-form-model-item label="分区名称" prop="subareaAreaName">
        <a-input
          id="addZone-modal-subareaAreaName"
          :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="addZone-modal-back" @click="isShow = false" >取消</a-button>
      <a-button type="primary" style="margin-left: 15px;" id="addZone-modal-save" @click="handleSave">保存</a-button>
    </div>
  </a-modal>
</template>

<script>
import { subareaSaveSubareaArea } from '@/api/subarea'
import subarea from '@/views/common/subarea.js'
export default {
  name: 'MarketingDivisionAddZoneModal',
  components: { subarea },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    parentData: {
      type: Object,
      default: function () {
        return null
      }
    }
  },
  computed: {
    // 弹窗标题
    modalTit () {
      return (this.parentData && this.parentData.subareaAreaSn ? '编辑' : '新增') + '分区'
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      // 表单label设置
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      // 提交数据
      form: {
        subareaAreaName: '', // 分区名称
        subareaSn: undefined,
        subareaAreaSn: undefined
      },
      subareaSn: [],
      // 表单验证规则
      zoneRules: {
        subareaAreaName: [
          { required: true, message: '请输入分区名称', trigger: 'blur' }
        ],
        subareaSn: [
          { required: true, message: '请选择区域', trigger: 'change' }
        ]
      }
    }
  },
  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
    },
    subareaChange (val) {
      this.form.subareaSn = val[0] ? val[0] : undefined
    },
    //  保存
    handleSave () {
      const _this = this
      _this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(_this.form))
          // 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 = ''
        this.$refs.subarea.clearData()
        this.subareaSn = []
      } else {
        // 编辑
        if (this.parentData.subareaAreaSn) {
          this.form.subareaSn = this.parentData.subareaSn
          this.subareaSn = [this.form.subareaSn]
          this.form.subareaAreaSn = this.parentData.subareaAreaSn
          this.form.subareaAreaName = this.parentData.subareaAreaName
        } else {
          this.form.subareaSn = this.parentData.subareaSn
          this.subareaSn = [this.form.subareaSn]
          this.form.subareaAreaSn = undefined
        }
      }
    }
  }
}
</script>

<style lang="less">
  .addZone-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>
