<template>
  <el-dialog
    :title="$t('common.Please_select')"
    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="$t('md_me_materialbase.material_name')">
        <el-input
          v-model="query.search"
          clearable
          size="mini"
          :placeholder="$t('md_me_materialbase.material_name')"
          @keyup.enter.native="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"
      @select-all="onSelectAll"
      @current-change="clickChange"
    >
      <el-table-column v-if="!isSingle" type="selection" width="55" />
      <el-table-column v-if="isSingle" :label="$t('common.Please_select')" 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="material_code" :label="$t('md_me_materialbase.material_code')" width="140" />
      <el-table-column prop="material_name" :label="$t('md_me_materialbase.material_name')" width="170" show-overflow-tooltip />
      <el-table-column prop="material_spec" :label="$t('md_me_materialbase.material_spec')" width="170" show-overflow-tooltip />
      <el-table-column prop="class_name" :label="$t('md_pb_classstandard.class_name')" width="140" />
      <el-table-column prop="unit_name" :label="$t('md_pb_measureunit.unit_name')" />
      <el-table-column prop="product_series_name" :label="$t('md_me_materialbase.product_series_name')" />
      <el-table-column prop="update_optname" :label="$t('common.update_optname')" />
      <el-table-column prop="update_time" :label="$t('common.update_time')" width="135" />
    </el-table>
    <!--分页组件-->
    <pagination />
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">{{ $t('common.Cancel') }}</el-button>
      <el-button type="primary" @click="submit">{{ $t('common.Confirm') }}</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'
import i18n from '@/i18n'

export default {
  name: 'MaterialDialog',
  components: { rrOperation, pagination },
  dicts: ['is_used'],
  cruds() {
    return CRUD({ title: i18n.t('common.Please_select'), url: 'api/Materia', optShow: {}})
  },
  mixins: [presenter(), header()],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    isSingle: {
      type: Boolean,
      default: true
    }
  },
  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.$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('materialChoose', this.tableRadio)
        return
      }
      this.rows = this.$refs.table.selection
      if (this.rows.length <= 0) {
        this.$message(i18n.t('common.Please_select'))
        return
      }
      this.crud.resetQuery(false)
      this.$emit('update:dialogShow', false)
      this.$emit('materialChoose', this.rows)
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .el-dialog__body {
    padding-top: 0px;
  }
</style>

