<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.name" clearable size="mini" placeholder="输入名称" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <date-range-picker v-model="query.createTime" class="date-item" />
        <rrOperation />
      </div>
    </div>
    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
      <el-table-column :selectable="checkboxT" type="selection" width="55" />
      <el-table-column show-overflow-tooltip prop="name" :label="$t('Auto.table.name')" :min-width="flexWidth('name',crud.data,$t('Auto.table.name'))"/>
      <el-table-column :show-overflow-tooltip="false" prop="code" :label="$t('Auto.table.code')" :min-width="flexWidth('code',crud.data,$t('Auto.table.code'))" />
      <el-table-column show-overflow-tooltip prop="status" :label="$t('Auto.table.status')" :min-width="flexWidth('status',crud.data,$t('Auto.table.status'))" />
      <el-table-column show-overflow-tooltip prop="thread_name" :label="$t('Auto.table.thread_name')" :min-width="flexWidth('thread_name',crud.data,$t('Auto.table.thread_name'))"/>
      <el-table-column show-overflow-tooltip prop="thread_state" :label="$t('Auto.table.thread_state')" :min-width="flexWidth('thread_state',crud.data,$t('Auto.table.thread_state'))" />
      <el-table-column show-overflow-tooltip prop="usedStatus" :label="$t('Auto.table.usedStatus')" :min-width="flexWidth('usedStatus',crud.data,$t('Auto.table.usedStatus'))" />
      <el-table-column show-overflow-tooltip prop="stopMessage"  :label="$t('Auto.table.stopMessage')" :min-width="flexWidth('stopMessage',crud.data,$t('Auto.table.stopMessage'))" />
      <el-table-column v-permission="['admin','timing:edit','timing:del']" :label="$t('Auto.table.operate')"  width="170px" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button v-permission="['admin','timing:edit']" style="margin-left: -2px" type="text" size="mini" icon="el-icon-video-pause" @click="start(scope.row.code)">{{ $t('common.firing') }}</el-button>
          <el-popover
            :ref="scope.row.code"
            placement="top"
            width="200"
          >
            {{ $t('Auto.msg.stop_msg') }}
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.code].doClose()">{{ $t('common.Cancel') }}</el-button>
              <el-button :loading="delLoading" type="primary" size="mini"  @click="stop(scope.row.code)">{{ $t('common.determine') }}</el-button>
            </div>
            <el-button slot="reference" icon="el-icon-video-play" type="text" size="mini">{{ $t('common.stop') }}</el-button>
          </el-popover>

        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import crudAutoRun from '@/api/system/autorun'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import crudJob from '@/views/system/timing/timing'

export default {
  name: 'Autorun',
  components: { pagination, crudOperation, rrOperation },
  cruds() {
    return CRUD({ title: '自动线程', url: 'api/autorun', crudMethod: { ...crudAutoRun }})
  },
  mixins: [presenter(), header(), crud()],
  data() {
    return {
      stopLoading: false,
      permission: {

      },
      rules: {

      }
    }
  },
  methods: {
    start(id) {
      crudAutoRun.start(id).then(res => {
        this.crud.notify('启动成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    stop(id) {
      this.stopLoading = true
      crudAutoRun.stop([id]).then(() => {
        this.stopLoading = false
        this.$refs[id].doClose()
        this.crud.toQuery()
      }).catch(() => {
        this.stopLoading = false
        this.$refs[id].doClose()
      })
    }
  }
}
</script>
