<template>
  <a-card size="small" :bordered="false" class="supplierInfoList-wrap">
    <!-- 搜索条件 -->
    <div ref="tableSearch" class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="15">
          <a-col :md="6" :sm="24">
            <a-form-item label="供应商名称">
              <a-input id="supplierInfoList-supplierName" v-model.trim="queryParam.supplierName" allowClear placeholder="请输入供应商名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="启用状态">
              <v-select code="ENABLE_FLAG" id="supplierInfoList-enabledFlag" v-model="queryParam.enabledFlag" allowClear placeholder="请选择启用状态"></v-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="supplierInfoList-refresh">查询</a-button>
            <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="supplierInfoList-reset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 操作按钮 -->
    <div class="table-operator">
      <a-button
        v-if="$hasPermissions('B_supplier_supplierInfo_add')"
        id="supplierInfoList-add"
        type="primary"
        class="button-error"
        @click="handleEdit()">新增</a-button>
    </div>
    <!-- 列表 -->
    <s-table
      class="sTable fixPagination"
      ref="table"
      :style="{ height: tableHeight+84.5+'px' }"
      size="small"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :scroll="{ y: tableHeight }"
      :defaultLoadData="false"
      bordered>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button
          v-if="$hasPermissions('B_supplier_supplierInfo_edit') && record.tenantId!=0"
          size="small"
          type="link"
          class="button-primary"
          @click="handleEdit(record)"
          id="supplierInfoList-edit-btn">编辑</a-button>
        <span v-else>--</span>
      </template>
    </s-table>
    <!-- 供应商 -->
    <supplier-info-edit-modal :itemId="itemId" :openModal="openModal" @close="closeModal" />
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import { supplierList } from '@/api/supplier'
import supplierInfoEditModal from './editModal.vue'
import { commonMixin } from '@/utils/mixin'
export default {
  name: 'SupplierList',
  components: { STable, VSelect, supplierInfoEditModal },
  mixins: [commonMixin],
  data () {
    return {
      queryParam: { //  查询条件
        supplierName: '', //  供应商名称
        enabledFlag: undefined //  状态
      },
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '供应商名称', dataIndex: 'supplierName', width: '17%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '地址', dataIndex: 'address', width: '15%', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '联系人', dataIndex: 'contactPerson', width: '13%', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '联系电话', dataIndex: 'contactTel', width: '11%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '主营内容', dataIndex: 'businessScope', width: '14%', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '创建时间', dataIndex: 'createDate', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '状态', dataIndex: 'enabledFlagDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '8%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return supplierList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
              const provinceName = data.list[i].provinceName ? data.list[i].provinceName : ''
              const cityName = data.list[i].cityName ? data.list[i].cityName : ''
              const countyName = data.list[i].countyName ? data.list[i].countyName : ''
              const supplierAddress = data.list[i].supplierAddress ? data.list[i].supplierAddress : ''
              data.list[i].address = provinceName + cityName + countyName + supplierAddress
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      itemId: '', //  当前id
      openModal: false // 弹框
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.queryParam.supplierName = ''
      this.queryParam.enabledFlag = undefined
      this.$refs.table.refresh(true)
    },
    //  新增/编辑
    handleEdit (row) {
      this.itemId = row && row.id ? row.id : null
      this.openModal = true
    },
    // 关闭弹框
    closeModal () {
      this.itemId = ''
      this.openModal = false
      this.$refs.table.refresh(true)
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 235
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
