<template>
  <div class="role-edit">
    <handle-top>
      <save slot="btn" type="save" @click.native="save()"></save>
    </handle-top>
    <role-info-handle ref="info"></role-info-handle>
    <role-power-select ref="power"></role-power-select>
  </div>
</template>

<script>
import HandleTop from '@/components/handle-top/HandleTop'
import roleInfoHandle from '@/SManagement/user/role-edit/components/roleInfoHandle'
import RolePowerSelect from '@/SManagement/user/role-edit/components/RolePowerSelect'
import save from '@/SManagement/archives_upload/components/save'
export default {
  name: 'roleEdit',
  data () {
    return {
      //角色新建
      add: {
        "classname": "sale.role.role",
        "method": "insertormodify_role",
        "content": {
          "roleid": 0,
          "rolename": "",
          "remarks": ""
        }
      },
      roleid: parseInt(window.sessionStorage.getItem('currentEditRole')) == 0 ? 0 : JSON.parse(window.sessionStorage.getItem('currentEditRole')).roleid,
      editData: parseInt(window.sessionStorage.getItem('currentEditRole')) == 0 ? 0 : JSON.parse(window.sessionStorage.getItem('currentEditRole')),
      allPower: []
    };
  },
  components: { roleInfoHandle, HandleTop, save, RolePowerSelect },
  computed: {
  },
  watch: {
  },
  created () {
    this.init()
  },
  methods: {
    //初始化
    init () {
      if (this.editData) {
        this.$nextTick(() => {
          this.$refs.info.ruleForm.name = this.editData.rolename
          this.$refs.info.ruleForm.descript = this.editData.remarks
        })
      }
    },
    //计算选中的所有权限
    getAllPower () {
      this.$refs.power.tempPowerList.forEach(item => {
        if (item.optionids.length < 1) return
        let result = this.allPower.find(item2 => item2.systemappid == item.systemappid)
        if (result == undefined) {
          let obj = {}
          obj.systemappid = item.systemappid
          obj.optionids = item.optionids
          this.allPower.push(obj)
        } else {
          result.optionids = result.optionids
        }
      })
    },
    async save () {
      this.add.content.rolename = this.$refs.info.ruleForm.name      
      this.add.content.remarks = this.$refs.info.ruleForm.descript
      this.getAllPower()
      this.$refs.info.$refs.ruleForm.validate(async val => {
        if (val) {
          if (this.editData != 0) {
            this.add.content.roleid = this.editData.roleid
            let res = await this.$api.requested(this.add)
            if(res.code == 1) {
              let res = await this.$api.requested({
                "classname": "sale.role.role",
                "method": "add_appauth",
                "content": {
                  "roleid": this.roleid,
                  "systemapps": this.allPower
                }
              })
              this.tool.showMessage(res, () => {
                this.$router.back()
              })
            }
          } else {
            let res = await this.$api.requested(this.add)
            if (res.code == 1) {
              let res2 = this.$api.requested({
                "classname": "sale.role.role",
                "method": "add_appauth",
                "content": {
                  "roleid": res.data.roleid,
                  "systemapps": this.allPower
                }
              })
              this.tool.showMessage(res2, () => {
                this.$router.back()
              })
            }
          }
        }
      })
    }
  },
};
</script>

<style scoped>
.role-edit {
  min-height: 100%;
}
.power1 {
  background: #ffffff;
  min-height: calc(100vh - 320px);
}
.title-box {
  height: 48px;
  background: #ffffff;
  line-height: 48px;
  padding-left: 16px;
}
.title-box .title {
  font-size: 14px;
  font-weight: bold;
  color: #333333;
  margin-bottom: 20px;
  height: 48px;
  width: 100%;
}
</style>
