<template>
  <div>
    <el-row>
      <el-form ref="form" :rules="rules" :model="userRole" label-width="100px" :size="$store.getters.size">
        <el-row :gutter="gridNum.row.gutter">
          <el-col :span="12">
            <el-form-item label="所属系统" prop="systemSource">
              <el-select v-model="userRole.systemSource" filterable style="width: 100%" placeholder="请选择" @change="systemSourceChange">
                <el-option
                  v-for="item in roleTypeList"
                  :key="item.typeCode"
                  :label="item.typeName"
                  :value="item.typeCode"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色" prop="roleId">
              <el-select v-model="userRole.roleIds" multiple filterable style="width: 100%" placeholder="请选择">
                <el-option

                  v-for="item in roleList"
                  :key="item.id"
                  :label="item.roleName+(item.belongRole?'('+item.belongRole+')':'(无所属)')"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-col :span="24" style="text-align: right">
          <el-button @click="close">取消</el-button>
          <el-button
            type="primary"
            @click="submit"
          >提交</el-button>
        </el-col>
      </el-form>
    </el-row>
  </div>
</template>
<script>
import sysUserRoleApi from '@/api/auth/sysUserRole'
import oilDicMarkApi from '@/api/dict/oilDicMark'
import oilOmsRoleApi from '@/api/auth/oilOmsRole'

export default {
  props: {
    sysCustomerInfo: {
      type: Object,
      default() {

      }
    }
  },
  data() {
    return {
      roleList: [],
      roleTypeList: [],
      gridNum: {
        row: {
          gutter: 2
        },
        cols: {
          xs: 24,
          sm: 24,
          md: 12,
          lg: 12,
          xl: 6
        }
      },
      userRole: {
        roleIds: undefined,
        systemSource: undefined,
        userId: undefined

      },
      rules: {
        systemSource: [
          { required: true, message: '请选择系统', trigger: 'change' }
        ],
        roleIds: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    // 查询角色类型列表
    oilDicMarkApi.getByType('SYS_GROUP').then(res => {
      this.roleTypeList = res.data
    })
    this.userRole.userId = this.sysCustomerInfo.id
  },
  methods: {
    systemSourceChange(system) { // 系统发送改变
      oilOmsRoleApi.getByRoleType(system, this.userRole.userId).then(res => {
        this.roleList = res.data
      })
    },
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.save()
        }
      })
    },
    save() {
      // 保存
      sysUserRoleApi.save(this.userRole).then(res => {
        this.$message.success(res.msg)
        this.$emit('closeDialog')
        this.$emit('getRoleList')
      })
    },
    close() {
      this.$emit('closeDialog')
    }
  }
}
</script>
