<template>
  <a-card size="small" :bordered="false" class="supplierInfoList-wrap">
    <!-- 搜索条件 -->
    <div 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-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 id="supplierInfoList-add" type="primary" class="button-error" @click="handleEdit()">新增</a-button>
    </div>
    <!-- 总计 -->
    <a-alert type="info" showIcon style="margin-bottom:15px">
      <div slot="message">合计：<strong>{{ total }}</strong>条</div>
    </a-alert>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      bordered>
      <!-- 状态 -->
      <template slot="enabledFlag" slot-scope="text, record">
        <a-switch checkedChildren="启用" unCheckedChildren="禁用" v-model="record.enableFlag" @change="changeFlagHandle(text, record)"/>
      </template>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button size="small" type="link" class="button-warning" @click="handleRelated(record)" id="supplierInfoList-related-btn">关联产品</a-button>
        <a-button size="small" type="link" class="button-info" @click="handleEdit(record)" id="supplierInfoList-edit-btn">编辑</a-button>
      </template>
    </s-table>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import { supplierList, updateEnableStatus } from '@/api/supplier'
export default {
  components: { STable, VSelect },
  data () {
    return {
      queryParam: { //  查询条件
        supplierName: '' //  供应商名称
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '供应商名称', dataIndex: 'supplierName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '联系人', dataIndex: 'contact', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '产品款数', dataIndex: 'totalProductCategory', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '状态', scopedSlots: { customRender: 'enabledFlag' }, width: 150, align: 'center', fixed: 'right' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 150, align: 'center', fixed: 'right' }
      ],
      total: 0,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return supplierList(Object.assign(parameter, this.queryParam)).then(res => {
          const data = res.data
          this.total = data.count
          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
          return data
        })
      }
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.queryParam.supplierName = ''
      this.$refs.table.refresh(true)
    },
    //  新增/编辑
    handleEdit (row) {
      if (row) { //  编辑
        this.$router.push({ name: `supplierInfoEdit`, params: { id: row.id } })
      } 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'
      }
      updateEnableStatus(_data).then(res => {
        if (res.status + '' === '200') {
          this.$message.success(res.message)
        } else {
          record.enableFlag = !record.enableFlag
        }
      })
    }
  }
}
</script>
