<template>
  <a-modal
    centered
    class="marketingDivisionSetEdit-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancel="isShow=false"
    :width="500">
    <!-- 表单 -->
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="marketingDivisionSetEdit-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="区域名称" prop="subareaName">
            <a-input
              id="marketingDivisionSetEdit-subareaName"
              :maxLength="30"
              v-model.trim="form.subareaName"
              @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-spin>
    </div>
  </a-modal>
</template>

<script>
// 接口
import { subareaSaveSubarea, subareaQuerySubarea } from '@/api/subarea'
export default {
  name: 'MarketingDivisionSetEditModal',
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemId: {
      type: String,
      default: ''
    }
  },
  computed: {
    // 弹窗标题
    modalTit () {
      return (this.itemId ? '编辑' : '新增') + '区域'
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      // 表单label布局
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      // 提交数据
      form: {
        subareaName: '' // 区域名称
      },
      // 表单验证规则
      rules: {
        subareaName: [
          { required: true, message: '请输入区域名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 区域名称
    filterEmpty () {
      let str = this.form.subareaName
      str = str.replace(/\ +/g, '')
      str = str.replace(/[ ]/g, '')
      str = str.replace(/[\r\n]/g, '')
      this.form.subareaName = str
    },
    // 获取详情数据
    getDetail () {
      this.spinning = true
      subareaQuerySubarea({ subareaSn: this.itemId }).then(res => {
        if (res.status == 200) {
          this.form.subareaName = res.data[0].subareaName
        }
        this.spinning = false
      })
    },
    //  保存
    handleSave () {
      const _this = this
      _this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(_this.form))
          // 编辑
          if (this.itemId) {
            formData.subareaSn = this.itemId
          }
          _this.spinning = true
          subareaSaveSubarea(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.subareaName = ''
      } else {
        if (this.itemId) {
          this.getDetail()
        }
      }
    }
  }
}
</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>
