<template>
  <div>
    <el-button
      type="primary"
      plain
      icon="el-icon-refresh"
      size="mini"
      :loading="syncLoading"
      @click="openSelectSynchronous"
    >手动同步</el-button>
    <el-dialog
      v-dialogDrag
      title="同步盘点"
      :visible.sync="open"
      width="800px"
      append-to-body
      @open="dialogOpen"
    >
      <el-table
        ref="multipleTable"
        v-loading="loading"
        border
        :max-height="400"
        :data="list"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
        />
        <el-table-column
          label="erp单据编码"
          align="center"
          show-overflow-tooltip
          prop="billNo"
        />
        <el-table-column
          label="erp单据状态"
          align="center"
          show-overflow-tooltip
          prop="documentStatus"
        />
        <el-table-column
          label="erp单据日期"
          align="center"
          prop="dateErp"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.dateErp) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button
          type="primary"
          :loading="ensureLoading"
          @click="handleSync"
        >确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { writeBackStockOut, getAllStockOutList } from '@/api/wms/inLibrary/periodicInventory'
export default {
  components: {
  },
  data() {
    return {
      // 同步加载
      syncLoading: false,
      // 遮罩层
      loading: false,
      // 是否显示弹出层
      open: false,
      // 确定遮罩层
      ensureLoading: false,
      // 盘点作业 list
      list: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        wmsStockCountIdErps: null
      },
      // 表单参数
      form: {},
      // 选中的参数
      currentRows: [],
      // 表单校验
      rules: {
        wmsStockCountIdErps: [{ required: true, message: '单号不能为空', trigger: 'change' }]
      }
    }
  },
  created() {
  },
  methods: {
    // 获取盘点单列表
    getList() {
      this.loading = true
      getAllStockOutList().then(res => {
        this.list = res.data
      }).finally(() => {
        this.loading = false
      })
    },
    // 手动同步
    openSelectSynchronous() {
      this.open = true
    },
    // 同步
    handleSync() {
      if (this.currentRows.length < 0) {
        this.$modal.msgWarning('请选择您要同步的盘点单!')
        return
      }
      const wmsStockCountIdErps = this.currentRows.map(item => {
        const { idErp } = item
        return {
          idErp
        }
      })
      this.ensureLoading = true
      writeBackStockOut(wmsStockCountIdErps).then(() => {
        this.$modal.msgSuccess('同步成功!')
        this.$emit('writeSuccess')
        this.close()
      }).finally(() => {
        this.ensureLoading = false
      })
    },
    // 弹窗打开事件
    dialogOpen() {
      this.$nextTick(() => {
        this.getList()
      })
    },
    // 取消
    close() {
      this.open = false
      this.$refs.multipleTable.clearSelection()
      this.form.wmsStockCountIdErps = null
    },
    // 多选
    handleSelectionChange(val) {
      this.currentRows = val
    }
  }
}
</script>

