<template>
  <div class="app-container">
    <!-- 搜索 -->
    <div class="filter-container">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="调度员名称" >
          <el-input v-model="searchForm.yardman_name" placeholder="请输入调度员名称" />
        </el-form-item>
        <el-form-item label="登录账号" >
          <el-input v-model="searchForm.account" placeholder="请输入登录账号" />
        </el-form-item>
        <el-form-item label="联系电话" >
          <el-input v-model="searchForm.yardman_phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="冻结状态" >
          <el-select v-model="searchForm.freeze_flag" placeholder="请选择" clearable>
            <el-option label="全部"  value=""></el-option>
            <el-option label="正常" :value="false" ></el-option>
            <el-option label="冻结" :value="true" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="fetchData">搜索</el-button>
          <router-link to="/basicInfo/disCreate"><el-button class="ml-10" type="primary" icon="el-icon-edit">新增</el-button></router-link>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <el-table v-loading="listLoading" :data="firmList" border fit  highlight-current-row style="width: 100%;">
      <el-table-column label="调度员名称" prop="username" align="center" width="150">
        <template slot-scope="scope">
          <div class="flex flex-center">
            <img class="img ml-5" v-if="scope.row.yardman_head" :src="scope.row.yardman_head" alt="">
            <span class="ml-5">{{scope.row.yardman_name}}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="登录账号" prop="account" align="center"></el-table-column>
      <el-table-column label="性别" prop="yardman_gender" align="center">
        <template slot-scope="scope">
          {{scope.row.yardman_gender==1?'男':(scope.row.yardman_gender==2?'女':'未知')}}
        </template>
      </el-table-column>
      <el-table-column label="联系电话" prop="yardman_phone" align="center"></el-table-column>
      <el-table-column label="下级司机数量" prop="driver_count" align="center"></el-table-column>
      <el-table-column label="下级司机配送总数" prop="order_sum" align="center"></el-table-column>
      <el-table-column label="登录时间" prop="login_time" align="center"></el-table-column>
      <el-table-column label="账户状态" prop="freeze_flag" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.freeze_flag==false">正常</el-tag>
          <el-tag v-else-if="scope.row.freeze_flag==true" type="danger">冻结</el-tag>
          <!-- {{scope.row.freeze_flag==false?'正常':'冻结'}} -->
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="220">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleFreeze(scope.row)"> 
            {{scope.row.freeze_flag?'解冻':'冻结'}}
          </el-button>
          <router-link :to="'/basicInfo/disEdit/'+scope.row.yardman_id">
            <el-button class="ml-10" type="primary" size="mini">编辑</el-button>
          </router-link>
          <el-button class="ml-10" size="mini" type="danger" @click="handleDelete(scope.row.yardman_id)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 翻页 -->
    <pagination v-show="total>0" 
      :total="total" 
      :page.sync="searchForm.page" 
      :page_len.sync="searchForm.page_len"
      :limit.sync="searchForm.limit" 
      @pagination="fetchData" />
  </div>
</template>

<script>
import { listYardman, deleteYardman, freezeYardman } from '@/api/user/index'

export default {
  data() {
    return {
      firmList: [],  // 列表
      total: 0,  // 总页数
      listLoading: true,
      // 搜索相关数据
      searchForm: {
        page: 1,
        page_len: 10
      },
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取企业列表
    fetchData(obj={}) {
      if(obj.page) {
        this.searchForm.page = obj.page
        this.searchForm.page_len = obj.page_len
      }
      this.listLoading = true
      let json = Object.assign({}, this.searchForm);
      json.freeze_flag===""?delete json.freeze_flag:""
      this.$http(listYardman(json), (res) => {
        this.firmList = res.data.result
        this.total = res.data.data_sum
        this.listLoading = false
      })
    },
    // 删除
    handleDelete(id) {
      this.$confirm('删除', `确认删除吗`, () => {
        this.$http(deleteYardman(id), res => {
          if(res.code == 0) {
            this.$message.success(res.message)
            this.fetchData()
          }
        })
      })
    },
    // 冻结/解冻调度员
    handleFreeze(row) {
      let json = {
        yardman_id: row.yardman_id,
        freeze_flag: !row.freeze_flag
      }
      this.$confirm(row.freeze_flag?'解冻':'冻结', '确认'+(row.freeze_flag?'解冻':'冻结')+'吗?', () => {
        this.$http(freezeYardman(json), res => {
          if(res.code == 0) {
            this.$message.success(res.message)
            this.fetchData()
          }
        })
      })
    },
  }
}
</script>

<style lang='scss' scoped>
  .img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
  }
</style>
