<template>
  <div>
    <el-card>
      <el-form :model="query" label-width="auto" v-show="showSearch" size="mini" class="order-el-form">
        <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 label="所属仓库:">-->

              <!--              :isDisable.sync="defaultInv"-->
              <!--</el-form-item>-->
              <!--<el-select-->
              <!--  v-model="query.key"-->
              <!--  placeholder="请选择仓库">-->
              <!--  <el-option v-for="item in unitStorageList"-->
              <!--             :key="item.name"-->
              <!--             :label="item.name"-->
              <!--             :value="item.code"></el-option>-->
              <!--</el-select>-->
            </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-plus" @click="addWorkplace">新增工位</el-button>
        </el-button-group>
      </div>

      <el-table v-loading="loading" :data="list" style="width: 100%" :row-style="{height: '32px' }"
      >
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column label="工位编码" prop="workplaceId" width="90"></el-table-column>
        <el-table-column label="工位名称" prop="workplaceName" width="90"></el-table-column>
        <el-table-column label="所属仓库" prop="invName" width="90"></el-table-column>
        <el-table-column label="创建时间" prop="createTime" width="90"></el-table-column>
        <el-table-column label="备注" prop="remake" width="90"></el-table-column>
        <el-table-column label="操作" fixed="right" width="120">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="printLabel(scope.row)"
            >打印
            </el-button>

            <el-button
              type="text"
              size="small"
              @click.native.stop="edit(scope.row)"
            >编辑
            </el-button>
            <el-button
              type="text"
              size="small"
              @click.native.stop="deleteWorkplace(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>


      <el-dialog
        :title="fromMap[fromName]"
        :visible.sync="addWorkplaceDialogVisible"
        width="40%"
        v-if="addWorkplaceDialogVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <el-form :model="formData" style="width: 60%;" ref="dataForm"
                 label-width="auto"
        >
          <el-row :gutter="20" class="el-row">
            <el-col :span="24" class="el-col">
              <el-form-item label="工位编码:" prop="title" class="query-form-item">
                <el-input
                  disabled
                  v-model="formData.workplaceId" style="width: 80%"
                  auto-complete="off"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24" class="el-col">
              <el-form-item label="工位名称:" prop="name" class="query-form-item">
                <el-input
                  v-model="formData.workplaceName" style="width: 80%"
                  auto-complete="off"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="24" class="el-col">
              <el-form-item label="所属仓库:" prop="name" class="query-form-item">
                <invSelect :value.sync="formData.invCode" :params.sync="filterBadInv" :changeValue.sync="invChange"
                           style="width: 80%"
                />
              </el-form-item>
            </el-col>

            <el-col :span="24" class="el-col">
              <el-form-item label="备注:" prop="name" class="query-form-item">
                <el-input
                  v-model="formData.remake" style="width: 80%"
                  auto-complete="off"
                ></el-input>
              </el-form-item>
            </el-col>

          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="hideForm">取消</el-button>
          <el-button
            type="primary"
            @click.native="formSubmit()"
          >提交
          </el-button
          >
        </div>
      </el-dialog>


    </el-card>

  </div>
</template>

<script>
import {
  listPage,
  addWorkplace,
  updateWorkplace,
  deleteWorkplace,
  createWorkplaceId, printWorkLabel
} from '@/api/basic/sysWorkplaceManage'


export default {
  data() {
    return {
      query: {
        invCode: 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
    }
  },
  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
      this.getFromStorage()
    },
    getFromStorage() {
      let query = {}
      filterFromAllByUser(query)
        .then((response) => {
          this.unitStorageList = response.data || []
          console.log(this.filterQuery.invCode)
          if (this.filterQuery.invCode) {
            this.unitStorageList = this.unitStorageList.filter(item => item.code != this.filterQuery.invCode) || []
          }
          console.log(this.unitStorageList)
        })
        .catch(() => {
        })
    },
    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(() => {

      })
    },
    printLabel(row) {
      row.labelId = 5;
      printWorkLabel(row).then((response) => {
        const binaryData = [];
        binaryData.push(response);
        let url = window.URL.createObjectURL(
          new Blob(binaryData, {type: "application/pdf"})
        );
        this.loading = false;
        window.open(url);
      }).catch(() => {
        this.loading = false;
      });
    }
  },
  created() {
    this.getList()
  }
}
</script>

<style scoped>

</style>
