<template>
  <div>
    <el-card style="margin-top: -20px">
      <el-form :model="query" label-width="auto" v-show="showSearch" size="mini">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="当前仓库:" class="query-form-item">
              <invSelect :value.sync="query.invCode" :params.sync="filterBadInv" :changeValue.sync="invChange"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="编码/名称:" class="query-form-item">
              <el-input v-model="query.key" placeholder="请输入编码/工位名称" clearable>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div class="top-right-btn">
        <el-button-group>
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
          <el-button type="primary" icon="el-icon-check" @click="bindWork">绑定</el-button>
        </el-button-group>
      </div>

      <el-table v-loading="loading" :data="list" style="width: 100%" :row-style="{height: '32px' }"
                @current-change="productRowClick"
      >
        <el-table-column label width="45">
          <template slot-scope="scope">
            <el-radio :label="scope.row.id" v-model="radioCheck"><span></span></el-radio>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column label="工位编码" prop="workplaceId"></el-table-column>
        <el-table-column label="工位名称" prop="workplaceName"></el-table-column>
        <el-table-column label="所属仓库" prop="invName"></el-table-column>
        <el-table-column label="备注" prop="remake"></el-table-column>
      </el-table>
    </el-card>

  </div>
</template>

<script>
import {
  listPage,
  addWorkplace,
  updateWorkplace,
  deleteWorkplace,
  createWorkplaceId
} from '@/api/basic/workPlace/sysWorkplaceManage'
import {bindWork} from "@/api/collect/collectOrder";


export default {
  name: "bindWorkDialog",
  props: {
    prescribeListData: {
      type: Object,
      required: true,
    },
    closeDialog: {
      type: Function,
      required: true,
    },
  },
  data() {
    return {
      query: {
        invCode: null,//this.$store.getters.locInvCode
        page: 1,
        limit: 20
      },
      showSearch: true,
      loading: false,
      list: [],
      addWorkplaceDialogVisible: false,
      fromName: 'add',
      fromMap: {
        'add': '新增工位',
        'edit': '编辑工位'
      },
      formData: {},
      workplaceCode: '6001',
      unitStorageList: [],
      filterBadInv: true,
      radioCheck: null,
      currentRow: {},
    }
  },
  methods: {
    hideSearch() {
      this.showSearch = !this.showSearch
    },
    onSubmit() {
      this.query.page = 1
      this.getList()
    },
    onReset() {
      this.$router.push({
        path: ''
      })
      this.query = {
        page: 1,
        limit: 20
      }
      this.getList()
    },
    getList() {
      listPage(this.query).then(res => {
        if (res.code != 20000) {
          return this.$message.error('错误错误')
        }
        this.list = res.data.list || []
      })
    },
    addWorkplace() {
      this.fromName = 'add'
      createWorkplaceId().then(res => {
        if (res.code == 20000) {
          if (res.data == null) {
            this.formData.workplaceId = this.workplaceCode + 1001
            this.addWorkplaceDialogVisible = true
          } else {
            this.formData = {}
            this.formData.workplaceId = res.data
            this.addWorkplaceDialogVisible = true
          }
        } else {
          this.$message.error('获取错误')
        }
      }).catch(() => {
        this.$message.error('获取错误')
      })
    },
    hideForm() {
      this.addWorkplaceDialogVisible = false
    },
    invChange(invCode) {
      this.filterQuery.invCode = invCode
    },
    formSubmit() {
      if (this.fromName == 'add') {
        addWorkplace(this.formData).then(res => {
          if (res.code != 20000) {
            return this.$message.error('新增错误')
          }
          this.addWorkplaceDialogVisible = false
          this.$message.success(res.data)
          this.getList()
        }).cache(() => {
          this.addWorkplaceDialogVisible = false
          this.$message.error('新增错误')
        })
      } else {
        updateWorkplace(this.formData).then(res => {
          if (res.code != 20000) {
            return this.$message.error('更新错误')
          }
          this.addWorkplaceDialogVisible = false
          this.$message.success(res.data)
          this.getList()
        }).catch(() => {
          this.addWorkplaceDialogVisible = false
          this.$message.error('更新错误')
        })
      }

    },
    edit(row) {
      this.formData = row
      this.fromName = 'edit'
      this.addWorkplaceDialogVisible = true
    },
    deleteWorkplace(row) {
      this.$confirm('此操作将永久删除该工位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteWorkplace(row).then(res => {
          if (res.code != 20000) {
            this.$message.error('删除错误')
          } else {
            this.$message.success(res.data)
            this.getList()
          }
        })
      }).catch(() => {

      })
    },
    productRowClick(row) {
      this.radioCheck = row.workplaceId
      this.currentRow = row;
    },

    bindWork() {
      let query = {
        collOrder: this.prescribeListData,
        workplaceCode: this.currentRow.workplaceId
      }
      bindWork(query).then(res => {
        if (res.code != 20000) {
          this.$message.error(res.message)
        } else {
          this.$message.success(res.data)
          this.closeDialog();
        }
      })
    },

  },
  created() {
    this.getList()
  }
}
</script>

<style scoped>
.query-form {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
}

.query-form-item {
  margin-right: 5px;
  margin-bottom: 6px;
}
</style>
