<template>
  <a-modal
    centered
    class="user-modal"
    :footer="null"
    :maskClosable="false"
    :title="title"
    v-model="isShow"
    @cancel="isShow=false"
    :width="600">
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
        >
          <a-form-model-item label="用户名称">
            {{ form.name }}
          </a-form-model-item>
          <a-form-model-item label="所属营销分区" prop="subareaSnList">
            <!-- <subarea id="user-subarea" v-model="form.subareaSn"></subarea> -->
            <a-select
              placeholder="请选择分区"
              allowClear
              mode="multiple"
              v-model="form.subareaSnList"
              :showSearch="true"
              :filter-option="filterOption">
              <a-select-option v-for="item in subareaList" :key="item.subareaSn" :value="item.subareaSn">{{ item.subareaName }}</a-select-option>
            </a-select>
          </a-form-model-item>
          <div style="display: flex;justify-content: center;padding: 30px 0;">
            <a-button type="primary" @click="onSubmit">确定</a-button>
            <a-button style="margin-left: 15px;" @click="cansel">取消</a-button>
          </div>
        </a-form-model>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { VSelect } from '@/components'
import { userSubareaSave } from '@/api/power-user'
import { subareaQueryAll } from '@/api/subarea'
import subarea from '@/views/common/subarea.js'
export default {
  name: 'UpdateArea',
  mixins: [commonMixin],
  components: { VSelect, subarea },
  props: {
    openUdModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      isShow: this.openUdModal, //  是否打开弹框
      spinning: false,
      title: '分区设置',
      labelCol: { span: 5 },
      wrapperCol: { span: 16 },
      other: '',
      form: {
        name: '',
        subareaSnList: undefined
      },
      rules: {
        subareaSnList: [
          { required: true, message: '请选择所属营销分区', trigger: 'change' }
        ]
      },
      nowData: undefined,
      subareaList: []
    }
  },
  methods: {
    setData (row) {
      this.form.id = row.id
      this.form.name = row.name
      this.form.subareaSnList = row.subareaSnList || undefined
      this.nowData = row
    },
    onSubmit () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.submitForm()
        } else {
          return false
        }
      })
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    getSubareaList () {
      const _this = this
      subareaQueryAll().then(res => {
        if (res.status == 200) {
          _this.subareaList = res.data || []
        } else {
          _this.subareaList = []
        }
      })
    },
    submitForm () {
      const _this = this
      const params = {
        userSn: _this.nowData.sn,
        subareaSnList: _this.form.subareaSnList
      }
      _this.spinning = true
      userSubareaSave(params).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          _this.isShow = false
          _this.$emit('ok')
          _this.spinning = false
        } else {
          _this.spinning = false
        }
      })
    },
    cansel () {
      this.$emit('close')
      this.$refs.ruleForm.resetFields()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openUdModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        this.getSubareaList()
      }
    }
  }
}
</script>

<style lang="less">
  .merchantInfoManagement-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
  }
</style>
