<template>
  <a-modal
    centered
    class="userSyncEdit-modal"
    :footer="null"
    :maskClosable="false"
    title="编辑"
    v-model="isShow"
    @cancle="isShow=false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 表单 -->
      <div>
        <a-form-model
          id="userSyncEdit-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="用户名称">{{ detailData && detailData.name ? detailData.name : '--' }}</a-form-model-item>
          <a-form-model-item label="用户账号">{{ detailData && detailData.loginName ? detailData.loginName : '--' }}</a-form-model-item>
          <a-form-model-item label="用户角色" prop="roleNames">
            <a-select placeholder="请选择角色" id="userSyncEdit-roleNames" allowClear v-model="form.roleNames" mode="multiple">
              <a-select-option v-for="item in roleList" :key="item.id" :value="item.id" :disabled="item.isEnable == '0' ? true : false">{{ item.name }}</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button type="primary" id="userSyncEdit-modal-save" @click="handleSave">保存</a-button>
          <a-button id="userSyncEdit-modal-back" @click="isShow = false" style="margin-left: 15px;">关闭</a-button>
        </div>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { getRoleList, saveUserPower } from '@/api/power-user'
export default {
  name: 'UserSyncModal',
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    nowData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      form: {
        roleNames: []
      },
      rules: {
        roleNames: [ { required: true, message: '请选择用户角色', trigger: 'change' } ]
      },
      roleList: [],
      detailData: null
    }
  },
  methods: {
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(_this.detailData))
          params.roleNames = _this.form.roleNames.join(',')
          params.ownerOrgFlag = _this.nowData && _this.nowData.ownerOrgFlag
          _this.spinning = true
          saveUserPower(params).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('ok')
              setTimeout(function () {
                _this.isShow = false
                _this.spinning = false
              }, 300)
            } else {
              _this.spinning = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 获取角色列表接口
    getRoleList () {
      getRoleList().then(res => {
        if (res.status == 200) {
          this.roleList = res.data
        } else {
          this.roleList = []
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
        this.detailData = null
        this.form.roleNames = []
      } else {
        this.getRoleList()
      }
    },
    nowData: {
      handler (newValue, oldValue) {
        if (this.isShow && newValue) {
          this.detailData = newValue
          if (this.detailData.roleIds) {
            this.form.roleNames = this.detailData.roleIds.split(',')
          } else {
            this.form.roleNames = []
          }
        }
      },
      deep: true
    }
  }
}
</script>

<style lang="less">
  .userSyncEdit-modal{
    .ant-modal-body {
    	padding: 40px 40px 24px;
    }
    .btn-cont {
    	text-align: center;
    	margin: 35px 0 10px;
    }
  }
</style>
