<template>
  <div>
    <Modal v-model="supplyModal" v-drag width="550" :mask-closable="false">
      <p slot="header">选择供应商</p>
      <Form id="supply-library-form" inline onsubmit="return false">
        <Form-item style="width: 100%;">
          <Input
            name="name"
            placeholder="请输入供应商名称,回车查询"
            style="width:100%"
            icon="ios-search-strong"
            @on-enter="_search"
          />
        </Form-item>
      </Form>
      <radioTable
        ref="pageTable"
        :page-columns="pageColumns"
        :table-height="tableHeight"
        :get-page="getPage"
        hide-elevator
        hide-sizer
        @on-result-change="_tableResultChange"
      >
        <el-table-column
          v-for="item in pageColumns"
          :key="item.key"
          sortable
          show-overflow-tooltip
          :prop="item.key"
          :label="item.title"
          :min-width="item.width"
          :fixed="item.fixed?item.fixed:undefined"
        >
          <template slot-scope="scope">
            <span>{{ scope.row[item.key] }}</span>
          </template>
        </el-table-column>
      </radioTable>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
  </div>
</template>
<script>
import radioTable from '../../components/table/radioTable'
import { supplier } from '../../api'
export default {
  components: { radioTable },
  data() {
    return {
      supplyModal: false,
      tableHeight: '300',
      pageColumns: [{ title: '供应商名称', key: 'name' }],
      getPage: {},
      modalTitle: '报告模板',
      footerList: [
        { id: '', type: '', name: '取消' },
        { id: '', type: 'primary', name: '保存' }
      ],
      supply: ''
    }
  },
  methods: {
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'radioSelect':
          console.log(data)
          this.supply = data
          break
        default:
          this._page()
      }
    },
    _open() {
      this.supplyModal = true
      this.$refs.footerModal._hideLoading()
      this._clearPage()
      this._page()
    },
    _search() {
      this.$refs.pageTable._pageChange(1)
    },
    _clearPage() {
      $('#supply-library-form')
        .find('input')
        .each(function() {
          $(this).val('')
        })
    },
    async _page() {
      const data = this.$serialize('supply-library-form')
      Object.assign(data, this.$refs.pageTable._searchParams())
      const result = await supplier.pageSupplier(data)
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
        this.$refs.pageTable.templateRadio = ''
      }
    },
    _footerResult(msg) {
      switch (msg) {
        case '取消':
          this.supplyModal = false
          break
        case '保存':
          this._ok()
          break
      }
    },
    _ok() {
      if (this.supply === '') {
        this.$Message.warning('请选择一条数据!')
      } else {
        this.$emit('on-result-change', this.supply)
        this.supplyModal = false
      }
    }
  }
}
</script>
