<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-select
          v-model="query.area_type"
          :disabled="typeDisable"
          clearable
          placeholder="全部"
          class="filter-item"
        >
          <el-option
            v-for="item in sch_area_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-col>
      <el-col :span="6">
        <el-input
          v-model="query.name"
          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 type="index" label="序号" width="50" align="center" />
      <el-table-column prop="point_code" label="点位编码" align="center" />
      <el-table-column prop="point_name" label="点位名称" align="center" />
      <el-table-column prop="area_type_name" label="所属区域" align="center" />
    </el-table>
    <!--分页组件-->
    <pagination />
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import crudPoint from '@/api/wms/sch/point'
import CRUD, { crud, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import checkoutbill, { schAreaType } from '@/api/wms/st/core/outbill/checkoutbill'

export default {
  name: 'PointDialog',
  components: { crudOperation, rrOperation, pagination },
  cruds() {
    return CRUD({ title: '点位',
      url: 'api/point',
      query: { area_type: '', name: '' },
      crudMethod: { ...crudPoint },
      optShow: {}})
  },
  mixins: [presenter(), header(), crud()],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    dialogAreatype: {
      type: String
    },
    dialogTypedisable: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      classes: [],
      dialogVisible: false,
      areaType: '',
      typeDisable: false,
      sch_area_type: [],
      checkrow: {},
      lock: '',
      rows: []
    }
  },
  watch: {
    dialogShow: {
      handler(newValue, oldValue) {
        this.dialogVisible = newValue
      }
    },
    dialogTypedisable: {
      handler(newValue, oldValue) {
        this.typeDisable = newValue
      }
    },
    dialogAreatype: {
      handler(newValue, oldValue) {
        this.areaType = newValue
        checkoutbill.schAreaType({ ids: this.areaType }).then(res => {
          this.sch_area_type = res
        })
      }
    }
  },
  methods: {
    /**
     * 接受父组件传值
     * @param msg
     */
    getMsg(msg) {
      this.lock = '00'
      this.areaType = msg
    },
    open() {
      this.crud.query.ids = this.areaType
      this.crud.query.lock_type = this.lock
      checkoutbill.schAreaType({ ids: this.areaType }).then(res => {
        this.sch_area_type = res
      })
      this.crud.toQuery()
    },
    handleSelectionChange(val, row) {
      if (val.length > 1) {
        this.$refs.table.clearSelection()
        this.$refs.table.toggleRowSelection(val.pop())
      } else {
        this.checkrow = row
      }
    },
    onSelectAll() {
      this.$refs.table.clearSelection()
    },
    close() {
      this.crud.resetQuery(false)
      this.$emit('update:dialogShow', false)
    },
    submit() {
      this.rows = this.$refs.table.selection
      if (this.rows.length <= 0) {
        this.$message('请先勾选点位')
        return
      }
      this.crud.resetQuery(false)
      this.$emit('update:dialogShow', false)
      this.$emit('PointChanged', this.checkrow)
    }
  }
}
</script>

