<template>
  <el-dialog
    title="货位选择"
    append-to-body
    :visible.sync="dialogVisible"
    destroy-on-close
    width="1000px"
    @close="close"
    @open="open"
  >
    <!-- 搜索 -->
    <el-row>
      <el-col :span="6">
        <el-cascader
          v-model="query.sect"
          placeholder="所属库区"
          :options="sects"
          :props="{ checkStrictly: true }"
          clearable
          @change="sectQueryChange"
        />
      </el-col>
      <el-col :span="6">
        <el-input
          v-model="query.point_code"
          clearable
          style="width: 200px"
          size="mini"
          placeholder="输入点位编码、名称"
          prefix-icon="el-icon-search"
          class="filter-item"
        /> </el-col>

      <el-col :span="6">
        <rrOperation />
      </el-col>
    </el-row>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%;"
      border
      :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
      @select="handleSelectionChange"
      @select-all="onSelectAll"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="point_code" label="点位编码" />
      <el-table-column prop="point_name" label="点位名称" />
      <el-table-column prop="region_name" label="区域名称" />
      <el-table-column prop="product_area" label="生产车间" />
    </el-table>
    <!--分页组件-->
    <pagination />
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import CRUD, { header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/Pagination'
import crudRegion from '@/views/wms/scheduler_manage/region/region'

export default {
  name: 'StructDialog',
  components: { rrOperation, pagination },
  cruds() {
    return CRUD({
      title: '点位',
      optShow: {},
      url: 'api/mpsSaleOrder',
      idField: 'region_code',
      sort: 'region_code,desc',
      query: { search: '', is_lock: '1', lock_type: '', sect_id: '', stor_id: '' },
      crudMethod: { ...crudRegion }
    })
  },
  mixins: [presenter(), header()],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    sectProp: {
      type: Object
    },
    storId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      sects: [],
      classes: [],
      dialogVisible: false,
      sect: {},
      checkrow: {},
      rows: [],
      dialogDis: true,
      lock: ''
    }
  },
  watch: {
    dialogShow: {
      handler(newValue, oldValue) {
        this.dialogVisible = newValue
      }
    },
    sectProp: {
      handler(newValue, oldValue) {
        this.sect = newValue
      }
    }
  },
  methods: {
    /**
     * 接受父组件传值
     * @param msg
     */
    getMsg(msg) {
      this.dialogDis = msg
      this.lock = '1'
    },
    [CRUD.HOOK.beforeRefresh]() {
      this.query.lock_type = this.lock
      return true
    },
    open() {
      crudRegion.getRegionSelect({ 'stor_id': this.storId }).then(res => {
        this.sects = res.content
      })
      if (this.sect) {
        this.query.sect = this.sect
        if (this.sect.length === 1) {
          this.query.stor_id = this.sect[0]
          this.query.stor_id = this.storId
        }
        if (this.sect.length === 0) {
          this.query.sect_id = ''
          this.query.stor_id = ''
        }
        if (this.sect.length === 2) {
          this.query.stor_id = this.sect[0]
          this.query.sect_id = this.sect[1]
        }
      }
      this.query.is_lock = '1'
      this.query.lock_type = this.lock
      this.query.is_used = '1'

      this.query.stor_id = this.storId
      this.crud.toQuery()
    },
    handleSelectionChange(val, row) {
      if (val.length > 1) {
        this.$refs.table.clearSelection()
        this.$refs.table.toggleRowSelection(val.pop())
      } else {
        this.checkrow = row
      }
    },
    sectQueryChange(val) {
      if (val.length === 1) {
        this.crud.query.stor_id = val[0]
        this.crud.query.sect_id = ''
      }
      if (val.length === 0) {
        this.crud.query.sect_id = ''
        this.crud.query.stor_id = ''
      }
      if (val.length === 2) {
        this.crud.query.stor_id = val[0]
        this.crud.query.sect_id = val[1]
      }
    },
    onSelectAll() {
      this.$refs.table.clearSelection()
    },
    close() {
      this.crud.resetQuery()
      this.query.sect = null
      this.$emit('update:dialogShow', false)
    },
    submit() {
      this.rows = this.$refs.table.selection
      if (this.rows.length <= 0) {
        this.$message('请先勾选仓位')
        return
      }
      this.$emit('update:dialogShow', false)
      this.$emit('tableChanged', this.checkrow)
    }
  }
}
</script>

