<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input v-model="listQuery.username" placeholder="用户名" style="width: 200px; margin-right: 10px;" class="filter-item" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="getList">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-circle-plus" @click="handleCreate({})">
        添加
      </el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">

      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleCreate(scope.row)" size="small" icon="el-icon-edit">
            修改
          </el-button>

          <el-button type="primary" v-if="scope.row.status" @click="onWork(scope.row)" size="small" icon="el-icon-date">
            排班
          </el-button>
        </template>
      </el-table-column>

      <el-table-column align="center" label="ID" width="80" prop="id"></el-table-column>

      <el-table-column align="center" label="用户名" width="160" prop="username"></el-table-column>

      <el-table-column align="center" label="姓名" width="160" prop="name"></el-table-column>

      <el-table-column align="center" label="手机" width="160" prop="mobile"></el-table-column>

      <el-table-column align="center" label="头像">
        <template slot-scope="scope">
          <el-avatar :size="50" :src="scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="状态" width="180">
        <template slot-scope="{row}">
          <el-switch
            v-model="row.status"
            @change="setStatus(row)"
            active-text="启用"
            :active-value="1"
            :inactive-value="0"
            inactive-text="禁用">
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="修改时间">
        <template slot-scope="scope">
          <span>{{ scope.row.update_time | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />


    <el-dialog title="修改管理员" :visible.sync="dialogVisible">
      <el-form label-width="120px" :model="item">
        <el-form-item label="用户名">
          <el-input v-model="item.username" name="username" placeholder="管理员的用户名"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="item.name" name="name" placeholder="管理员的姓名"></el-input>
        </el-form-item>
        <el-form-item label="手机">
          <el-input v-model="item.mobile" name="name" placeholder="发送短时的时候会用到这个手机"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="item.password" type="password" name="password" placeholder="管理员的新密码"></el-input>
        </el-form-item>
        <el-form-item label="是否分配">
          <el-switch
            v-model="item.is_order"
            active-text="分配"
            :active-value="1"
            inactive-text="不分配"
            :inactive-value="0"
            >
          </el-switch>
        </el-form-item>
        <el-form-item label="是否主播">
          <el-switch
            v-model="item.is_anchor"
            active-text="主播"
            :active-value="1"
            inactive-text=""
            :inactive-value="0"
            >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onSave(item)">保 存</el-button>
      </div>
    </el-dialog>

    <el-dialog title="添加排班" :visible.sync="dialogWork">
      <el-form label-width="120px" :model="from">

        <el-form-item label="上班日期">
          <el-date-picker
            style="margin-right: 10px;"
            type="dates"
            v-model="from.dates"
            placeholder="选择一个或多个日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="上班时间段">
          <el-time-picker
            is-range
            v-model="from.time"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            placeholder="选择时间范围"
          >
          </el-time-picker>
        </el-form-item>

        <el-form-item label="渠道">
          <el-checkbox-group v-model="from.oss" >
            <el-checkbox v-for="(v,i,k) in oss" :label="i" :key="k">{{v}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onWork()">保 存</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'Adminlist',
  components: { Pagination },
  data() {
    return {
      list: [],
      oss: [],
      from: {oss:[]},
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      dialogVisible: false,
      dialogWork: false,
      item: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.$axios.get('/admin/admin/index', { params: this.listQuery }).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.oss = response.ext.oss
        // for(let k in response.ext.oss){
        //   this.oss.push({id:k,val:response.ext.oss[k]})
        // }
        console.log(this.oss)
        this.listLoading = false
      }).catch(err=>{
      })
    },
    setStatus(item) {
      console.log(item)
      this.$axios.post('/admin/admin/disabled',item).then(res=>{
        this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
      }).catch(err=>{
        this.$notify({
          title: '成功',
          message: '修改失败',
          type: 'error',
          duration: 2000
        })
      })
    },
    handleCreate(item) {
      this.dialogVisible = true;
      this.item = item
    },
    onSave(from) {
      this.$axios.post('/admin/admin/save', from).then(response => {
        this.item = {}
        this.dialogVisible = false
        this.getList()
      }).catch(err=>{
      })
    },
    onWork(item) {
      if(item) {
        this.item = item
        this.from = {oss:[]}
        this.dialogWork = true
        return
      }
      this.from.admin_id = this.item.id;
      this.$axios.post('/admin/work/saves', this.from).then(response => {
        this.item = {}
        this.dialogWork = false
      }).catch(err=>{

      })
    },
    setTime(val) {
      console.log(val)
    }
  }
}
</script>
