<template>
  <a-card :bordered="false" class="goodsList-table-page-wrapper">
    <div class="goodsList-searchForm">
      <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="48">
          <a-col :span="6">
            <a-form-item label="商品名称" :label-col="{ span:7 }" :wrapper-col="{ span:17}">
              <a-input id="goodsList-name" allowClear v-decorator="[ 'queryParam.name', { initialValue: queryParam.name, getValueFromEvent: $filterEmpty, rules: [] }]" :maxLength="30" placeholder="请输入商品名称" />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="供货商" :label-col="{ span:7 }" :wrapper-col="{ span:17}">
              <a-input id="goodsList-supplierName" allowClear v-decorator="[ 'queryParam.supplierName', { initialValue: queryParam.supplierName, getValueFromEvent: $filterEmpty, rules: [] }]" :maxLength="30" placeholder="请输入供货商" />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="商品状态" :label-col="{ span:7 }" :wrapper-col="{ span:17}">
              <v-select
                v-model="queryParam.goodsStatus"
                ref="goodsStatus"
                id="goodsList-status"
                code="SETTLE_STATUS_ALL"
                placeholder="请选择商品状态"
                allowClear></v-select>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-button class="handle-btn serach-btn" id="goodsList-btn-serach" type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button class="handle-btn" type="" id="goodsList-btn-reset" @click="handleReset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-divider />
    <div class="table-operator">
      <a-button id="goodsList-btn-add" type="primary" icon="plus" @click="handleGoods('add')">新增</a-button>
    </div>
    <s-table
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      bordered>
      <!-- 商品状态 -->
      <template slot="orderStatus" slot-scope="text, record">
        <span>{{ record.orderStatus==1 ? '已启用' : '已禁用' }}</span>
      </template>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-icon title="编辑" type="edit" id="goodsList-btn-edit" class="actionBtn icon-blues" @click="handleGoods('edit', record)" />
        <a-icon title="删除" type="delete" id="goodsList-btn-delect" class="actionBtn icon-red" @click="handleDel(record)" />
      </template>
    </s-table>
  </a-card>

</template>

<script>
import { STable, VSelect } from '@/components'
export default {
  name: 'GoodsList',
  components: { STable, VSelect },
  data () {
    return {
      // 查询参数
      queryParam: {
        name: '', //  商品名称
        supplierName: '', //  供货商
        goodsStatus: '' // 商品状态
      },
      loading: false, // 导出loading
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '商品名称', dataIndex: 'orderDate', width: 200, align: 'center' },
        { title: '商品价格（乐豆）', dataIndex: 'userMoblie', width: 200, align: 'center' },
        { title: '供货商', dataIndex: 'orderNo', width: 200, align: 'center' },
        { title: '商品状态', scopedSlots: { customRender: 'orderStatus' }, width: 200, align: 'center' },
        { title: '库存数量', dataIndex: 'orderPrice', width: 200, align: 'center' },
        { title: '已售数量', dataIndex: 'orderNum', width: 200, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 150, align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // return getTenantsList(Object.assign(parameter, this.queryParam)).then(res => {
        //   if (res.status == 200) {
        //     const no = (res.data.pageNo - 1) * res.data.pageSize
        //     for (let i = 0; i < res.data.list.length; i++) {
        //       const _item = res.data.list[i]
        //       _item.no = no + i + 1
        //       _item.status = _item.status + '' === '1'
        //     }
        //     return res.data
        //   }
        // })
        return new Promise((resolve) => {
          const data = [
            { no: 1, orderDate: '2020-10-30 09:14', userMoblie: '15709252525', orderNo: 'NO54456798', orderStatus: '1', orderPrice: '155', orderNum: '67' },
            { no: 2, orderDate: '2020-10-30 09:14', userMoblie: '15709252525', orderNo: 'NO54456798', orderStatus: '0', orderPrice: '155', orderNum: '67' },
            { no: 3, orderDate: '2020-10-30 09:14', userMoblie: '15709252525', orderNo: 'NO54456798', orderStatus: '1', orderPrice: '155', orderNum: '67' },
            { no: 4, orderDate: '2020-10-30 09:14', userMoblie: '15709252525', orderNo: 'NO54456798', orderStatus: '1', orderPrice: '155', orderNum: '67' },
            { no: 5, orderDate: '2020-10-30 09:14', userMoblie: '15709252525', orderNo: 'NO54456798', orderStatus: '0', orderPrice: '155', orderNum: '67' },
            { no: 6, orderDate: '2020-10-30 09:14', userMoblie: '15709252525', orderNo: 'NO54456798', orderStatus: '0', orderPrice: '155', orderNum: '67' }
          ]
          resolve(data)
        })
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.handleReset()
    })
  },
  methods: {
    moment,
    // 不可选日期
    disabledDate (date, dateStrings) {
      return date && date.valueOf() > Date.now()
    },
    // 创建时间change
    onChange (dates, dateStrings) {
      if ((dates, dateStrings)) {
        this.searchForm.beginDate = dateStrings[0]
        this.searchForm.endDate = dateStrings[1]
      }
    },
    //  新增、编辑商品
    handleGoods (type, row) {
      if (type == 'add') {
        this.$router.push({ path: '/shop/goods/add' })
      } else if (type == 'edit') {
        this.$router.push({ path: `/shop/goods/edit/${row.no}` })
      }
    },
    //  删除商品
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        centered: true,
        content: '删除后原数据不可恢复，是否删除?',
        okText: '确定',
        cancelText: '取消',
        onOk () {
          // tenantsDelete({
          // 	id: row.id
          // }).then(res => {
          // 	if (res.status == 200) {
          // 		_this.$message.success('res.message')
          // 		_this.$refs.table.refresh()
          // 	}
          // })
        }
      })
    },
    // 重置
    handleReset () {
      this.queryParam.name = ''
      this.queryParam.supplierName = ''
      this.queryParam.goodsStatus = null
      this.$refs.table.refresh(true)
    }
  }
}
</script>
<style lang="less" scoped>
  .goodsList-table-page-wrapper{
    .goodsList-searchForm{
      .ant-form-inline .ant-form-item{
        width: 100%;
      }
      // 搜索框的下间距
      .ant-form-item {
      	margin-bottom: 10px;
      }
      .handle-btn{
        margin-top: 4px;
      }
      .serach-btn{
        margin-right: 10px;
      }
    }
    .export-btn{
      background-color: #ff9900;
      border-color: #ff9900;
      color: #fff;
    }
    .export-btn.ant-btn:focus:not(.ant-btn-primary):not(.ant-btn-danger), .export-btn.ant-btn:hover:not(.ant-btn-primary):not(.ant-btn-danger){
      color: #fff;
      border-color: #ff9900;
    }
    .actionBtn {
    	font-size: 20px;
    	padding: 0 10px;
    }
    .blue {
    	color: #1890FF;
    }
    .green {
    	color: #16b50e;
    }
    .red {
    	color: red;
    }
  }
</style>
