<template>
  <div :class="$style.container">
    <SearchBar
      :placeholder="$t('module.teamManager.Handover.Placeholder')"
      :handover-tme="'PlannedHandoverTime'"
      @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="planTeamName"
        :label="$t('module.teamManager.Handover.TeamName')"
        width="180"
        fixed
      />
      <el-table-column
        prop="createTime"
        :label="$t('module.teamManager.Handover.CreateTime')"
        width="180"
      >
        <template slot-scope="scope">
          {{ moment(scope.row.createTime).format("YYYY-MM-DD HH:mm:ss") }}
        </template>
      </el-table-column>
      <el-table-column
        prop="planOnlineTime"
        :label="$t('module.teamManager.Handover.PlanWorkingHours')"
        width="180"
      >
        <template slot-scope="scope">
          {{ moment(scope.row.planOnlineTime).format("YYYY-MM-DD HH:mm:ss") }}
        </template>
      </el-table-column>
      <el-table-column
        prop="planHandoverTime"
        :label="$t('module.teamManager.Handover.PlannedHandoverTime')"
        width="180"
      >
        <template slot-scope="scope">
          {{ moment(scope.row.planHandoverTime).format("YYYY-MM-DD HH:mm:ss") }}
        </template>
      </el-table-column>
      <el-table-column
        prop="planTeamLeader"
        :label="$t('module.teamManager.Handover.TeamLeader')"
        width="180"
      />
      <el-table-column
        prop="planPreTeamName"
        :label="$t('module.teamManager.Handover.LastTeamName')"
        width="180"
      />
      <el-table-column
        prop="planPreTeamLeader"
        :label="$t('module.teamManager.Handover.PreviousTeamLeader')"
        width="180"
      />
      <el-table-column
        prop="handoverStatus"
        :label="$t('module.teamManager.Handover.HandoverStatus')"
        width="80"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.handoverStatus === 1" type="success">
            已完成
          </el-tag>
          <el-tag v-else type="info">
            等待中
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="remark"
        :label="$t('module.teamManager.Handover.Remark')"
        width="180"
      />
      <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="280"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.handoverStatus !== 1"
            type="primary"
            size="mini"
            icon="el-icon-play"
            @click="handleStartWork(scope.row.id)"
          >
            {{ "btn.start" | i18nFilter }}
          </el-button>
          <el-button
            v-if="scope.row.handoverStatus !== 1"
            size="mini"
            type="info"
            icon="el-icon-edit"
            @click="handleEdit(scope.row.id)"
          >
            {{ "btn.edit" | 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"
    />
  </div>
</template>
<script>
import moment from 'moment'
import { page, del } from '@/api/team-manage/team-plan.js'
import SearchBar from '@/views/team-manage/components/search-bar'
import i18n from '@/lang'
export default {
  components: { SearchBar },
  props: {},
  data() {
    return {
      moment,
      isEdit: false,
      workList: [],
      page: {
        total: 40,
        current: 1,
        size: 10
      },
      offlineDrawerVisible: false,
      param: {},
      currentTeam: null
    }
  },
  created() {
    this.handleSearch()
  },
  methods: {
    handleSearch(param) {
      this.param = param
      console.log(param)
      page({ ...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
        }))
      })
    },
    handleStartWork(id) {
      this.$router.push({
        path: 'team/add',
        query: {
          redirect: '/team-manage/list',
          title: '班组计划新增',
          planId: id
        }
      })
    },
    handleAdd() {
      this.$router.push({
        path: 'plan/add',
        query: {
          redirect: '/team-manage/plan',
          title: '班组计划新增'
        }
      })
    },
    handleEdit(id, disable) {
      this.$router.push({
        path: 'plan/add',
        query: {
          redirect: '/team-manage/plan',
          title: '班组计划详情',
          id,
          disable
        }
      })
    },
    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)
    },
    handleOffline(team) {
      this.offlineDrawerVisible = true
      this.currentTeam = team
      this.isEdit = true
    },
    handleDelete(id) {
      this.$confirm(
        `${this.$t('module.teamManager.Handover.alertPlanContent')}`,
        `${this.$t('module.teamManager.Handover.alertTitle')}`,
        {
          confirmButtonText: i18n.t('btn.confirm'),
          cancelButtonText: i18n.t('btn.cancel'),
          type: 'warning'
        }
      ).then(() => {
        console.log(id)
        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>
