<template>
  <a-card :bordered="false" class="goodsList-table-page-wrapper">
    <div class="goodsList-searchForm">
      <a-form layout="inline" ref="form" v-bind="formItemLayout" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="20">
          <a-col :span="6">
            <a-form-item label="商品名称" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-input id="goodsList-name" v-model="queryParam.name" allowClear :maxLength="30" placeholder="请输入商品名称" />
            </a-form-item>
          </a-col>
          <a-col :span="4">
            <a-form-item label="商品分类" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-select id="goodsList-goodsTypeNo" allowClear placeholder="请选择商品分类" v-model="queryParam.goodsTypeNo">
                <a-select-option v-for="(item,index) in goodsTypeList" :key="index" :value="item.goodsTypeNo">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="4">
            <a-form-item label="商品状态" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <v-select
                v-model="queryParam.state"
                ref="state"
                id="goodsList-state"
                code="GOODS_STATE"
                placeholder="请选择商品状态"
                allowClear></v-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="20">
          <a-col :span="6">
            <a-form-item label="供货商" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-select id="goodsList-officialPartnerNo" allowClear placeholder="请选择供货商" v-model="queryParam.officialPartnerNo">
                <a-select-option v-for="(item,index) in partnerList" :key="index" :value="item.officialPartnerNo">{{ item.name }}</a-select-option>
              </a-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>
    <div class="table-operator">
      <a-button
        v-if="$hasPermissions('B_goodsManage_add')"
        id="goodsList-btn-add"
        type="primary"
        icon="plus"
        @click="handleGoods('add')"
        style="margin-top: 18px;">新增</a-button>
    </div>
    <s-table
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      bordered>
      <!-- 商品状态 -->
      <template slot="state" slot-scope="text, record">
        <span :class="record.state==0 ? 'red' : record.state==1 ? 'green' : ''">{{ record.state==0 ? '下架' : record.state==1 ? '上架' : '--' }}</span>
      </template>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button v-if="$hasPermissions('B_goodsManage_edit')" id="goodsList-btn-edit" class="icon-blues" type="link" @click="handleGoods('edit', record)">编辑</a-button>
        <a-button v-if="$hasPermissions('B_goodsManage_shelf') && record.state" id="goodsList-btn-shelves" class="icon-orange" type="link" @click="handleShelves(record)">{{ record.state == 0 ? '上架' : record.state == 1 ? '下架' : '' }}</a-button>
        <a-button v-if="$hasPermissions('B_goodsManage_del') && record.state==0" id="goodsList-btn-delect" class="icon-red" type="link" @click="handleDel(record)">删除</a-button>
        <span v-if="!$hasPermissions('B_goodsManage_edit') && !($hasPermissions('B_goodsManage_shelf') && record.state) && !($hasPermissions('B_goodsManage_del') && record.state==0)">--</span>
      </template>
    </s-table>
  </a-card>

</template>

<script>
import { STable, VSelect } from '@/components'
import { goodsList, goodsDel, goodsState } from '@/api/goods'
import { goodsTypeQueryList } from '@/api/goodsType'
import { supplierQueryList } from '@/api/supplier'
export default {
  name: 'GoodsList',
  components: { STable, VSelect },
  data () {
    return {
      formItemLayout: {
        labelCol: { span: 7 },
        wrapperCol: { span: 17 }
      },
      // 查询参数
      queryParam: {
        name: '', //  商品名称
        officialPartnerNo: undefined, //  供货商
        goodsTypeNo: undefined, //  商品分类
        state: undefined // 商品状态
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '商品名称', dataIndex: 'name', width: 200, align: 'center' },
        { title: '商品分类', dataIndex: 'goodsTypeName', width: 200, align: 'center' },
        { title: '商品价格（乐豆）', dataIndex: 'sellGold', width: 150, align: 'center' },
        { title: '供货商简称', dataIndex: 'officialPartnerSimpleName', width: 100, align: 'center' },
        { title: '商品状态', scopedSlots: { customRender: 'state' }, width: 100, align: 'center' },
        { title: '库存数量', dataIndex: 'inventoryQty', width: 100, align: 'center' },
        { title: '已售数量', dataIndex: 'sellQty', width: 100, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 200, align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return goodsList(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
          }
        })
      },
      goodsTypeList: [], //  商品分类下拉数据
      partnerList: [] //  供应商下拉数据
    }
  },
  methods: {
    //  商品分类下拉数据
    getGoodsType () {
      goodsTypeQueryList({}).then(res => {
        if (res.status == 200) {
          this.goodsTypeList = res.data
        } else {
          this.goodsTypeList = []
        }
      })
    },
    //  供应商下拉数据
    getPartnerQueryList () {
      supplierQueryList({}).then(res => {
        if (res.status == 200) {
          this.partnerList = res.data
        } else {
          this.partnerList = []
        }
      })
    },
    //  新增、编辑商品
    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.id}` })
      }
    },
    //  删除商品
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        centered: true,
        content: '删除后原数据不可恢复，是否删除?',
        okText: '确定',
        cancelText: '取消',
        onOk () {
          goodsDel({ id: row.id }).then(res => {
          	if (res.status == 200) {
          		_this.$message.success(res.message)
          		_this.$refs.table.refresh()
          	}
          })
        }
      })
    },
    //  上下架
    handleShelves (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        centered: true,
        content: '确认更改商品状态吗？',
        okText: '确定',
        cancelText: '取消',
        onOk () {
          const state = row.state == 0 ? 1 : row.state == 1 ? 0 : ''
          goodsState({ id: row.id, state: state }).then(res => {
          	if (res.status == 200) {
          		_this.$message.success(res.message)
          		_this.$refs.table.refresh()
          	}
          })
        }
      })
    },
    // 重置
    handleReset () {
      this.queryParam.name = ''
      this.queryParam.officialPartnerNo = undefined
      this.queryParam.goodsTypeNo = undefined
      this.queryParam.state = undefined
      this.$refs.table.refresh(true)
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getGoodsType()
      vm.getPartnerQueryList()
    })
  }
}
</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;
      }
    }
    .blue {
    	color: #1890FF;
    }
    .green {
    	color: #16b50e;
    }
    .red {
    	color: red;
    }
  }
</style>
