<template>
  <div>
    <el-form
      class="searchBox"
      ref="form"
      :model="form"
      :inline="true"
      label-width="140px"
    >
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="起始时间"
        prop="startDate"
      >
        <el-date-picker
          v-model="form.startDate"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="截止时间"
        prop="endDate"
      >
        <el-date-picker
          v-model="form.endDate"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="操作员"
        prop="keyWords1"
      >
        <el-input v-model="form.keyWords1"></el-input>
      </el-form-item>
      <!-- <el-form-item
        label="任务类型"
        prop="keyWords2"
      >
        <el-select v-model="form.keyWords2">
          <el-option
            value=""
            label="全部"
          ></el-option>
          <el-option
            v-for="item in Object.entries(typeDic)"
            :key="item[0]"
            :value="item[0]"
            :label="item[1]"
          ></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item
        label="状态"
        prop="keyWords3"
      >
        <el-select v-model="form.keyWords3">
          <el-option
            value=""
            label="全部"
          ></el-option>
          <el-option
            v-for="item in Object.entries(statusDic)"
            :key="item[0]"
            :value="item[0]"
            :label="item[1]"
          ></el-option>
        </el-select>
      </el-form-item>
      <div class="btn-wrap">
        <el-button
          @click="reset"
        >
          重置
        </el-button>
        <el-button
          type="primary"
          @click="getData"
        >
          搜索
        </el-button>
        <el-button
          type="primary"
          @click="logReturnStorage({})"
        >
          退库
        </el-button>
      </div>
    </el-form>
    <el-table
      stripe
      border
      v-loading="loading"
      :data="tableData"
    >
      <el-table-column
        prop="manager_name"
        label="操作员"
      >
      </el-table-column>
      <el-table-column
        prop="min_id"
        label="起始序号"
      >
      </el-table-column>
      <el-table-column
        prop="max_id"
        label="截止序号"
      >
      </el-table-column>
      <el-table-column
        width="80"
        label="数量"
      >
        <template slot-scope="scope">
          {{ scope.row.max_id - scope.row.min_id + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
      >
        <template slot-scope="scope">
          {{ scope.row.create_time | formatTime }}
        </template>
      </el-table-column>
      <el-table-column
        label="完成时间"
      >
        <template slot-scope="scope">
          {{ scope.row.finish_time | formatTime }}
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
      >
        <template slot-scope="scope">
          {{ statusDic[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="logReturnStorage(scope.row)">
            退库
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page.sync="form.pageNum"
      :page-size="10"
      layout="total, prev, pager, next"
      @current-change="getData"
      :total="total">
    </el-pagination>
    <ElDialog
      title="门票退库"
      width="700px"
      v-model="returnStorageVisible">
      <ReturnStorage
        :current-log="currentLog"
        @update="getData"
      ></ReturnStorage>
    </ElDialog>
  </div>
</template>
<script>
import { getMission } from '@/api/ticketType'
import ElDialog from '@/components/Dialog'
import ReturnStorage from './returnStorage'
export default {
  components: {
    ElDialog,
    ReturnStorage
  },
  data () {
    return {
      form: {
        keyWords1: '',
        keyWords2: 'ACTIVATE',
        keyWords3: '',
        pageNum: 1,
        pageSize: 10,
        startDate: '',
        endDate: ''
      },
      loading: false,
      total: 0,
      tableData: [],
      returnStorageVisible: false,
      currentLog: {},
      typeDic: {
        'ADD': '入库',
        'EXPORT': '导出',
        'ACTIVATE': '激活',
        'RETURN': '退库'
      },
      statusDic: {
        'WAITING': '未开始',
        'PROCESSING': '处理中',
        'FINISH': '成功',
        'FAIL': '失败'
      }
    }
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    getData () {
      this.loading = true
      getMission(this.form).then(res => {
        this.tableData = res.list
        this.total = res.total
      }).finally(() => {
        this.loading = false
      })
    },
    // 按入库记录来退库
    logReturnStorage (item) {
      if (item.min_id && item.max_id) {
        this.currentLog = item
      } else {
        this.currentLog = {}
      }
      this.returnStorageVisible = true
    }
  }
}
</script>
