<template>
  <div class="productInfoList-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="productInfoList-code" v-model.trim="queryParam.productCode" allowClear placeholder="请输入产品编码"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="产品名称">
              <a-input id="productInfoList-name" v-model.trim="queryParam.productName" allowClear placeholder="请输入产品名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="原厂编码">
              <a-input id="productInfoList-origCode" v-model.trim="queryParam.productOrigCode" allowClear placeholder="请输入原厂编码"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-item label="车架号(VIN)">
                <a-input id="productInfoList-vinNo" v-model.trim="queryParam.vinNo" allowClear placeholder="请输入车架号(VIN)"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="产品品牌">
                <a-select
                  placeholder="请选择产品品牌"
                  id="productInfoList-productBrandSn"
                  allowClear
                  v-model="queryParam.brandSn"
                  :showSearch="true"
                  option-filter-prop="children"
                  :filter-option="filterOption">
                  <a-select-option v-for="item in productBrandList" :key="item.brandSn" :value="item.brandSn">{{ item.brandName }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="产品类别">
                <a-cascader
                  @change="changeProductType"
                  change-on-select
                  v-model="productType"
                  :options="productTypeList"
                  :fieldNames="{ label: 'productTypeName', value: 'productTypeSn', children: 'children' }"
                  id="productInfoList-productType"
                  placeholder="请选择产品分类"
                  allowClear />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="车型">
                <v-select code="CHECK_ENABLE_STATE" id="productInfoList-state" v-model="queryParam.state" allowClear placeholder="请选择车型"></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="仓库">
                <a-select
                  placeholder="请选择仓库"
                  id="productInfoList-warehouseSn"
                  allowClear
                  v-model="queryParam.warehouseSn"
                  :showSearch="true"
                  option-filter-prop="children"
                  :filter-option="filterOption">
                  <a-select-option v-for="item in warehouseList" :key="item.warehouseSn" :value="item.warehouseSn">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="24">
            <a-button style="margin-bottom: 18px;" type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="productInfoList-refresh">查询</a-button>
            <a-button style="margin: 0 0 18px 8px" @click="resetSearchForm" :disabled="disabled" id="productInfoList-reset">重置</a-button>
            <a @click="advanced=!advanced" style="margin:0 15px 0 8px">
              {{ advanced ? '收起' : '展开' }}
              <a-icon :type="advanced ? 'up' : 'down'"/>
            </a>
            <a-popover>
              <template slot="content">双击列表配件可快速选中添加</template>
              <a-icon type="question-circle" theme="twoTone" />
            </a-popover>
            <a-icon style="margin:0 15px" type="setting" theme="twoTone" />
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :customRow="handleClickRow"
      :scroll="{ x: 1530, y: 150 }"
      :pageSize="30"
      bordered>
      <!-- 售价 -->
      <template slot="price" slot-scope="text, record">
        <div @dblclick.stop>
          <a-input-number
            v-model="record.dealerProduct.cityPrice"
            :precision="2"
            :min="0"
            :max="999999"
            placeholder="请输入" />
        </div>
      </template>
      <!-- 销售数量 -->
      <template slot="nums" slot-scope="text, record">
        <div @dblclick.stop>
          <a-input-number
            v-model="record.salesNums"
            :precision="0"
            :min="0"
            :max="999999"
            placeholder="请输入" />
        </div>
      </template>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button size="small" type="link" @click="handleAdd(record)" id="productInfoList-edit-btn">添加</a-button>
        <a-divider type="vertical" style="margin: 0;" />
        <a-button size="small" type="link" @click="handleDetail(record)" id="productInfoList-detail-btn" style="margin-left: 15px;">销售记录</a-button>
      </template>
    </s-table>
    <!-- 销售记录 -->
    <product-salesRecord-modal :openModal="openModal" :itemId="itemId" @close="closeModal" />
  </div>
</template>

<script>
import { productBrandQuery } from '@/api/productBrand'
import { productTypeQuery } from '@/api/productType'
import { querySumByProductLocation } from '@/api/stock'
import { warehouseAllList } from '@/api/warehouse'
import productSalesRecordModal from './productSalesRecordModal.vue'
import { STable, VSelect } from '@/components'
export default {
  name: 'QueryPart',
  components: { STable, VSelect, productSalesRecordModal },
  data () {
    return {
      advanced: false, // 高级搜索 展开/关闭
      productType: [],
      queryParam: { //  查询条件
        productCode: '', //  产品编码
        productName: '', //  产品名称
        productOrigCode: '', //  原厂编码
        brandSn: undefined, //  产品品牌
        productTypeSn1: '', //  产品一级分类
        productTypeSn2: '', //  产品二级分类
        productTypeSn3: '', //  产品三级分类
        warehouseSn: undefined //  仓库
      },
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false, // 导出loading
      productBrandList: [], //  下拉数据
      productTypeList: [], //  产品类别  下拉数据
      warehouseList: [], // 仓库列表
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: 140, align: 'center', sorter: true },
        { title: '产品名称', dataIndex: 'productName', align: 'center' },
        { title: '原厂编码', dataIndex: 'productOrigCode', width: 140, align: 'center' },
        { title: '品牌', dataIndex: 'brandName', width: 100, align: 'center', sorter: true },
        { title: '仓库', dataIndex: 'warehouseSn', width: 100, align: 'center', sorter: true },
        { title: '仓位', dataIndex: 'warehouseLocationSn', width: 100, align: 'center' },
        { title: '售价', scopedSlots: { customRender: 'price' }, width: 150, align: 'center' },
        { title: '销售数量', scopedSlots: { customRender: 'nums' }, width: 150, align: 'center' },
        { title: '单位', dataIndex: 'unit', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 170, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return querySumByProductLocation(Object.assign(parameter, this.queryParam)).then(res => {
          const 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
            data.list[i].salesNums = 1
          }
          this.disabled = false
          // 产品品牌
          if (this.productBrandList.length == 0) {
            this.getProductBrand()
          }
          // 产品分类
          if (this.productTypeList.length == 0) {
            this.getProductType()
          }
          // 仓库
          if (this.warehouseList.length == 0) {
            this.getWarehouse()
          }
          return data
        })
      },
      openModal: false, //  查看客户详情  弹框
      itemId: '' //  当前产品id
    }
  },
  methods: {
    // 双击列表
    handleClickRow (record) {
      const _this = this
      return {
        on: {
          dblclick: (event) => {
            event.stopPropagation()
            _this.$emit('add', record)
          }
        }
      }
    },
    //  重置
    resetSearchForm () {
      this.queryParam.productName = ''
      this.queryParam.productOrigCode = ''
      this.queryParam.brandSn = undefined
      this.queryParam.productTypeSn1 = ''
      this.queryParam.productTypeSn2 = ''
      this.queryParam.productTypeSn3 = ''
      this.queryParam.warehouseSn = undefined
      this.productType = []
      this.$refs.table.refresh(true)
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    // 选择配件
    handleAdd (row) {
      this.$emit('add', row)
    },
    // 销售记录
    handleDetail (row) {
      this.itemId = row.id
      this.openModal = true
    },
    //  关闭弹框
    closeModal () {
      this.itemId = ''
      this.openModal = false
    },
    // 仓库
    getWarehouse () {
      warehouseAllList({}).then(res => {
        if (res.status == 200) {
          this.warehouseList = res.data
        } else {
          this.warehouseList = []
        }
      })
    },
    //  产品品牌  列表
    getProductBrand () {
      productBrandQuery({}).then(res => {
        if (res.status == 200) {
          this.productBrandList = res.data
        } else {
          this.productBrandList = []
        }
      })
    },
    //  产品分类  列表
    getProductType () {
      productTypeQuery({}).then(res => {
        if (res.status == 200) {
          this.productTypeList = res.data
        } else {
          this.productTypeList = []
        }
      })
    },
    //  产品分类  change
    changeProductType (val, opt) {
      this.queryParam.productTypeSn1 = val[0] ? val[0] : ''
      this.queryParam.productTypeSn2 = val[1] ? val[1] : ''
      this.queryParam.productTypeSn3 = val[2] ? val[2] : ''
    }
  }
}
</script>
