<template>
  <div class="Inbound">
    <p-card title="盘点计划" :style="{ borderBottom: 'none' }">
      <span slot="extra">
        <a-space>
          <p-button type="primary" @click="StartCounting">开始盘点</p-button>
          <p-button type="primary" @click="completeCounting">完成盘点</p-button>
        </a-space>
      </span>

      <div class="plg-inner-form-mb">
        <p-grid-form
          ref="search"
          :span="6"
          :label-col="{ span: 6 }"
          :items="items"
          @submit="hanlderSubmit"
        ></p-grid-form>
      </div>
      <p-table
        ref="table"
        rowKey="id"
        :bordered="true"
        :autoLoad="true"
        :scroll="{ x: 1200 }"
        :has-row-count="true"
        :row-selection="rowSelection"
        :filterDropdownColum="false"
        :colorTransformRow="false"
        :remoteUrl="remoteUrl"
        :columns="columns"
        :data-source.sync="data"
        :remoteData="remoteParams"
        :remoteParams="remoteParams"
        :selectedRowKeys.sync="selectedRowKeys"
      >
        <!-- <template slot="action" slot-scope="text, record">
          <a @click="Initialize(record)">初始化</a>
          <a-divider type="vertical" />
          <a @click="cancelTask(record)">取消任务</a>
        </template> -->
      </p-table>
    </p-card>
  </div>
</template>

<script>
import { items, columns, remoteUrl } from './config'
import { initialize, cancelTask, start, end } from './api'
export default {
  name: 'index',
  data() {
    return {
      items: items(this),
      columns,
      remoteUrl,
      data: [],
      selectedRowKeys: [],
      remoteParams: {
        isLock: 0,
        taskType: 0,
      },
      selectedRows: [],
    }
  },
  computed: {
    rowSelection() {
      let _this = this
      return {
        selectedRowKeys: _this.selectedRowKeys,
        onSelectAll(selected, selectedRows, changeRows) {
          var checked = selected
          var key = changeRows.map((item) => item.id)
        },
        onSelect(record, selected, selectedRows, nativeEvent) {
          var checked = selected
          var key = record.id
        },
        onChange(selectedRowKeys, selectedRows) {
          _this.selectedRows = selectedRows
          _this.selectedRowKeys = selectedRowKeys
        },
      }
    },
  },
  methods: {
    //   开始盘点
    async StartCounting() {
      if (this.selectedRows.length == 0) {
        this.$message.warning('请选择盘点任务')
        return
      }
      let data = this.selectedRows.map((i) => {
        return {
          containerNo: i.containerNo,
          containerStoreId: i.containerStoreId,
          goodsId: i.goodsId,
        }
      })
      console.log(data)
      try {
        let res = await start(data)
        if (res.success) {
          this.$message.success(res.data.message)
          this.remoteParams = { ...this.remoteParams }
        } else {
          this.$exception(res)
        }
      } catch (error) {
        this.$exception(error)
      }
      //   start
    },
    //完成盘点
    async completeCounting() {
      if (this.selectedRows.length == 0) {
        this.$message.warning('请选择盘点任务')
        return
      }

      //       "containerNo":"XXXX",
      //         "goodsId":"XXXX",
      //         "containerStoreId":"XXXX"

      let i = this.selectedRows.some((i) => i.taskType == 32)
      console.log(i)
      if (!i) {
        this.$message.warning('状态不为盘点出库无法进行盘点')
        return
      }
      let inventoryTaskId = this.selectedRows.map((i) => i.inventoryTaskId)
      console.log(inventoryTaskId)
      try {
        let res = await end(inventoryTaskId)
        if (res.success) {
          this.$message.success('盘点完成')
          this.remoteParams = { ...this.remoteParams }
        } else {
          this.$exception(res)
        }
      } catch (error) {
        this.$exception(error)
      }
    },

    getData(params) {
      this.$refs.table.reload()
    },
    hanlderSubmit(event, form) {
      event.preventDefault()
      form.validateFields((err, values) => {
        if (!err) {
          this.remoteParams = values
        }
      })
    },
    Initialize(data) {
      let _this = this
      this.$confirm({
        title: '您确定要初始化吗?',
        onOk() {
          initialize(data).then((res) => {
            let msg = res.data
            if (res.success) {
              _this.$message.success(msg || '操作成功')
            } else {
              _this.$exception(res)
            }
            _this.getData()
          })
        },
        onCancel() {},
      })
    },
    cancelTask(data) {
      let _this = this
      this.$confirm({
        title: '您确定要取消任务吗?',
        onOk() {
          cancelTask(data).then((res) => {
            let msg = res.data
            if (res.success) {
              _this.$message.success(msg || '操作成功')
            } else {
              _this.$exception(res)
            }
            _this.getData()
          })
        },
        onCancel() {},
      })
    },
  },
}
</script>

<style scoped></style>
