  <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.username" :maxlength = 20>
              </el-input>
              </div>
            </el-form-item>
          </div>
        </el-col>
        <el-col :xs="23" :sm="23" :md="11" :lg="6" :xl="6">
          <div class="grid-content bg-purple" >
            <el-form-item  :label="$t('usermanager.name')" >
              <div class="block">
                <el-input style="width: 100%;" class="filter-item"
                  v-model="seachQuery.name" :maxlength = 20>
               </el-input>
              </div>
            </el-form-item>
          </div>
        </el-col>
        <el-col :xs="23" :sm="23" :md="12" :lg="6" :xl="6">
          <div class="grid-content bg-purple" >
            <el-form-item  :label="$t('usermanager.phone')" >
              <div class="block">
                 <el-input style="width: 100%;" class="filter-item"
                  v-model="seachQuery.phone" :maxlength = 20>
               </el-input>
              </div>
            </el-form-item>
          </div>
        </el-col>
        <el-col :xs="23" :sm="23" :md="11" :lg="5" :xl="5">
          <div class="grid-content bg-purple" >
            <el-form-item  :label="$t('usermanager.role')" >
              <div class="block">
                <el-select class="filter-item" v-model="seachQuery.role"  style="width: 100%;">
                  <el-option v-for="item in adminroles" :key="item.id" :label="item.jr_rolename" :value="item.id">
                  </el-option>
                </el-select>
              </div>
            </el-form-item>
          </div>
        </el-col>
        </el-row>
        <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.city')" >
              <div class="block">
                <el-select v-if="mktEnable" disabled class="filter-item" v-model="seachQuery.city"  style="width: 100%;">
                  <el-option v-for="item in branches" :key="item.branche_code" :label="item.branche_name" :value="item.branche_code">
                  </el-option>
                </el-select>
                <el-select v-else class="filter-item" v-model="seachQuery.city"  style="width: 100%;">
                  <el-option v-for="item in branches" :key="item.branche_code" :label="item.branche_name" :value="item.branche_code">
                  </el-option>
                </el-select>
              </div>
            </el-form-item>
          </div>
        </el-col>
        <el-col :xs="23" :sm="23" :md="11" :lg="6" :xl="6">
            <div class="grid-content bg-purple" >
            <el-form-item  :label="$t('usermanager.status')" >
              <div class="block">
                <el-select class="filter-item" v-model="seachQuery.status"  style="width: 100%;">
                  <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </div>
            </el-form-item>
          </div>
        </el-col>
        </el-row>
        <el-row :gutter="24" >
          <div  align="center" > <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></div>
        </el-row>
        <el-row :gutter="24">
          <div align="right">
            <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary"
                  icon="el-icon-edit">{{$t('table.add')}}
          </el-button>&nbsp;&nbsp;&nbsp;</div>    
        </el-row>
      </el-form>
      <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="加载中......" border fit
                highlight-current-row
                style="width: 100%">
        <el-table-column type="index" align="center" :label="$t('table.seq')" width="50">
        </el-table-column>
        <el-table-column align="center" :label="$t('usermanager.username')">
          <template slot-scope="scope">
            <span>{{scope.row.username}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" :label="$t('usermanager.name')">
          <template slot-scope="scope">
            <span>{{scope.row.name}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" :label="$t('usermanager.phone')" width="110">
          <template slot-scope="scope">
            <span>{{scope.row.phone}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" :label="$t('usermanager.role')">
          <template slot-scope="scope">
            <span>{{scope.row.rolename}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" :label="$t('usermanager.status')">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == '1' " type="success">{{$t('table.status1')}}</el-tag>
            <el-tag v-else type="danger" >{{$t('table.status6')}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" :label="$t('usermanager.city')">
          <template slot-scope="scope">
            <span>{{scope.row.cityname}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" :label="$t('table.actions')" width="180" 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="#" v-if="scope.row.status" @click="hrefDisabe(scope.row.id)"><el-tag >{{$t('table.status6')}}</el-tag></a> 
            <el-tag v-else type="danger">{{$t('table.status6')}}</el-tag>
            <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="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :before-close="handleClose">
        <el-form :rules="rules" ref="addForm" :model="addAdmininfo" label-position="left" label-width="80px"
                style='width: 600px;margin-left:20px'>
                <el-row :gutter="16" style="margin-bottom: 15px;">
                <el-col :span="14">
                  <div class="grid-content bg-purple">
                    <el-form-item  :label="$t('usermanager.username')" prop="username" >
                      <el-input v-model="addAdmininfo.username" style="width: 220px;" @blur="checkDuplicate" ></el-input>
                    </el-form-item>
                  </div>
                </el-col>
                <el-col :span="8">
                  <el-form-item  :label="$t('usermanager.phone')" prop="phone" >
                      <el-input v-model="addAdmininfo.phone" style="width: 220px;"></el-input>
                    </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="16" style="margin-bottom: 15px;">  
                <el-col :span="14">
                  <div class="grid-content bg-purple" style="width: 300px;">
                    <el-form-item  :label="$t('usermanager.name')" prop="name" style="width: 300px;">
                      <div class="block">
                    <el-input v-model="addAdmininfo.name"> </el-input>
                      </div>
                    </el-form-item>
                  </div>
                </el-col>
                <el-col :span="8">
                  <div class="grid-content bg-purple">
                    <div class="grid-content bg-purple" style="width: 300px;">
                    <el-form-item  :label="$t('usermanager.password')" prop="password">
                    <el-input v-model="addAdmininfo.password"></el-input>
                    </el-form-item>
                  </div>
                  </div>
                </el-col>
              </el-row>
              <el-row :gutter="16" style="margin-bottom: 15px;">
                <el-col :span="14">
                  <div class="grid-content bg-purple" style="width: 300px;">
                    <el-form-item  :label="$t('usermanager.role')" prop="role">
                      <el-select class="filter-item" v-model="addAdmininfo.role"  style="width: 220px;">
                        <el-option v-for="item in adminroles" :key="item.id" :label="item.jr_rolename" :value="item.id">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </div>
                </el-col>
                <el-col :span="8">
                  <div class="grid-content bg-purple" style="width: 300px;">
                    <el-form-item  :label="$t('usermanager.city')" prop="city">
                    <el-select v-if="mktEnable" disabled  class="filter-item" v-model="addAdmininfo.city"  style="width: 220px;">
                      <el-option v-for="item in branches" :key="item.branche_code" :label="item.branche_name" :value="item.branche_code">
                      </el-option>
                    </el-select>
                    <el-select v-else class="filter-item" v-model="addAdmininfo.city"  style="width: 220px;">
                      <el-option v-for="item in branches" :key="item.branche_code" :label="item.branche_name" :value="item.branche_code">
                      </el-option>
                    </el-select>
                    </el-form-item>
                  </div>
                </el-col>
              </el-row>
          </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button  type="primary" @click="addAdmininfob">{{$t('table.confirm')}}</el-button>
          <el-button @click="resetForm('addForm')">{{$t('navbar.reset')}}</el-button>
        </div>
      </el-dialog>

      <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 { queryAdminUsers, getAdminRoles, checkUsername, getMktBranches, JlAdminadd, disableAdminUser, JlAdmindelete } from '@/api/manager'
    import crypto from 'crypto'
    import regex from '@/utils/regex'
    export default {
      name: 'usermanager',
      directives: {
        waves
      },
      data() {
        return {
          seachQuery: {
            username: null,
            name: null,
            role: null,
            status: null,
            city: null,
            phone: null
          },
          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,
            phone: undefined
          },
          disableAdmininfo: {
            id: undefined,
            status: 0
          },
          deleteAdmininfo: {
            id: undefined
          },
          queryCheckUsername: {
            username: undefined
          },
          rules: {
            username: [{ required: true, min: 1, max: 20, message: '请正确录入用户名', trigger: 'blur' },
              { pattern: /^[a-zA-Z0-9_-]{4,16}$/, 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' }],
            phone: [{ required: true, message: '请输入手机号码', trigger: 'blur' },
              { pattern: regex.REPHONE, 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
          queryAdminUsers(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
          queryAdminUsers(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
          queryAdminUsers(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()
          }
        },
        handleModifyStatus(row, status) {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          row.status = status
        },
        handleCreate() {
          this.dialogFormVisible = true
          this.addAdmininfo.username = ''
          this.addAdmininfo.name = ''
          this.addAdmininfo.password = 'abcd1234'
          this.addAdmininfo.role = ''
          this.addAdmininfo.status = ''
          this.addAdmininfo.city = ''
          this.addAdmininfo.phone = ''
          if (this.userMkt !== 'ALL') {
            this.addAdmininfo.city = this.userMkt
          }
          this.$nextTick(() => {
            this.$refs['addForm'].clearValidate()
          })
        },
        handleClose(done) {
          this.$confirm('确认关闭？').then(_ => {
            done()
          }).catch(_ => {})
        },
        addAdmininfob() {
          this.$refs['addForm'].validate((valid) => {
            if (valid) {
              this.listLoading = true
              var md5 = crypto.createHash('md5')
              md5.update(this.addAdmininfo.password)
              this.addAdmininfo.password = md5.digest('hex')
              JlAdminadd(this.addAdmininfo).then(response => {
                this.list = response.data.items
                this.total = response.data.total
                this.listLoading = false
                this.dialogFormVisible = false
                if (response.data === 1) {
                  createArticle(this.temp).then(() => {
                    this.$notify({
                      title: '成功',
                      message: '创建成功',
                      type: 'success',
                      duration: 2000
                    })
                  })
                }
                this.getList()
              })
            }
          })
        },
        hrefDisabe(id) {
          this.disableAdmininfo.id = id
          disableAdminUser(this.disableAdmininfo).then(response => {
            if (response.data === 1) {
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
            } else {
              this.$notify({
                title: '失败',
                message: '更新失败',
                type: 'warning',
                duration: 2000
              })
            }
            this.handleFilter()
          })
        },
        hrefDelete(id) {
          createArticle(this.temp).then(() => {
            this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.deleteAdmininfo.id = id
              JlAdmindelete(this.deleteAdmininfo).then(response => {
                if (response.data === 1) {
                  this.$notify({
                    title: '成功',
                    message: '删除成功',
                    type: 'success',
                    duration: 2000
                  })
                } else {
                  this.$notify({
                    title: '失败',
                    message: '删除失败',
                    type: 'warning',
                    duration: 2000
                  })
                }
                this.getList()
              })
            }).catch(() => {
            })
          })
        },
        checkDuplicate() {
          this.queryCheckUsername.username = this.addAdmininfo.username
          checkUsername(this.queryCheckUsername).then(response => {
            if (response.data >= 1) {
              this.addAdmininfo.username = ''
              this.$message({ message: '用户编号重复！', type: 'warning' })
            }
          })
        },
        hrefuserEdit(id) {
          this.$router.push({ name: 'useredit', params: { userId: 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.username = null
          this.seachQuery.name = null
          this.seachQuery.role = null
          this.seachQuery.status = null
          this.seachQuery.city = null
          this.seachQuery.phone = null
          if (this.userMkt !== 'ALL') {
            this.seachQuery.city = this.userMkt
          }
        },
        resetForm(formName) {
          this.$refs[formName].resetFields()
        }
      }
    }
  </script>
  <style>
    .el-form--label-left .el-form-item__label{
    text-align:right
    }
    .el-rate{
      line-height: 0;
      margin-top:8px;
    }
  </style>
  