<template>
  <a-row :gutter="24">
    <a-col :md="24">
      <a-card :bordered="false">
        <!-- 查询区域 -->
        <filter-form
          @search="onSearch"
          @reset="onReset"
          :queryParam="queryParam"
          :spinnerList="spinnerList"
          :deoptData="deoptData"
        ></filter-form>
        <div style="margin-bottom: 6px">
          <a-button :disabled="!selectedRowKeys.length" type="primary" @click="onStartTask">开始盘点</a-button>
        </div>
        <a-table
          ref="table"
          size="middle"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :scroll="scroll"
          :loading="loading"
          :rowSelection="{
            selectedRowKeys: selectedRowKeys,
            onChange: onSelectChange,
            getCheckboxProps: getCheckboxProps,
          }"
          @change="handleTableChange"
        >
          <template slot="taskType" slot-scope="value">
            {{ value === 1 ? '全盘' : '抽盘' }}
          </template>
          <template slot="taskStatus" slot-scope="value">
            {{ formateTaskStatus(value) }}
          </template>
          <span slot="action" slot-scope="text, record">
            <a @click="addTask('detail', record)">查看</a>
            <a-divider type="vertical" />
            <a :disabled="record.taskStatus > 3" @click="addTask('edit', record)">编辑</a>
            <!-- <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
              <a :disabled="record.taskStatus !== 1 || record.taskStatus !== 4">删除</a>
            </a-popconfirm> -->
          </span>
        </a-table>
      </a-card>
    </a-col>
    <check-modal
      :spinnerList="spinnerList"
      :deoptData="deoptData"
      :stockVisible.sync="stockVisible"
      :title="title"
      :rules="rules"
      :taskId="taskId"
      :openType="openType"
      ref="stockF"
    ></check-modal>
  </a-row>
</template>

<script>
// import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import FilterForm from './components/FilterForm.vue'
import table from './utils/table'
// import StockModal from './components/stockModal.vue'
import { getAction, postAction } from '@/api/manage'
import { newTableMixin } from '@/mixins/newTableMixin'
import CheckModal from './components/checkModal.vue'

export default {
  components: { FilterForm, CheckModal },
  mixins: [newTableMixin],
  data() {
    return {
      description: '盘点任务列表',
      // 表头
      scroll: { x: 1500 },
      // 权限按钮集合
      btnEnableList: [1, 1, 1],
      queryParam: {
        taskStatus: undefined,
        number: '',
        depotId: undefined,
        createBy: undefined,
      },
      // stockTable: {
      //   loading: false,
      //   dataSource: [],
      //   columns: table.taskColumns,
      // },
      columns: table.taskColumns,
      dataSource: [],
      loading: false,

      rules: {
        taskType: { rules: [{ required: true, message: '请选择盘点类型' }] },
        depotId: { rules: [{ required: true, message: '请选择盘点仓库' }] },
        taskName: { rules: [{ required: true, message: '请输入盘点任务名称' }] },
      },

      // rules: {
      //   number: { rules: [{ required: true, message: '请输入盘点编号' }] },
      // },

      url: {
        list: '/stocktaking/list',
        add: '/stocktaking/add',
        detailList: '/stocktaking/detail',
        spinnerList: '/stocktaking/creatorSpinnerList',
        depotList: '/depot/findDepotByCurrentUser',
        detailByItemList: '/stocktaking/detailByItemList',
        delete: '/stocktaking/taskDelete/',
        cancel: '/stocktaking/taskCancel/',
        startTask: '/stocktaking/startTask/',
      },
      stockVisible: false,
      title: '',
      spinnerList: [],
      deoptData: [],
      taskId: '',
      openType: 'add',
    }
  },
  watch: {
    stockVisible(val) {
      if (!val) {
        this.getList()
      }
    },
  },
  created() {
    this.getList()
    this.getSpinnerList(), this.getDepotList()
  },
  methods: {
    addTask(type, data) {
      this.taskId = ''
      this.openType = type
      this.title = type === 'add' ? '新增' : type === 'edit' ? '编辑' : '查看'
      if (type !== 'add') {
        this.getDetailList(data.id)
      }
      this.stockVisible = true
    },
    getList(type) {
      if (type === 'search') this.ipagination.current = 1 // 重新加载数据时，重置当前页为第一页
      if (type === 'reset') {
        for (let i in this.queryParam) {
          this.$set(this.queryParam, i, null)
        }
      }
      const url = this.url.list + '?currentPage=' + this.ipagination.current + '&pageSize=' + this.ipagination.pageSize
      this.loading = true
      const params = { ...this.queryParam }
      postAction(url, params).then((res) => {
        this.dataSource = res.data.rows
        this.ipagination.total = Number(res.data.total)
        this.loading = false
      })
    },
    getDetailList(id) {
      this.taskId = id
    },

    getSpinnerList() {
      getAction(this.url.spinnerList).then((res) => {
        this.spinnerList = res.data || []
      })
    },

    getDepotList() {
      getAction(this.url.depotList).then((res) => {
        this.deoptData = res.data.map((item) => {
          return {
            label: item.depotName,
            value: item.id,
          }
        })
      })
    },

    handleDelete(id) {
      const ids = id || this.selectedRowKeys
      const url = this.url.delete + ids
      getAction(url)
        .then((res) => {
          this.$message.success('删除成功')
          this.getList()
        })
        .catch((err) => {
          this.$message.error('删除失败')
        })
    },
    cancelTask() {
      const ids = this.selectedRowKeys
      const url = this.url.cancel + ids
      getAction(url)
        .then((res) => {
          this.$message.success('取消成功')
          this.getList()
        })
        .catch((err) => {
          this.$message.error('取消失败')
        })
    },
    //开始盘点
    onStartTask() {
      const ids = this.selectedRowKeys
      const url = this.url.startTask + ids
      // this.$refs.table.clearSelection()

      getAction(url)
        .then((res) => {
          this.$message.success('盘点成功')
          this.getList()
          this.selectedRowKeys = []
        })
        .catch((err) => {
          this.$message.error('盘点失败')
        })
    },
  },
}
</script>

<style scoped>
@import '~@assets/less/common.less';
</style>
