<template>
  <el-dialog
    title="父类点位选择"
    append-to-body
    :visible.sync="dialogVisible"
    destroy-on-close
    width="1000px"
    @close="close"
    @open="open"
  >
    <el-form
      :inline="true"
      class="demo-form-inline"
      label-position="right"
      label-width="80px"
      label-suffix=":"
    >
      <el-form-item label="点位名称">
        <el-input
          v-model="query.blurry"
          clearable
          size="mini"
          placeholder="点位名称"
          @keyup.enter.native="crud.toQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用">
        <el-switch
          v-model="query.is_used"
          :active-value="true"
          :inactive-value="false"
          @change="crud.toQuery()"
        />
      </el-form-item>
      <rrOperation />
    </el-form>

    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%;"
      size="mini"
      border
      :cell-style="{'text-align':'center'}"
      :header-cell-style="{background:'#f5f7fa',color:'#606266','text-align':'center'}"
      @select="handleSelectionChange"
      @current-change="clickChange"
    >
      <el-table-column v-if="!isSingle" type="selection" width="55" />
      <el-table-column v-if="isSingle" label="选择" width="55">
        <template slot-scope="scope">
          <el-radio v-model="tableRadio" :label="scope.row"><i /></el-radio>
        </template>
      </el-table-column>
      <el-table-column prop="point_code" label="点位名称" width="160" />
      <el-table-column prop="point_name" label="点位编码" width="180" show-overflow-tooltip />
      <el-table-column prop="region_code" label="区域编码" :min-width="flexWidth('region_code',crud.data,'区域编码')" />
      <el-table-column prop="region_name" label="区域名称" :min-width="flexWidth('region_name',crud.data,'区域名称')" />
      <el-table-column prop="point_type_name" label="点位类型" :min-width="flexWidth('point_type_name',crud.data,'点位类型')"/>
      <el-table-column prop="point_status_name" label="点位状态" :min-width="flexWidth('point_status_name',crud.data,'点位类型')"/>
      <el-table-column prop="remark" label="备注" :min-width="flexWidth('remark',crud.data,'备注')" />
      <el-table-column prop="is_used" label="是否启用" :min-width="flexWidth('is_used',crud.data,'是否启用')">
        <template slot-scope="scope">
          {{scope.row.is_used?'是':'否'}}
        </template>
      </el-table-column>
    </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 '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'PointDialog',
  components: { rrOperation, pagination },
  dicts: ['is_used'],
  cruds() {
    return CRUD({
      title: '点位',
      url: 'api/schBasePoint',
      optShow: {},
      query: {
        is_used: true,
        parent_point: true
      }
    })
  },
  mixins: [presenter(), header()],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    isSingle: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: false,
      tableRadio: null,
      tableData: []
    }
  },
  watch: {
    dialogShow: {
      handler(newValue) {
        this.dialogVisible = newValue
      }
    }
  },
  methods: {
    clickChange(item) {
      this.tableRadio = item
    },
    open() {

    },
    handleSelectionChange(val, row) {
      if (val.length > 1 && this.isSingle) {
        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() {
      // 处理单选
      if (this.isSingle && this.tableRadio) {
        this.$emit('update:dialogShow', false)
        this.$emit('tableChanged', this.tableRadio)
        return
      }
      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('tableChanged', this.rows)
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .el-dialog__body {
    padding-top: 0px;
  }
</style>

