<template>
  <div :class="$style.container">
    <SearchBar :placeholder="$t('module.teamManager.Handover.Placeholder')" @on-search="handleSearch">
      <el-button type="success" icon="el-icon-plus" @click="handleAdd()">
        {{ "btn.add" | i18nFilter }}
      </el-button>
    </SearchBar>
    <el-table
      :data="workList"
      :class="$style.table"
      :stripe="true"
      :header-cell-style="{background:'#eef1f6',color:'#606266',height: '56px', textAlign: 'center'}"
      :cell-style="{ textAlign: 'center' }"
      size="medium"
    >
      <el-table-column prop="index" :label="$t('module.teamManager.Handover.index')" width="80" fixed />
      <el-table-column prop="teamName" :label="$t('module.teamManager.Handover.TeamName')" width="180" fixed />
      <el-table-column prop="handoverTime" :label="$t('module.teamManager.Handover.HandoverTme')" width="180">
        <template slot-scope="scope">
          {{ scope.row.handoverTime && moment(scope.row.handoverTime).format("YYYY-MM-DD HH:mm:ss") }}
        </template>
      </el-table-column>
      <el-table-column prop="teamLeader" :label="$t('module.teamManager.Handover.TeamLeader')" width="180" />
      <el-table-column prop="onlineTime" :label="$t('module.teamManager.Handover.WorkingHours')" width="180">
        <template slot-scope="scope">
          {{ scope.row.onlineTime && moment(scope.row.onlineTime).format("YYYY-MM-DD HH:mm:ss") }}
        </template>
      </el-table-column>
      <el-table-column prop="offlineTime" :label="$t('module.teamManager.Handover.OffWorkTime')" width="180">
        <template slot-scope="scope">
          {{ scope.row.offlineTime && moment(scope.row.offlineTime).format("YYYY-MM-DD HH:mm:ss") }}
        </template>
      </el-table-column>
      <el-table-column prop="preTeamName" :label="$t('module.teamManager.Handover.LastTeamName')" width="180" />
      <el-table-column prop="preTeamLeader" :label="$t('module.teamManager.Handover.PreviousTeamLeader')" width="180" />
      <el-table-column prop="teamWorkStatus" :label="$t('module.teamManager.Handover.TeamWorkStatus')" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.teamWorkStatus === 1" type="success">
            {{ $t('module.teamManager.Handover.working') }}
          </el-tag>
          <el-tag v-else type="info">
            {{ $t('module.teamManager.Handover.offduty') }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('module.teamManager.Handover.Detail')" width="80">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row.id, true)">{{
            "btn.detail" | i18nFilter
          }}</el-button>
        </template>
      </el-table-column>
      <el-table-column :label="$t('module.teamManager.Handover.Operation')" width="400" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.teamWorkStatus === 1"
            size="mini"
            type="info"
            icon="el-icon-edit"
            @click="handleOffline(scope.row)"
          >
            {{ "btn.goOffWork" | i18nFilter }}
          </el-button>
          <el-button
            type="info"
            size="mini"
            icon="el-icon-edit"
            @click="handleShow(scope.row.id)"
          >
            {{ "btn.log" | i18nFilter }}
          </el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="handleDelete(scope.row.id)"
          >
            {{ "btn.delete" | i18nFilter }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :hide-on-single-page="false"
      :class="$style.table"
      :current-page="page.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next"
      :total="page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <OfflineDrawer
      :team="currentTeam"
      :class="$style.dialog"
      :visible="offlineDrawerVisible"
      @on-close="offlineDrawerClose()"
    />
  </div>
</template>
<script>
import moment from 'moment'
import { list, del } from '@/api/team-manage/team.js'
import SearchBar from '@/views/team-manage/components/search-bar'
import OfflineDrawer from '@/views/team-manage/components/offline/offline-drawer'
import i18n from '@/lang'
export default {
  components: { SearchBar, OfflineDrawer },
  props: {},
  data() {
    return {
      moment,
      isEdit: false,
      workList: [],
      page: {
        total: 0,
        current: 1,
        size: 10
      },
      offlineDrawerVisible: false,
      param: {},
      currentTeam: null
    }
  },
  created() {
    this.$bus.on('on-offline-edit-success', this.offlineDrawerClose)
    this.handleSearch()
  },
  beforeDestroy() {
    this.$bus.off('on-offline-edit-success', this.offlineDrawerClose)
  },
  methods: {
    handleShow(id) {
      this.$router.push({
        path: 'showlog',
        query: {
          id
        }
      })
    },
    handleSearch(param) {
      this.param = param
      console.log(param)
      list({ ...param, ...this.page }).then(res => {
        console.log(res)
        this.page.total = res.data.total
        if (!res.data.records) {
          this.workList = []
          return
        }
        this.workList = res.data.records.map((m, index) => ({
          ...m,
          index: this.page.size * (this.page.current - 1) + index + 1
        }))
      })
    },
    handleAdd() {
      this.$router.push({
        path: 'team/add',
        query: {
          redirect: '/team-manage/list',
          title: '班组计划新增'
        }
      })
    },
    handleEdit(id, disable) {
      this.$router.push({
        path: 'team/add',
        query: {
          redirect: '/team-manage/list',
          title: '班组计划详情',
          id,
          disable
        }
      })
    },
    handleOffline(team) {
      this.offlineDrawerVisible = true
      this.currentTeam = team
      this.isEdit = true
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.page.size = val
      this.handleSearch(this.param)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page.current = val
      this.handleSearch(this.param)
    },
    handleDelete(id) {
      this.$confirm(`${this.$t('module.teamManager.Handover.alertListContent')}`,
        `${this.$t('module.teamManager.Handover.alertTitle')}`, {
          confirmButtonText: i18n.t('btn.confirm'),
          cancelButtonText: i18n.t('btn.cancel'),
          type: 'warning'
        }).then(() => {
        del({ id }).then(res => {
          this.handleSearch(this.param)
          this.$message({
            type: 'info',
            message: i18n.t('deleteMsgBox.doneMsg')
          })
        })
      }).catch(() => {
        // this.$message({
        //   type: 'info',
        //   message: '已取消删除'
        // })
      })
      // this.$alert(`${this.$t('module.teamManager.Handover.alertListContent')}`, `${this.$t('module.teamManager.Handover.alertTitle')}`, {
      //   confirmButtonText: i18n.t('btn.confirm'),
      //   callback: action => {
      //     del({ id }).then(res => {
      //       this.handleSearch(this.param)
      //       this.$message({
      //         type: 'info',
      //         message: i18n.t('deleteMsgBox.doneMsg')
      //       })
      //     })
      //   }
      // })
    },
    offlineDrawerClose() {
      this.handleSearch()
      this.offlineDrawerVisible = false
    }
  }
}
</script>
<style lang="scss" module>
.container {
  .table {
    margin: 16px;
    width: 98.5%;
  }
}
</style>
