  <template>
    <div class="app-container calendar-list-container">
       <el-form label-position="right" label-width="100px"  style='width:100%;'>
        <el-row :gutter="24">
        <el-col :xs="23" :sm="23" :md="12" :lg="6" :xl="6">
          <div class="grid-content bg-purple" >
            <el-form-item  :label="$t('usermanager.username')" >
              <div class="block">
                <el-input style="width: 100%;" class="filter-item"
                  v-model="seachQuery.jr_rolename" :maxlength = 20>
              </el-input>
              </div>
            </el-form-item>
          </div>
        </el-col>
        <el-col :xs="23" :sm="23" :md="12" :lg="12" :xl="12">
          <el-button :loading ="buttonloading" class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilterfirst">
          {{$t('table.search')}}
        </el-button>
        <el-button class="filter-item" type="primary" v-waves icon="el-icon-delete" @click="handleref">
          {{$t('table.cancel')}}
        </el-button>
        <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary"
                  icon="el-icon-edit">{{$t('table.add')}}
          </el-button>
        </el-col>
        </el-row>
       </el-form>
      <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="加载中......" border fit
                highlight-current-row
                style="width: 60%">
        <el-table-column type="index" align="center" :label="$t('table.seq')" width="50">
        </el-table-column>
        <el-table-column align="center" :label="$t('rolemanager.rolename')">
          <template slot-scope="scope">
            <span>{{scope.row.jr_rolename}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" :label="$t('table.actions')" width="268" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <a href="#" @click="hrefuserEdit(scope.row.id)"><el-tag>{{$t('table.edit')}}</el-tag></a> 
            <a href="#" @click="hrefDelete(scope.row.id)"><el-tag>{{$t('table.delete')}}</el-tag></a> 
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                      :current-page="page" :page-sizes="[10,20,30,50]" :page-size="limit"
                      layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
      

      <el-dialog title="Reading statistics" :visible.sync="dialogPvVisible">
        <el-table :data="pvData" border fit highlight-current-row style="width: 100%">
          <el-table-column prop="key" label="Channel"></el-table-column>
          <el-table-column prop="pv" label="Pv"></el-table-column>
        </el-table>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="dialogPvVisible = false">{{$t('table.confirm')}}</el-button>
        </span>
      </el-dialog>
    </div>
  </template>
  <script>
    import { createArticle } from '@/api/article'
    import waves from '@/directive/waves' // 水波纹指令
    import { getAdminRoles, getMktBranches } from '@/api/manager'
    import { queryAdminRoles, delRole } from '@/api/role'
    export default {
      name: 'rolemanager',
      directives: {
        waves
      },
      data() {
        return {
          seachQuery: {
            jr_rolename: null,
            page: 1,
            limit: 10
          },
          userMkt: null,
          mktEnable: null,
          adminroles: [],
          branches: [],
          tableKey: 0,
          list: null,
          total: null,
          page: 1,
          limit: 10,
          buttonloading: false,
          listLoading: true,
          statusOptions: [{ label: '正常', value: '1' }, { label: '停用', value: '0' }],
          downloadLoading: false,
          addAdmininfo: {
            username: undefined,
            name: undefined,
            password: undefined,
            role: undefined,
            status: undefined,
            city: 0
          },
          disableAdmininfo: {
            id: undefined,
            status: 0
          },
          deleteRole: {
            id: undefined
          },
          queryCheckUsername: {
            username: undefined
          },
          rules: {
            username: [{ required: true, min: 1, max: 20, message: '请正确录入用户名', trigger: 'blur' }],
            name: [{ required: true, min: 1, max: 20, message: '请正确录入姓名', trigger: 'blur' }],
            password: [{ required: true, min: 1, max: 20, message: '请正确录入密码', trigger: 'blur' }],
            role: [{ required: true, message: '请正确选择角色', trigger: 'blur' }],
            city: [{ required: true, message: '请正确选择所属门店', trigger: 'blur' }]
          },
          // gogogo
          queryType: 'list',
          showReviewer: false,
          dialogFormVisible: false,
          dialogStatus: '',
          textMap: {
            update: 'Edit',
            create: '添加月嫂'
          },
          dialogPvVisible: false,
          pvData: []
        }
      },
      created() {
        this.userMkt = this.$store.getters.mkt
        if (this.userMkt !== 'ALL') {
          this.seachQuery.city = this.userMkt
          this.addAdmininfo.city = this.userMkt
          this.mktEnable = true
        }
        this.getList()
        this.list = []
        this.listLoading = false
        getAdminRoles().then(response3 => {
          this.adminroles = response3.data
        })
        getMktBranches().then(response4 => {
          this.branches = response4.data
        })
      },
      methods: {
        getList() {
          this.seachQuery.page = this.page
          this.seachQuery.limit = this.limit
          this.queryType = 'list'
          this.listLoading = true
          queryAdminRoles(this.seachQuery).then(response => {
            this.list = response.data.items
            this.total = response.data.total
            this.listLoading = false
          })
        },
        handleFilter() {
          if (this.seachQuery.experience === 0) {
            this.seachQuery.experience = null
          }
          this.seachQuery.page = this.page
          this.seachQuery.limit = this.limit
          this.queryType = 'query'
          this.listLoading = true
          queryAdminRoles(this.seachQuery).then(response => {
            this.list = response.data.items
            this.total = response.data.total
            this.listLoading = false
          })
        },
        handleFilterfirst() {
          this.seachQuery.page = 1
          this.seachQuery.limit = 10
          this.page = 1
          this.limit = 10
          this.queryType = 'query'
          this.listLoading = true
          this.buttonloading = true
          queryAdminRoles(this.seachQuery).then(response => {
            this.buttonloading = false
            this.list = response.data.items
            this.total = response.data.total
            this.listLoading = false
          })
        },
        handleSizeChange(val) {
          this.limit = val
          if (this.queryType === 'list') {
            this.seachQuery.limit = val
            this.seachQuery.page = this.page
            this.getList()
          } else {
            this.seachQuery.limit = val
            this.seachQuery.page = this.page
            this.handleFilter()
          }
        },
        handleCurrentChange(val) {
          this.page = val
          if (this.queryType === 'list') {
            this.seachQuery.limit = this.limit
            this.seachQuery.page = val
            this.getList()
          } else {
            this.seachQuery.limit = this.limit
            this.seachQuery.page = val
            this.handleFilter()
          }
        },
        handleClose(done) {
          this.$confirm('确认关闭？').then(_ => {
            done()
          }).catch(_ => {})
        },
        handleCreate() {
          this.$router.push({ name: 'roleadd' })
        },
        hrefDelete(id) {
          createArticle(this.temp).then(() => {
            this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.deleteRole.id = id
              delRole(this.deleteRole).then(response => {
                if (response.data === 1) {
                  this.$notify({
                    title: '成功',
                    message: '删除成功',
                    type: 'success',
                    duration: 2000
                  })
                } else if (response.data === 9) {
                  this.$notify({
                    title: '失败',
                    message: '删除失败,角色已被使用！',
                    type: 'warning',
                    duration: 2000
                  })
                } else {
                  this.$notify({
                    title: '失败',
                    message: '删除失败',
                    type: 'warning',
                    duration: 2000
                  })
                }
                this.handleFilter()
              })
            }).catch(() => {
            })
          })
        },
        hrefuserEdit(id) {
          this.$router.push({ name: 'roleedit', params: { roleId: id }})
        },
        handleDelete(row) {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          const index = this.list.indexOf(row)
          this.list.splice(index, 1)
        },
        handleref() {
          this.seachQuery.jr_rolename = null
        }
      }
    }
  </script>
  <style>
    .el-form--label-left .el-form-item__label{
    text-align:right
    }
    .el-rate{
      line-height: 0;
      margin-top:8px;
    }
  </style>
  