<template>
  <div>
    <a-card size="small" :bordered="false" class="supplierInfoList-wrap searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" id="supplierInfoList-form" @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-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="supplierInfoList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="supplierInfoList-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 操作按钮 -->
        <div class="table-operator">
          <a-button id="supplierInfoList-add" type="primary" v-if="$hasPermissions('B_supplierInfo_add')" @click="handleEdit()">新增</a-button>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+70+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :pageSize="30"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 状态 -->
          <template slot="enabledFlag" slot-scope="text, record">
            <a-switch
              :id="'supplierInfoList-enableFlag-'+record.id"
              v-if="$hasPermissions('B_supplierInfo_enabled')"
              checkedChildren="启用"
              unCheckedChildren="禁用"
              v-model="record.enableFlag"
              @change="changeFlagHandle(text, record)"/>
            <span v-else :style="{color:(record.enableFlag==1?'#00aa00':'#999')}"> {{ record.enableFlag==1? '已启用': '已禁用' }} </span>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              v-if="$hasPermissions('B_supplierInfo_relation')"
              class="button-warning"
              @click="handleRelated(record)"
              :id="'supplierInfoList-related-btn-'+record.id">关联产品</a-button>
            <a-button
              size="small"
              type="link"
              v-if="$hasPermissions('B_supplierInfo_edit')"
              class="button-info"
              @click="handleEdit(record)"
              :id="'supplierInfoList-edit-btn-'+record.id">编辑</a-button>
            <span v-if="!$hasPermissions('B_supplierInfo_relation') && !$hasPermissions('B_supplierInfo_edit')">--</span>
          </template>
        </s-table>
      </a-spin>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
// 接口
import { supplierList, updateEnableStatus } from '@/api/supplier'
export default {
  name: 'SupplierInfoList',
  mixins: [commonMixin],
  components: { STable, VSelect },
  data () {
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0, // 表格高度
      //  查询条件
      queryParam: {
        supplierName: '' //  供应商名称
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '13%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '供应商名称', dataIndex: 'supplierName', width: '30%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '联系人', dataIndex: 'contact', align: 'center', width: '18%', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '产品款数', dataIndex: 'totalProductCategory', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '状态', scopedSlots: { customRender: 'enabledFlag' }, width: '10%', align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '15%', align: 'center' }
      ],
      total: 0, // 总计数量
      // 加载数据方法 必须为 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
            // 获取数据总条数
            this.total = data.count || 0
            // 计算表格显示序号
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
              data.list[i].enableFlag = data.list[i].enableFlag + '' === '1'
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.queryParam.supplierName = ''
      this.$refs.table.refresh(true)
    },
    //  新增/编辑
    handleEdit (row) {
      if (row) { //  编辑
        this.$router.push({ name: `supplierInfoEdit`, params: { id: row.supplierSn } })
      } else { //  新增
        this.$router.push({ name: 'supplierInfoAdd' })
      }
    },
    // 关联产品
    handleRelated (row) {
      this.$router.push({ name: 'associatedProduct', params: { sn: row.supplierSn, name: encodeURI(row.supplierName) } })
    },
    // 修改状态
    changeFlagHandle (text, record) {
      const _data = {
        sn: record.supplierSn,
        flag: record.enableFlag ? '1' : '0'
      }
      this.spinning = true
      updateEnableStatus(_data).then(res => {
        if (res.status == 200) {
          this.$message.success(res.message)
          this.$refs.table.refresh()
          this.spinning = false
        } else {
          this.$refs.table.refresh()
          this.spinning = false
        }
      })
    },
    // 页面初始化
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 238
    }
  },
  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()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
