<template>
  <div>
    <el-form :model="query" label-width="auto" v-show="showSearch" size="mini" class="order-el-form">
      <el-row :gutter="24">
        <!--<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-col :span="8">
          <el-form-item label="所属组别:" class="query-form-item">
            <el-select v-model="query.code" placeholder="请选择所属组别"
                       style="width: 80%" clearable
            >
              <el-option
                v-for="item in fromDeptOptions"
                :key="item.code"
                :label="item.label"
                :value="item.code"
              >
                <span style="float: left">{{ item.label }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="工位类型:" prop="workPlaceClass" class="query-form-item" clearable>
            <el-select v-model="query.workPlaceClass" style="width: 80%" placeholder="工位类型">
              <el-option label="采集工位" :value="1"></el-option>
              <el-option label="上货工位" :value="2"></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="addWork">选入工位</el-button>
      </el-button-group>
    </div>
    <el-divider style="margin: 15px"></el-divider>
    <el-table v-loading="loading" :data="list" style="width: 100%" :row-style="{height: '32px' }" highlight-current-row
              @current-change="handCurrentChange" ref="table"
    >
      <!--<el-table-column label width="45">-->
      <!--  <template slot-scope="scope">-->
      <!--    <el-radio :label="scope.row.workplaceId" v-model="radioCheck"><span></span></el-radio>-->
      <!--  </template>-->
      <!--</el-table-column>-->
      <el-table-column type="selection" width="55" :selectable="checkSelectable">
      </el-table-column>
      <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="120"></el-table-column>
      <el-table-column label="工位类型" prop="workPlaceClass" width="90">
        <template slot-scope="scope">
          <span>{{ workPlaceClasss[scope.row.workPlaceClass] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工位状态" prop="workplaceStatus" width="90">
        <template slot-scope="scope">
          <span>{{ workplaceStatusMap[scope.row.workplaceStatus] }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="工位类型" prop="workplaceType" width="90">-->
      <!--  <template slot-scope="scope">-->
      <!--    <span>{{ workplaceTypes[scope.row.workplaceType] }}</span>-->
      <!--  </template>-->
      <!--</el-table-column>-->
      <el-table-column label="所属组别" prop="categoryName" width="90">
        <template slot-scope="scope">
          <span v-if="scope.row.categoryName !== '全部'">{{ scope.row.categoryName }}</span>
          <span v-else></span>
        </template>
      </el-table-column>
      <el-table-column label="所属部门" prop="deptCodeName" width="90"></el-table-column>
      <el-table-column label="工位负责人" prop="employeeName" 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="50">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="checkDetail(scope.row)"
          >详情
          </el-button>
          <!--@click.native.stop="printLabel(scope.row)"-->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="query.page"
      :limit.sync="query.limit"
      @pagination="getList"
    />

    <el-dialog
      title="工位详情"
      :visible.sync="WorkplaceDialogVisible"
      width="60%"
      v-if="WorkplaceDialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form :model="formData" style="width: 80%;" ref="dataForm" :rules="formRules"
               label-width="auto"
      >
        <el-row :gutter="24" class="el-row">
          <el-col :span="12" class="el-col">
            <el-form-item label="工位编码:" prop="workplaceId" class="query-form-item">
              <el-input
                disabled
                v-model="formData.workplaceId" style="width: 100%"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="el-col">
            <el-form-item label="工位名称:" prop="" class="query-form-item">
              <el-input
                disabled
                v-model="formData.workplaceName" style="width: 100%"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12" class="el-col">
            <el-form-item label="工位状态:" prop="workplaceStatus" class="query-form-item">
              <el-select v-model="formData.workplaceStatus" style="width: 100%" placeholder="工位状态" disabled>
                <el-option label="暂停" :value="0"></el-option>
                <el-option label="启用" :value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="el-col">
            <el-form-item prop="deptCode" label="所属部门:" >
              <el-select v-model="formData.deptCode" placeholder="请选择部门" clearable disabled
                         style="width: 100%"
                         @change="handleInvChange"
              >
                <el-option v-for="item in deptList" :key="item.id" :label="item.name" :value="item.code"/>
              </el-select>
            </el-form-item>
          </el-col>
          <!--<el-col  :span="12" class="el-col">-->
          <!--  <el-form-item label="所属部门:" class="query-form-item">-->
          <!--    <deptSelect  :value.sync="formData.deptCode"  style="width: 80%" :changeValue.sync="handleInvChange"/>-->
          <!--    &lt;!&ndash;<deptSelect :value.sync="query.invCode" :params.sync="filterBadInv" :changeValue.sync="invChange"/>&ndash;&gt;-->
          <!--  </el-form-item>-->
          <!--</el-col>-->

        </el-row>

        <el-row :gutter="24" class="el-row">
          <!--<el-col :span="24" class="el-col">-->
          <!--  <el-form-item label="所属仓库:" prop="invCode" class="query-form-item">-->
          <!--    <el-select v-model="formData.invCode" placeholder="请选择所属仓库"-->
          <!--               style="width: 80%"-->
          <!--               clearable-->
          <!--               @change="handleInvChange"-->
          <!--    >-->
          <!--      <el-option-->
          <!--        v-for="item in invList"-->
          <!--        :key="item.name"-->
          <!--        :label="item.name"-->
          <!--        :value="item.code"-->
          <!--      >-->
          <!--        <span style="float: left">{{ item.name }}</span>-->
          <!--      </el-option>-->
          <!--    </el-select>-->
          <!--  </el-form-item>-->
          <!--</el-col>-->
          <el-col :span="12" class="el-col">
            <el-form-item label="工位负责人:" prop="chargeUser" class="query-form-item">
              <el-input
                disabled
                v-model="formData.employeeName" style="width: 100%"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="el-col">
            <el-form-item label="创建时间:" prop="createTime" class="query-form-item">
              <el-input
                disabled
                v-model="formData.createTime" style="width: 100%"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>

          <!--<el-col :span="24" class="el-col">-->
          <!--  <el-form-item label="单据类型:" prop="busType" class="query-form-item">-->
          <!--    <el-select v-model="formData.orderId" placeholder="请选择单据类型"-->
          <!--               style="width: 80%"-->
          <!--               clearable-->
          <!--    >-->
          <!--      <el-option-->
          <!--        v-for="item in busTypes"-->
          <!--        :key="item.name"-->
          <!--        :label="item.name"-->
          <!--        :value="item.code"-->
          <!--      >-->
          <!--        <span style="float: left">{{ item.name }}</span>-->
          <!--      </el-option>-->
          <!--    </el-select>-->
          <!--  </el-form-item>-->
          <!--</el-col>-->
          <!--<el-col :span="24" class="el-col">-->
          <!--  <el-form-item label="所属组别:" prop="grouping" class="query-form-item">-->
          <!--    <el-select v-model="formData.constituencies" placeholder="请选择所属组别"-->
          <!--               style="width: 80%"-->
          <!--               clearable-->
          <!--    >-->
          <!--      <el-option-->
          <!--        v-for="item in fromDeptOptions"-->
          <!--        :key="item.code"-->
          <!--        :label="item.label"-->
          <!--        :value="item.code"-->
          <!--      >-->
          <!--        <span style="float: left">{{ item.label }}</span>-->
          <!--      </el-option>-->
          <!--    </el-select>-->
          <!--  </el-form-item>-->
          <!--</el-col>-->

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

        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPage,
  addWorkplace,
  updateWorkplace,
  deleteWorkplace,
  createWorkplaceId, printWorkLabel, getUsers
} from '@/api/basic/workPlace/sysWorkplaceManage'
import { filterList, getInvListByUser } from '@/api/system/invWarehouse'
import { getTree } from '@/api/basic/collectPoint/collectPointManage'
import { getCollectBusType } from '@/api/basic/collectPoint/gatherOrderType'
import { userBindWork } from '@/api/basic/collectPoint/userWorkplace'
import panelGroup from '@/views/dashboard/PanelGroup'

export default {
  props: {
    closeDialog: {
      type: Function,
      required: true
    },
    userId: {
      type: Object,
      required: true
    },
    workPlaceCode: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      query: {
        page: 1,
        limit: 10,
        workPlaceClass: null,
        code: null,
        key: ""
      },
      workQuery: {
        userId: null,
        workplaceId: []
      },
      showSearch: true,
      Dictionary: true,
      loading: false,
      list: [],
      treeList: [],
      addWorkplaceDialogVisible: false,
      WorkplaceDialogVisible: false,
      fromName: 'add',
      fromMap: {
        'add': '新增工位',
        'edit': '编辑工位'
      },
      formData: {},
      workplaceCode: '6001',
      unitStorageList: [],
      invList: [],
      fromDeptOptions: [],
      filterBadInv: true,
      formRules: {
        workplaceName: [
          { required: true, message: '请输入工位名称', trigger: 'blur' }
        ],
        invCode: [
          { required: true, message: '请选择仓库', trigger: 'blur' }
        ]
      },
      invQuery: {
        code: ''
      },
      users: [],
      busTypes: [],
      operationTypes: {
        1: '扫码生成业务单',
        2: '按单校验三期',
        3: '按单不校验三期'
      },
      checkInserts: {
        1: '自动添加',
        2: '自动不添加',
        3: '根据提醒决定'
      },
      warnTypeMap: {
        1: '信息提醒',
        2: '信息弹框提醒',
        3: '不提醒'
      },
      corpTypeMap: {
        1: '客户',
        2: '供应商',
        3: '内部科室',
        4: '特殊往来'
      },
      workplaceStatusMap: {
        0: '暂停',
        1: '启用'
      },
      workPlaceClasss: {
        1: '采集工位',
        2: '上货工位'
      },
      workplaceTypes: {
        1: '拆零',
        2: '取整',
        3: '全部'
      },
      radioCheck: '',
      rowData: {},
      rowList: [],
      deptList: [],
      total:0,
    }
  },
  methods: {
    hideSearch() {
      this.showSearch = !this.showSearch
    },
    onSubmit() {
      this.query.page = 1
      this.getList()
    },
    onReset() {
      this.$router.push({
        path: ''
      })
      this.query = {
        page: 1,
        limit: 10,
        key: "",
        workPlaceClass: null,
        code: null
      }
      this.getList()
    },
    getList() {
      listPage(this.query).then(res => {
        if (res.code != 20000) {
          return this.$message.error('错误错误')
        }
        this.list = res.data.list || []
        this.total = res.data.total || 0
      })
    },
    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') {
        this.$refs['dataForm'].validate((valid) => {
          if (!valid) {
            this.$message.error('请完善必填信息')
          } else {
            addWorkplace(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('新增错误')
            })
          }
        })
      } 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(() => {

      })
    },
    getInvList() {
      let query = {
        advanceType: 1
      }
      getInvListByUser(query)
        .then((response) => {
          this.invList = response.data || []
        })
        .catch(() => {
        })
    },
    getDeptList() {
      this.deptList = []
      let data =
        {
          enable: true
        }
      filterList(data).then(res => {
        if (res.code == 20000) {
          this.deptList = res.data.list || []
        }
      })
    },
    handleInvChange(invCode) {
      this.invQuery.code = invCode
      getUsers(this.invQuery).then(res => {
        if (res.code == 20000) {
          this.users = res.data || []
        } else {
          this.$message.error('错误')
        }
      })
    },
    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
      })
    },
    getLstTree() {
      getTree(this.query)
        .then((response) => {
          var invlist = response.data || []
          this.treeList = this.handleTree(invlist, 'code', 'parentCode')
        })
        .catch(() => {
        })
    },
    getBusType() {
      let query = {
        page: 1,
        limit: 10
      }
      getCollectBusType(query)
        .then((response) => {
          this.busTypes = response.data.list || []
          // this.filterQuery.action = this.busTypes[0].action;
          // this.getList();
        })
        .catch(() => {
        })
    },
    getTree() {
      getTree(this.query)
        .then((response) => {
          var treeList = response.data || []
          const filteredList = treeList.filter(item => item.parentCode != null)
          this.fromDeptOptions = filteredList
        })
        .catch(() => {
        })
    },
    handCurrentChange(row) {
      this.rowList = row
      // this.workQuery.workplaceId = row
      // this.rowData = row
    },
    handleNodeClick(row) {
      this.query.code = row.code
      this.getList()
    },

    //选入工位
    addWork() {
      let selectList = this.$refs.table.selection
      let list = selectList.map(item => item.workplaceId)
      this.workQuery.userId = this.userId
      this.workQuery.workplaceId = list
      if (list.length == 0) {
        // return this.closeDialog()
        return this.$message.error('未选择工位或该用户已没有可增加工位')
      }
      userBindWork(this.workQuery).then(res => {
        if (res.code == 20000) {
          this.closeDialog()
          this.$message.success('成功!!')
        } else {
          this.$message.error(res.message)
        }
      }).catch(() => {

      })
    },
    checkDetail(row) {
      this.formData = row
      this.WorkplaceDialogVisible = true
    },
    checkSelectable(row) {
      if (this.workPlaceCode == null) {
        return true
      } else {
        if (this.workPlaceCode.length > 0) {
          return this.workPlaceCode.indexOf(row.workplaceId) === -1
        } else {
          return true
        }
      }
    }
  },
  created() {
    this.getList()
    this.getInvList()
    this.getDeptList()
    // this.getBusType()
    this.getTree()
    this.getLstTree()
  }
}
</script>

<style scoped>
.iconButton {
  margin-left: 0px;
  height: 30px;
}
</style>
