<template>
  <a-card size="small" :bordered="false" class="chainStockReportList-wrap">
    <!-- 搜索条件 -->
    <div class="table-page-search-wrapper">
      <a-form-model
        id="chainStockReportList-form"
        ref="ruleForm"
        :model="queryParam"
        :rules="rules"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        @keyup.enter.native="handleSearch" >
        <a-row :gutter="15">
          <a-col :md="6" :sm="24">
            <a-form-model-item label="连锁店" prop="targetName">
              <a-select
                placeholder="请选择连锁店"
                id="chainStockReportList-targetName"
                allowClear
                v-model="queryParam.targetName"
                :showSearch="true"
                option-filter-prop="children"
                :filter-option="filterOption">
                <a-select-option v-for="item in allocateTypeList" :key="item.brandSn" :value="item.brandSn">{{ item.brandName }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-model-item label="产品编码">
              <a-input id="productInfoList-code" v-model.trim="queryParam.code" allowClear placeholder="请输入产品编码"/>
            </a-form-model-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-model-item label="产品名称">
              <a-input id="productInfoList-name" v-model.trim="queryParam.name" allowClear placeholder="请输入产品名称"/>
            </a-form-model-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="原厂编码">
                <a-input id="productInfoList-origCode" v-model.trim="queryParam.origCode" allowClear placeholder="请输入原厂编码"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="产品品牌">
                <a-select
                  placeholder="请选择产品品牌"
                  id="productInfoList-productBrandSn"
                  allowClear
                  v-model="queryParam.productBrandSn"
                  :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-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="产品分类">
                <a-cascader
                  @change="changeProductType"
                  change-on-select
                  v-model="productType"
                  expand-trigger="hover"
                  :options="productTypeList"
                  :fieldNames="{ label: 'productTypeName', value: 'productTypeSn', children: 'children' }"
                  id="productInfoList-productType"
                  placeholder="请选择产品分类"
                  allowClear />
              </a-form-model-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="24" style="margin-top: 3px;">
            <a-button type="primary" @click="handleSearch" :disabled="disabled" id="chainStockReportList-refresh">查询</a-button>
            <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="chainStockReportList-reset">重置</a-button>
            <a @click="advanced=!advanced" style="margin-left: 8px">
              {{ advanced ? '收起' : '展开' }}
              <a-icon :type="advanced ? 'up' : 'down'"/>
            </a>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
    <!-- 合计 -->
    <a-alert type="info" showIcon style="margin-bottom:15px">
      <div class="ftext" slot="message">现有库存总数量（个）：
        <strong>
          <!-- {{ currentStock.currentQty || 0 }} -->207
        </strong>；现有库存总成本(￥)：
        <strong>
          <!-- {{ currentStock.putCost || 0 }} -->1550.5
        </strong>。</div>
    </a-alert>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="small"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :scroll="{ x: 1170 }"
      bordered>
      <!-- 现有库存数量 -->
      <template slot="currentStockQty" slot-scope="text, record">
        {{ (record.currentStockQty + record.freezeQty) || 0 }}
        <span v-if="record.freezeQty">（冻结{{ record.freezeQty }}）</span>
      </template>
      <template slot="currentStockCost" slot-scope="text, record">
        {{ (Number(record.currentStockCost)*100 + Number(record.freezeCost)*100)/100 || 0 }}
      </template>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button size="small" type="link" class="button-success" @click="goDetail(record)" id="chainStockReportList-detail-btn">详情</a-button>
      </template>
    </s-table>
    <!-- 详情 -->
    <chain-stock-report-detail-modal :openModal="openModal" :itemId="itemId" @close="closeModal" />
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import chainStockReportDetailModal from './detailModal.vue'
import { dealerProductBrandQuery } from '@/api/dealerProductBrand'
import { dealerProductTypeList } from '@/api/dealerProductType'
// import { allocateBillList, allocateBillDel, allocateBillAudit, allocateBillExport } from '@/api/allocateBill'
// import { allocateTypeAllList } from '@/api/allocateType'
export default {
  components: { STable, VSelect, chainStockReportDetailModal },
  data () {
    return {
      advanced: false, // 高级搜索 展开/关闭
      tableHeight: 0,
      queryParam: { //  查询条件
        targetName: undefined,
        code: '', //  产品编码
        name: '', //  产品名称
        origCode: '', //  原厂编码
        productBrandSn: undefined, //  产品品牌
        productTypeSn1: '', //  产品一级分类
        productTypeSn2: '', //  产品二级分类
        productTypeSn3: '' //  产品三级分类
      },
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      rules: {
        'targetName': [{ required: true, message: '请选择连锁店', trigger: 'change' }]
      },
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false,
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '产品编码', dataIndex: 'code', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'name', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'origCode', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '现有库存数量(个)', dataIndex: 'currentStockQty', scopedSlots: { customRender: 'currentStockQty' }, width: 165, align: 'center', sorter: true },
        { title: '现有库存成本(￥)', dataIndex: 'currentStockCost', scopedSlots: { customRender: 'currentStockCost' }, width: 165, align: 'center', sorter: true },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 100, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        // return allocateBillList(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
        //   }
        //   this.disabled = false
        //   return data
        // })
        const _this = this
        return new Promise(function (resolve, reject) {
          const data = {
            pageNo: 1,
            pageSize: 10,
            list: [
              { id: '1', code: 'GA-11078N', name: '冠牌空气滤清器 丰田新款佳美2.4 12款逸致1.8', origCode: '17807-20040', currentStockQty: '207', freezeQty: '', currentStockCost: '1550.50', freezeCost: '' }
            ],
            count: 10
          }
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = i + 1
          }
          _this.disabled = false
          resolve(data)
        })
      },
      productType: [],
      productBrandList: [], //  品牌下拉数据
      productTypeList: [], //  分类下拉数据
      allocateTypeList: [], //  调拨类型
      itemId: '', //  当前id
      openModal: false //  弹框
    }
  },
  methods: {
    //  查询
    handleSearch () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$refs.table.refresh(true)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.queryParam.targetName = undefined
      this.queryParam.code = ''
      this.queryParam.name = ''
      this.queryParam.origCode = ''
      this.queryParam.productBrandSn = undefined
      this.queryParam.productTypeSn1 = ''
      this.queryParam.productTypeSn2 = ''
      this.queryParam.productTypeSn3 = ''
      this.productType = []
      this.$refs.table.refresh(true)
    },
    //  详情
    goDetail (row) {
      this.itemId = row.stockSn
      this.openModal = true
    },
    //  关闭弹框
    closeModal () {
      this.itemId = ''
      this.openModal = false
    },
    //  产品分类  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] : ''
    },
    //  产品品牌  列表
    getProductBrand () {
      dealerProductBrandQuery({ 'sysFlag': '0' }).then(res => {
        if (res.status == 200) {
          this.productBrandList = res.data
        } else {
          this.productBrandList = []
        }
      })
    },
    //  产品分类  列表
    getProductType () {
      dealerProductTypeList({ 'sysFlag': '0' }).then(res => {
        if (res.status == 200) {
          this.productTypeList = res.data
        } else {
          this.productTypeList = []
        }
      })
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getProductBrand()
      vm.getProductType()
    })
  }
}
</script>
