<template>
  <div class="receiptEdit-wrap">
    <a-page-header :ghost="false" @back="handleBack" >
      <!-- 自定义的二级文字标题 -->
      <template slot="subTitle">
        <a id="receiptEdit-back-btn" href="javascript:;" @click="handleBack">返回列表</a>
      </template>
      <!-- 操作区，位于 title 行的行尾 -->
      <template slot="extra">
        <a-button key="2" id="receiptEdit-preview-btn">打印预览</a-button>
        <a-button key="1" type="primary" id="receiptEdit-print-btn">快速打印</a-button>
      </template>
    </a-page-header>
    <!-- 内容 -->
    <a-page-header title="客户：抢个汽修厂" ></a-page-header>
    <!-- 收款信息 -->
    <a-card size="small" :bordered="false" class="receiptEdit-cont">
      <a-collapse :activeKey="['1']">
        <a-collapse-panel key="1" header="收款信息">
          <a-form-model
          	id="receiptEdit-form"
          	ref="ruleForm"
          	:model="form"
          	:rules="rules"
          	:label-col="formItemLayout.labelCol"
          	:wrapper-col="formItemLayout.wrapperCol" >
          	<a-row :gutter="15">
          		<a-col :span="8">
          			<a-form-model-item label="总计应收金额" prop="name">
                  <a-input-number id="receiptEdit-name" disabled v-model="form.name" :precision="0" :min="0" :max="999999" placeholder="请输入总计应收金额" style="width: 90%;" /> 元
          			</a-form-model-item>
          		</a-col>
          	</a-row>
            <a-row :gutter="15">
            	<a-col :span="8">
            		<a-form-model-item label="收款金额" prop="mobile">
                  <a-input-number id="receiptEdit-mobile" v-model="form.mobile" :precision="0" :min="0" :max="999999" placeholder="请输入收款金额" style="width: 90%;" /> 元
            		</a-form-model-item>
            	</a-col>
            	<a-col :span="8">
            		<a-form-model-item label="折让金额" prop="name">
            	    <a-input-number id="receiptEdit-name" v-model="form.name" :precision="0" :min="0" :max="999999" placeholder="请输入折让金额" style="width: 90%;" /> 元
            		</a-form-model-item>
            	</a-col>
            	<a-col :span="8">
            		<a-form-model-item label="结算方式" prop="paymentType">
            			<v-select
            				code="DISPATCH_FASHION"
            				id="receiptEdit-paymentType"
            				v-model="form.paymentType"
            				allowClear
            				placeholder="请选择结算方式"
            			></v-select>
            		</a-form-model-item>
            	</a-col>
            </a-row>
            <a-row :gutter="15">
            	<a-col :span="8">
            		<a-form-model-item label="实收金额" prop="name">
                  <a-input-number id="receiptEdit-name" disabled v-model="form.name" :precision="0" :min="0" :max="999999" placeholder="请输入实收金额" style="width: 90%;" /> 元
            		</a-form-model-item>
            	</a-col>
              <a-col :span="16">
              	<a-form-model-item label="备注" prop="remarks" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
              		<a-input id="receiptEdit-remarks" :maxLength="500" v-model="form.remarks" placeholder="请输入备注(最多500个字符)" allowClear />
              	</a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </a-collapse-panel>
      </a-collapse>
    </a-card>
    <!-- 收款明细 -->
    <a-card size="small" :bordered="false" class="receiptEdit-cont">
      <a-collapse :activeKey="['1']">
        <a-collapse-panel key="1">
          <template slot="header">
            收款明细
            <a-button size="small" class="add-bill-btn" id="receiptEdit-bill-btn" @click.stop="handleBill" style="margin-left: 15px;">添加单据</a-button>
            <a-button size="small" class="amount-btn" id="receiptEdit-amount-btn" @click.stop="handleAmount" style="margin-left: 15px;">分配结算金额</a-button>
          </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="receiptEdit-purchaseNo" v-model.trim="queryParam.purchaseNo" allowClear placeholder="请输入单据号"/>
                    </a-form-item>
                  </a-col>
                  <a-col :md="6" :sm="24">
                    <a-form-item label="审核时间">
                      <a-range-picker v-model="queryParam.creatDate" id="receiptEdit-creatDate"/>
                    </a-form-item>
                  </a-col>
                  <a-col :md="6" :sm="24">
                    <span class="table-page-search-submitButtons">
                      <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="receiptEdit-refresh">查询</a-button>
                      <a-button style="margin-left: 8px" @click="resetSearchForm()" :disabled="disabled" id="receiptEdit-reset">重置</a-button>
                    </span>
                  </a-col>
                </a-row>
              </a-form>
            </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>
            <div style="margin-bottom: 15px">
              <a-button type="primary" id="receiptEdit-export" :disabled="!hasSelected" :loading="loading" @click="handleNoCollection">
                批量暂不收款
              </a-button>
              <span style="margin-left: 8px">
                <template v-if="hasSelected">
                  {{ `已选 ${selectedRowKeys.length} 项` }}
                </template>
              </span>
            </div>
            <!-- 列表 -->
            <s-table
              class="sTable"
              ref="table"
              size="default"
              :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
              :rowKey="(record) => record.id"
              :columns="columns"
              :data="loadData"
              bordered>
              <!-- 结算金额 -->
              <template slot="storageQuantity" slot-scope="text, record">
                <a-input-number
                  id="receiptEdit-storageQuantity"
                  :value="record.storageQuantity"
                  :precision="0"
                  :min="0"
                  :max="999999"
                  placeholder="请输入" />
              </template>
              <!-- 操作 -->
              <template slot="action" slot-scope="text, record">
                <a-button size="small" type="primary" class="button-error" @click="handleDel(record)" id="receiptEdit-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="receiptEdit-submit-btn" 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, //  查询、重置按钮是否可操作
        formItemLayout: {
        	labelCol: { span: 8 },
        	wrapperCol: { span: 16 }
        },
        form: {
        	name: '', // 客户名称
        	mobile: '', //  联系手机
        	contact: '', //  联系人
        	provinceId: undefined, //  省
        	province: '',
        	cityId: undefined, // 市
        	city: '',
        	areaId: undefined, // 区
        	area: '',
        	address: '', //  详细地址
        	fax: '', //  客户传真
        	distributionType: '', //  配送方式
        	remarks: '', //  是否卫星仓客户
        	custType: undefined, //  客户类型
        	priceType: '', //  价格类型
        	payType: '', //  支付方式
        	paymentType: '', //  收款方式
        	contactTel: '' //  联系电话
        },
        rules: {
        	// name: [{ required: true, message: '请输入客户名称', trigger: 'blur' }],
        	mobile: [{ required: true, message: '请输入联系手机', trigger: 'blur' }],
        	provinceId: [{ required: true, message: '请选择省', trigger: 'change' }],
        	cityId: [{ required: true, message: '请选择市', trigger: 'change' }],
        	areaId: [{ required: true, message: '请选择区/县', trigger: 'change' }],
        	custType: [{ required: true, message: '请选择客户类型', trigger: 'change' }],
        	priceType: [{ required: true, message: '请选择价格类型', trigger: 'change' }],
        	payType: [{ required: true, message: '请选择支付方式', trigger: 'change' }],
        	paymentType: [{ required: true, message: '请选择收款方式', trigger: 'change' }]
        },
        // 表头
        columns: [
          { title: '序号', dataIndex: 'no', align: 'center' },
          { title: '单据号', dataIndex: 'creatDate', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '类型', dataIndex: 'custName', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '审核时间', dataIndex: 'custsName', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '金额', dataIndex: 'totalP', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
          { title: '余额', dataIndex: 'totalNums', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
          { title: '结算金额', scopedSlots: { customRender: 'storageQuantity' }, align: 'center' },
        ],
        selectedRowKeys: [], // Check here to configure the default column
        loading: false,
        // 加载数据方法 必须为 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)
          })
        },
      }
    },
    computed: {
      hasSelected () {
        return this.selectedRowKeys.length > 0
      }
    },
    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)
      },
      //  添加
      handleAdd(row){},
      //  删除
      handleDel(row){},
      //  编辑基本信息
      handleEditInfo(){
        this.openModal = true
      },
      //  提交
      handleSubmit(){
        
      },
      //  返回列表
      handleBack(){
        this.$router.push({ path: '/financialManagement/companyReceivablePayable/list'})
      },
      // 批量暂不收款
      handleNoCollection () {
        this.loading = true
        // ajax request after empty completing
        setTimeout(() => {
          this.loading = false
          this.selectedRowKeys = []
        }, 1000)
      },
      //  添加单据
      handleBill(){
        console.log(333)
      },
      //  分配结算金额
      handleAmount(){
        console.log(444)
      },
      onSelectChange (selectedRowKeys) {
        console.log('selectedRowKeys changed: ', selectedRowKeys)
        this.selectedRowKeys = selectedRowKeys
      },
      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">
  .receiptEdit-wrap{
    .receiptEdit-cont{
      margin-bottom: 15px;
      .sub-title{
        font-size: 12px;
        color: #808695;
        margin-left: 10px;
      }
      .tag-txt{
        font-size: 12px;
        color: #ed1c24;
      }
      .edit-circle-btn{
        margin-left: 15px;
        i{
          vertical-align: text-bottom;
        }
      }
      .import-btn{
        margin-left: 15px;
      }
    }
  }
</style>
