<template>
  <div class="selfDisk-wrap">
    <a-page-header :ghost="false" @back="handleBack" class="selfDisk-back">
      <!-- 自定义的二级文字标题 -->
      <template slot="subTitle">
        <a id="selfDisk-back-btn" href="javascript:;" @click="handleBack">返回列表</a>
      </template>
    </a-page-header>
    <!-- 选择产品 -->
    <a-card size="small" :bordered="false" class="selfDisk-cont">
      <a-collapse :activeKey="['1']">
        <a-collapse-panel key="1">
          <template slot="header">选择产品</template>
          <!-- 选择产品 -->
          <div>
            <!-- 搜索条件 -->
            <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="selfDisk-bundleName" v-model.trim="queryParam.bundleName" allowClear placeholder="产品名称/产品编码/原厂编码"/>
                    </a-form-item>
                  </a-col>
                  <a-col :md="6" :sm="24">
                    <a-form-item label="产品品牌">
                      <a-select
                        placeholder="请选择"
                        id="selfDisk-state"
                        allowClear
                        v-model="queryParam.dataSourceNo"
                        :showSearch="true"
                        option-filter-prop="children"
                        :filter-option="filterOption">
                        <a-select-option v-for="item in brandData" :key="item.salesChannelNo" :value="item.salesChannelNo">{{ item.salesChannelName }}</a-select-option>
                      </a-select>
                    </a-form-item>
                  </a-col>
                  <a-col :md="6" :sm="24">
                    <a-form-item label="产品分类">
                      <a-cascader :options="typeData" expand-trigger="hover" id="selfDisk-state" placeholder="请选择产品类别" allowClear v-model="queryParam.brand" />
                    </a-form-item>
                  </a-col>
                  <template v-if="advanced">
                    <a-col :md="6" :sm="24">
                      <a-form-item label="库存">
                        <a-select
                          placeholder="请选择"
                          id="selfDisk-state"
                          allowClear
                          v-model="queryParam.dataSourceNo"
                          :showSearch="true"
                          option-filter-prop="children"
                          :filter-option="filterOption">
                          <a-select-option v-for="item in brandData" :key="item.salesChannelNo" :value="item.salesChannelNo">{{ item.salesChannelName }}</a-select-option>
                        </a-select>
                      </a-form-item>
                    </a-col>
                    <a-col :md="6" :sm="24">
                      <a-form-item label="仓库">
                        <a-select
                          placeholder="请选择"
                          id="selfDisk-state"
                          allowClear
                          v-model="queryParam.dataSourceNo"
                          :showSearch="true"
                          option-filter-prop="children"
                          :filter-option="filterOption">
                          <a-select-option v-for="item in brandData" :key="item.salesChannelNo" :value="item.salesChannelNo">{{ item.salesChannelName }}</a-select-option>
                        </a-select>
                      </a-form-item>
                    </a-col>
                    <a-col :md="6" :sm="24">
                      <a-form-item label="仓位">
                        <a-select
                          placeholder="请选择"
                          id="selfDisk-state"
                          allowClear
                          v-model="queryParam.dataSourceNo"
                          :showSearch="true"
                          option-filter-prop="children"
                          :filter-option="filterOption">
                          <a-select-option v-for="item in brandData" :key="item.salesChannelNo" :value="item.salesChannelNo">{{ item.salesChannelName }}</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="selfDisk-refresh">查询</a-button>
                    <a-button style="margin: 0 0 18px 8px" @click="resetSearchForm" :disabled="disabled" id="selfDisk-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>
            </div>
            <!-- 列表 -->
            <s-table
              class="sTable"
              ref="table"
              size="default"
              :rowKey="(record) => record.id"
              :columns="columns"
              :data="loadData"
              :scroll="{ x: 1420 }"
              bordered>
              <!-- 操作 -->
              <template slot="action" slot-scope="text, record">
                <a-button size="small" type="link" @click="handleAdd(record)" id="selfDisk-add-btn">添加</a-button>
              </template>
            </s-table>
          </div>
        </a-collapse-panel>
      </a-collapse>
    </a-card>
    <!-- 已选产品 -->
    <a-card size="small" :bordered="false" class="selfDisk-cont">
      <a-collapse :activeKey="['1']">
        <a-collapse-panel key="1">
          <template slot="header">
            已选产品
            <a-button size="small" class="import-btn" id="selfDisk-import-btn" @click.stop="handleImport">导入明细</a-button>
          </template>
          <!-- 已选产品 -->
          <div>
            <!-- 总计 -->
            <a-alert type="info" showIcon style="margin-bottom:15px">
              <div slot="message">产品款数 <strong>6</strong> ，采购数量合计 <strong>6</strong> ，采购金额合计￥<strong>6.33</strong></div>
            </a-alert>
            <!-- 列表 -->
            <s-table
              class="sTable"
              ref="table"
              size="default"
              :rowKey="(record) => record.id"
              :columns="chooseColumns"
              :data="chooseLoadData"
              :scroll="{ x: 1420 }"
              bordered>
              <!-- 操作 -->
              <template slot="action" slot-scope="text, record">
                <a-button size="small" type="link" class="button-error" @click="handleDel(record)" id="selfDisk-del-btn">删除</a-button>
              </template>
            </s-table>
          </div>
        </a-collapse-panel>
      </a-collapse>
    </a-card>
    <a-affix :offset-bottom="0">
      <div style="text-align: center;width: 100%;background-color: #fff;padding: 12px 0;box-shadow: 0 0 20px #dcdee2;">
        <a-button type="primary" id="warehouseAllocationEdit-submit" size="large" class="button-primary" @click="handleSubmit" style="padding: 0 60px;">提交</a-button>
      </div>
    </a-affix>
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
export default {
  components: { STable, VSelect },
  data () {
    return {
      queryParam: {

      },
      brandData: [], //  产品品牌  下拉数据
      typeData: [], //  产品类别  下拉数据
      advanced: false, //  高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '产品编码', dataIndex: 'creatDate', width: 140, align: 'center', sorter: true, customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'custName', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '原厂编码', dataIndex: 'totalP', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品品牌', dataIndex: 'custssName', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品级别', dataIndex: 'custsdName', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '仓库', dataIndex: 'totalNums', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '仓位', dataIndex: 'totalNsums', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '库存数量', dataIndex: 'storageQuantity', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', scopedSlots: { customRender: 'outOfStockNum' }, width: 100, align: 'center' },
        { title: '最后入库时间', scopedSlots: { customRender: 'purchaseNum' }, width: 160, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 100, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        // return customerBundleDelayList( 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', purchaseNo: 'jgqp11111111111', creatDate: '产品1', custName: 'jgqp111123545', totalP: '箭冠品牌', totalNums: '产品分类1', totalPrice: '5', payType: '122' }
            ],
            count: 10
          }
          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
          resolve(data)
        })
      },
      // 表头
      chooseColumns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '产品编码', dataIndex: 'creatDate', width: 140, align: 'center', sorter: true },
        { title: '产品名称', dataIndex: 'custName', align: 'center' },
        { title: '原厂编码', dataIndex: 'totalP', width: 140, align: 'center' },
        { title: '产品品牌', dataIndex: 'custsNasme', width: 100, align: 'center' },
        { title: '产品级别', dataIndex: 'custsNdme', width: 100, align: 'center' },
        { title: '仓库', dataIndex: 'totalNums', width: 100, align: 'center' },
        { title: '仓位', dataIndex: 'totalNsums', width: 100, align: 'center' },
        { title: '库存数量', dataIndex: 'storageQuantity', width: 100, align: 'center' },
        { title: '单位', scopedSlots: { customRender: 'outOfStockNum' }, width: 100, align: 'center' },
        { title: '最后入库时间', scopedSlots: { customRender: 'purchaseNum' }, width: 160, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 100, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      chooseLoadData: parameter => {
        this.disabled = true
        // return customerBundleDelayList( 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', purchaseNo: 'jgqp11111111111', custssName: 0, creatDate: '产品1', custName: 'jgqp111123545', totalP: '箭冠品牌', totalNums: '产品分类1', totalPrice: '5', payType: '122' },
              { id: '2', purchaseNo: 'jgqp11111111111', custssName: 0, creatDate: '产品1', custName: 'jgqp111123545', totalP: '箭冠品牌', totalNums: '产品分类1', totalPrice: '5', payType: '122' },
              { id: '3', purchaseNo: 'jgqp11111111111', custssName: 0, creatDate: '产品1', custName: 'jgqp111123545', totalP: '箭冠品牌', totalNums: '产品分类1', totalPrice: '5', payType: '122' }
            ],
            count: 10
          }
          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
          resolve(data)
        })
      },
      openModal: false // 选择基本信息弹框是否显示
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.queryParam.orderBundleNo = ''
      this.queryParam.orderBundle.custMobile = ''
      this.queryParam.bundleName = ''
      this.queryParam.itemName = ''
      this.oldTime = undefined
      this.newTime = undefined
      this.$refs.table.refresh(true)
    },
    //  提交
    handleSubmit () {},
    //  添加
    handleAdd (row) {},
    //  删除
    handleDel (row) {},
    //  编辑基本信息
    handleEditInfo () {
      this.openModal = true
    },
    //  基本信息  保存
    handleOk () {

    },
    //  导入明细
    handleImport () {
      console.log(333)
    },
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/inventoryManagement/inventoryChecking/list' })
    },
    filterOption (input, option) {
      	return (
      		option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      	)
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.openModal = false
    })
  }
}
</script>

<style lang="less">
  .selfDisk-wrap{
    .selfDisk-back{
      margin-bottom: 15px;
    }
    .selfDisk-cont{
      margin-bottom: 15px;
      .import-btn{
        margin-left: 15px;
      }
    }
  }
</style>
