<template>
  <div class="app-container">
    <!-- 查询表单区域 -->
    <el-form :model="listQuery" size="small" :inline="true" label-position="top">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="开始日期">
            <el-date-picker v-model="listQuery.startDate" value-format="yyyy-MM-dd" type="date" 
              placeholder="开始日期" style="width: 100%">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="结束日期">
            <el-date-picker v-model="listQuery.endDate" value-format="yyyy-MM-dd" type="date" 
              placeholder="结束日期" style="width: 100%">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="仓库">
            <el-select v-model="listQuery.warehouseId" clearable filterable 
              placeholder="仓库" style="width: 100%">
              <el-option :key="item.id" v-for="item in warehouseList" 
                :label="item.warehouseName" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="盘点人">
            <el-select v-model="listQuery.addHandlerId" clearable filterable 
              placeholder="盘点人" style="width: 100%">
              <el-option :key="item.loginId" v-for="item in userSelsctList" 
                :label="item.deptName+'_'+item.userName" :value="item.loginId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="搜索">
            <el-button style="width: 100%;" type="primary" icon="el-icon-search" 
              @click="handleFilter">搜索</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="重置">
            <el-button style="width: 100%;" icon="el-icon-refresh" type="primary" 
              @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" class="editButton" size="small" 
          icon="el-icon-plus" @click="handleCreate">添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="small" 
          @click="handleDownload">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="small" 
          @click="delAll">批量删除</el-button>
      </el-col>
    </el-row>

    <!-- 表格区域 -->
    <div class="border-card">
      <el-table height="450" size="small" :data="list" @selection-change="handleSelectionChange" 
        v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>
        <el-table-column type="selection" width="55px" align="center"></el-table-column>
        <el-table-column type="index" label="序号" header-align="center" align="center"></el-table-column>
        <el-table-column align="center" min-width="150px" label="盘点单号">
          <template slot-scope="scope">
            <router-link :to="{ name: 'stockCountDetail', params: { id: scope.row.id } }">
              <div style="color: #337ab7;cursor: pointer;">{{ scope.row.serialCode }}</div>
            </router-link>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="100px" label="盘点时间" prop="serialDate"></el-table-column>
        <el-table-column align="center" min-width="100px" label="仓库" prop="warehouseName"></el-table-column>
        <el-table-column align="center" min-width="100px" label="盘点人" prop="addHandlerName"></el-table-column>
        <el-table-column align="center" label="操作" width="240px" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" 
              class="editText" size="small" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button v-if="scope.row.isRunFinish == '1'" type="text" icon="el-icon-delete" 
              class="deleteText" size="small" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="listQuery.page" :page-sizes="[10, 20, 30, 50]" :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { listStockCount, deleteStockCount, executeStockCount } from '@/api/stockCount'
import { warehouseList } from '@/api/warehouse'
import { allUserList } from '@/api/public'

export default {
  data() {
    return {
      warehouseList: [],
      userSelsctList: [],
      list: [],
      delarr: [],
      multipleSelection: [],
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 10,
        startDate: '',
        endDate: '',
        warehouseId: '',
        addHandlerId: ''
      }
    }
  },
  created() {
    this.getStoreList()
    this.getAllUserList()
    this.getList()
  },
  methods: {
    getStoreList() {
      warehouseList().then(response => {
        this.warehouseList = response.data.data
      }).catch(() => { })
    },
    getAllUserList() {
      allUserList().then(response => {
        this.userSelsctList = response.data.data
      }).catch(() => { })
    },
    resetQuery() {
      this.listQuery = {
        page: 1,
        limit: 10,
        startDate: '',
        endDate: '',
        warehouseId: '',
        addHandlerId: ''
      }
      this.getList()
    },
    handleDownload() {
      window.location.href = process.env.BASE_API + '/warehouse-stockCount/export'
    },
    handleCreate() {
      this.$router.push({ name: 'stockCountAdd' })
    },
    getList() {
      this.listLoading = true
      listStockCount(this.listQuery).then(response => {
        this.list = response.data.data.items
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleUpdate(row) {
      this.$router.push({ name: 'stockCountEdit', params: { id: row.id }})
    },
    handleExecute(row) {
      this.$confirm('确认盘点吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        executeStockCount({ ids: row.id }).then(response => {
          this.$notify({
            title: '成功',
            message: '盘点成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      }).catch(() => { })
    },
    executeAll() {
      const length = this.multipleSelection.length
      if (length > 0) {
        this.$confirm('确认盘点吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          for (let i = 0; i < length; i++) {
            this.delarr.push(this.multipleSelection[i].id)
          }
          const ids = this.delarr.join(',')
          executeStockCount({ ids: ids }).then(() => {
            this.$notify({
              title: '成功',
              message: '盘点成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        })
      } else {
        this.$notify({
          title: '提示',
          message: '请选择要盘点的信息！',
          type: 'warning'
        })
      }
    },
    handleDelete(row) {
      this.$confirm('确认删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteStockCount({ ids: row.id }).then(response => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      }).catch(() => { })
    },
    delAll() {
      const length = this.multipleSelection.length
      if (length > 0) {
        this.$confirm('确认删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          for (let i = 0; i < length; i++) {
            this.delarr.push(this.multipleSelection[i].id)
          }
          const ids = this.delarr.join(',')
          deleteStockCount({ ids: ids }).then(() => {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        })
      } else {
        this.$notify({
          title: '提示',
          message: '请选择要删除的信息！',
          type: 'warning'
        })
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  }
}
</script>