  <template>
    <div class="app-container calendar-list-container">
      <el-form :rules="rules" ref="addForm" :model="JlAdminRole" label-position="left" label-width="80px"
                style='width: 100%;'>
        <el-row :gutter="24">
          <el-col :xs="23" :sm="23" :md="12" :lg="8" :xl="8">
            <div class="grid-content bg-purple" style="width: 300px;">
              <el-form-item  :label="$t('rolemanager.rolename')" prop="jr_rolename" style="width: 100%;">
                <div class="block">
                  <el-input v-model="JlAdminRole.jr_rolename"> </el-input>
                </div>
              </el-form-item>
            </div>
          </el-col>
          <el-col :xs="23" :sm="23" :md="11" :lg="8" :xl="8">
            <div class="grid-content bg-purple" style="width: 100%;">
                <el-switch
                  style="margin-top:8px;"
                  v-model="JlAdminRole.jl_visit_my_order"
                  active-text="访问全部订单"
                  inactive-text="只能访问自己的订单">
                </el-switch>
            </div>
          </el-col>
          </el-row>
          <el-row :gutter="24" style="margin-bottom:15px;">
          <el-col :xs="23" :sm="23" :md="11" :lg="16" :xl="16">
          <div align="center">
            <el-button type="primary" :loading ="dialogbuttonloading" @click="updateRole">{{$t('table.save')}}</el-button>
            <el-button @click="resetChecked">{{$t('navbar.reset')}}</el-button>
          </div>
        </el-col>
        </el-row>
          <el-row :gutter="16" style="margin-bottom: 15px;" align="center">
            <el-col :span="16">
            <el-tree :data="data" 
            default-expand-all
            :props="defaultProps" 
            :default-checked-keys="checked"
            node-key="id"
            ref="tree"
            show-checkbox 
            ></el-tree>
            </el-col>
          </el-row>
        </el-form>
    </div>
  </template>
  <script>
    import waves from '@/directive/waves' // 水波纹指令
    import { getRolePermissions, updateAdminRole, getCheckRolePermissions, getOneAdminRole } from '@/api/manager'
    // arr to obj ,such as { CN : "China", US : "USA" }
    export default {
      name: 'roleedit321123',
      directives: {
        waves
      },
      data() {
        return {
          data: [],
          dialogbuttonloading: false,
          checked: [],
          defaultProps: {
            children: 'children',
            label: 'label'
          },
          count: 1,
          tableKey: 0,
          JlAdminRole: {
            id: null,
            jr_rolename: null,
            permissions: null,
            jl_visit_my_order: null
          },
          queryPermissions: {
            roleid: null
          },
          rules: {
            jr_rolename: [{ min: 1, max: 20, message: '请正确录入用户名', trigger: 'blur' }]
          },
          view: {
            name: undefined,
            path: undefined,
            title: undefined
          }
        }
      },
      destroyed() {
        this.view.name = 'roleedit'
        this.view.path = '/user/role_edit'
        this.view.title = 'roleedit'
        this.$store.dispatch('delVisitedViews', this.view)
      },
      created() {
        this.getList()
      },
      methods: {
        getList() {
          this.queryPermissions.roleid = this.$route.params.roleId
          getCheckRolePermissions(this.queryPermissions).then(response3 => {
            this.checked = response3.data.split(',')
          })
          getRolePermissions().then(response4 => {
            this.data = response4.data
          })
          getOneAdminRole(this.queryPermissions).then(response5 => {
            this.JlAdminRole.jr_rolename = response5.data.jr_rolename
            if (response5.data.jl_visit_my_order === 1) {
              this.JlAdminRole.jl_visit_my_order = false
            } else {
              this.JlAdminRole.jl_visit_my_order = true
            }
          })
        },
        updateRole() {
          var parent = this.$refs.tree.getHalfCheckedKeys()
          var children = this.$refs.tree.getCheckedKeys()
          if (this.JlAdminRole.jl_visit_my_order) {
            this.JlAdminRole.jl_visit_my_order = 0
          } else {
            this.JlAdminRole.jl_visit_my_order = 1
          }
          if (this.JlAdminRole.jr_rolename === null || children.length === 0) {
            this.$message({ message: '请正确填写信息！', type: 'warning' })
          } else {
            var temp = ''
            for (var valuep of parent) {
              temp = temp + valuep + ','
            }
            for (var valuec of children) {
              temp = temp + valuec + ','
            }
            this.JlAdminRole.id = this.$route.params.roleId
            this.JlAdminRole.permissions = temp
            this.dialogbuttonloading = true
            updateAdminRole(this.JlAdminRole).then(response3 => {
              this.dialogbuttonloading = false
              if (response3.data !== 1) {
                this.$notify({
                  title: '失败',
                  message: '修改失败！',
                  type: 'warning',
                  duration: 2000
                })
              } else {
                this.getList()
                this.resetChecked()
                this.$notify({
                  title: '成功',
                  message: '修改成功！',
                  type: 'success',
                  duration: 2000
                })
              }
            })
          }
        },
        resetChecked() {
          this.JlAdminRole.jr_rolename = ''
          this.$refs.tree.setCheckedKeys([])
        }
      }
    }
  </script>
  <style>
  .el-form--label-left .el-form-item__label{
    text-align:right
  }
  .el-rate{
    line-height: 0;
    margin-top:8px;
  }
  .el-checkbox.el-checkbox {
      margin-left: 30px;
  }
  </style>