<template>
  <a-modal
    centered
    class="addProvincialWarehouse-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancel="isShow=false"
    :width="500">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 表单 -->
      <a-form-model
        ref="ruleForm"
        id="addProvincialWarehouse-form"
        :model="form"
        :rules="zoneRules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
        :hideRequiredMark="true"
      >
        <a-form-model-item label="设为省仓" prop="serviceCenterName">
          <custList
            id="addProvincialWarehouse-dealerName"
            cooperateFlag="1"
            dealerLevel="PROVINCE"
            placeholder="请输入经销商名称搜索"
            ref="indirectDealer"
            @change="dealerChange" />
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button id="addProvincialWarehouse-modal-back" @click="isShow = false" >取消</a-button>
        <a-button type="primary" style="margin-left: 15px;" id="addProvincialWarehouse-modal-save" @click="handleSave">保存</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
// 组件
import custList from '@/views/common/custList.vue'
// 接口
import { serviceCenterAdd } from '@/api/serviceCenter'
export default {
  name: 'AddProvincialWarehouseModal',
  components: { custList },
  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: {
        serviceCenterSn: undefined, // 经销商sn
        serviceCenterName: undefined// 经销商名称
      },
      // 表单规则验证
      zoneRules: {
        serviceCenterName: [
          { required: true, message: '请输入经销商名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 选择经销商
    dealerChange (val) {
      this.form.serviceCenterSn = val.key
      this.form.serviceCenterName = val.key ? (val.label.replace('\n', '')).trim() : ''
    },
    //  保存
    handleSave () {
      const _this = this
      _this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(_this.form))
          _this.spinning = true
          serviceCenterAdd(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.indirectDealer.resetForm()
        this.form.serviceCenterSn = undefined
        this.form.serviceCenterName = undefined
      }
    }
  }
}
</script>

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