<template>
  <div class="auto basic-main" style="height:95%">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
          <div class="row">
            <div class="col-sm-2  form-group">
              <label class="font_normal_body">组织机构</label>
              <res-select restype='organization'
                          @res-select="$parent.$parent.getorg"
                          is-mul="false"
                          :initresid='$parent.$parent.curorgid'>
              </res-select>
            </div>
            <div  class="form-group col-sm-2">
              <label class="font_normal_body">客户编号</label>
              <input  type="text" class="form-control" v-model="model.f_userinfo_code" style="width:60%"
                      condition="f_userinfo_code = '{}' "  placeholder="客户编号"
              >
            </div>
            <div  class="form-group col-sm-2">
              <label class="font_normal_body">用户姓名</label>
              <input  type="text" class="form-control" v-model="model.f_user_name" style="width:60%"
                      condition="f_user_name = '{}'" placeholder="用户姓名"
              >
            </div>
            <div style="float: right">
              <button class="button_search"  @click="search()">查 询</button>
              <button class="button_search"  @click="$parent.$parent.kp()">开票</button>
              <div
                :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                @click="$parent.$parent.criteriaShow=!$parent.$parent.criteriaShow"
                class="button_spacing"
                style="float: right"></div>
            </div>
          </div>
          <div class="row" v-show="$parent.$parent.criteriaShow">
            <div  class="form-group col-sm-2" >
              <label class="font_normal_body">业务类型</label>
              <v-select id="f_charge_type"
                        style="width:60%"
                        v-model="model.f_type"
                        placeholder='业务类型'
                        condition="f_type ='{}'"
                        :value.sync="model.f_type"
                        :options='$parent.$parent.chargetypes'
                        close-on-select clear-button>
              </v-select>
            </div>
            <div class="form-group col-sm-2">
              <label for="startDate" class="font_normal_body">开始日期</label>
              <datepicker id="startDate" placeholder="开始日期"
                          v-model="model.startDate"
                          :value.sync="model.startDate"
                          :disabled-days-of-Week="[]"
                          condition="f_operate_date >='{}'"
                          style="width:60%"
              >
              </datepicker>
            </div>
            <div class="form-group col-sm-2">
              <label for="endDate" class="font_normal_body">结束日期</label>
              <datepicker id="endDate" placeholder="结束日期"
                          v-model="model.endDate"
                          :value.sync="model.endDate"
                          :disabled-days-of-Week="[]"
                          condition="f_operate_date <='{}'"
                          style="width:60%"
              >
              </datepicker>
            </div>
          </div>

        </div>
      </criteria>
      <data-grid  :model="model" partial='list'  class="list_area table_sy" v-ref:grid>
        <template partial='head'>
          <tr>
            <th>
              <input type="checkbox"  @click="$parent.$parent.$parent.addOrRemove($parent.$parent.$parent.selectAll)" :checked="$parent.$parent.$parent.selectAll">
            </th>
            <th>序号</th>
            <th>客户编号</th>
            <th>用户姓名</th>
            <th>用户地址</th>
            <th>气量</th>
            <th>应交金额</th>
            <th>实收金额</th>
            <th>收费类型</th>
            <th>收费时间</th>
          </tr>
        </template>
        <template partial='body'  >
          <td style="text-align:center">
            <input type="checkbox" v-model="$parent.$parent.$parent.selecteds" :value="row">
          </td>
          <td style="text-align:center">{{$index+1}}</td>
          <td style="text-align:center">{{row.f_userinfo_code}}</td>
          <td style="text-align:center">{{row.f_user_name}}</td>
          <td style="text-align:center">{{row.f_address}}</td>
          <td style="text-align:center">{{row.f_pregas}}</td>
          <td style="text-align:center">{{row.f_preamount}}</td>
          <td style="text-align:center">{{row.f_collection}}</td>
          <td style="text-align:center">{{row.f_type}}</td>
          <td style="text-align:center">{{row.f_operate_date}}</td>
        </template>
        <template partial='foot'></template>
      </data-grid>
    </criteria-paged>
  </div>
  <modal :show.sync="eticket_msg" v-ref:modal large backdrop="false" :width="900">
    <header slot="modal-header" class="modal-header">
      <button type="button" class="close" @click="eticket_msg = false"><span>&times;</span></button>
      <h4 class="modal-title">发票信息</h4>
    </header>
    <article slot="modal-body" class="modal-body">
      <div class="auto select-overspread form-horizontal">
        <div class="row auto">
          <div class="col-sm-6 form-group form-input-group" :class="[!row.f_paper_name ? 'has-error' : '']">
            <label class="control-label">开票名称:</label>
            <input class="form-control col-sm-2"
                   v-model="row.f_paper_name" placeholder="开票名称">
          </div>
          <div class="col-sm-6 form-group form-input-group">
            <nobr class="col-sm-4"><label class="control-label">是否征税:</label></nobr>
            <v-select v-model="invoice_is_pax"
                      placeholder='请选择'
                      :value.sync="invoice_is_pax"
                      close-on-select
                      :width="60"
                      :value-single="true"
                      :search="false"
                      :options='is_pax' clear-button>
            </v-select>
          </div>
          <div class="col-sm-12 form-group form-input-group">
            <label class="control-label">开户行及账号:</label>
            <input class="form-control col-sm-2"
                   v-model="row.f_paper_account" placeholder="购方开户行及账号">
          </div>
          <div class="col-sm-12 form-group form-input-group" :class="[!row.f_taxpayer_id ? 'has-error' : '']">
            <label class="control-label">纳税人识别号:</label>
            <input class="form-control col-sm-2"
                   v-model="row.f_taxpayer_id" placeholder="纳税人识别号">
          </div>
          <div class="col-sm-12 form-group form-input-group" :class="[!row.f_address_phone ? 'has-error' : '']">
            <label class="control-label">开票地址电话:</label>
            <input class="form-control col-sm-2"
                   v-model="row.f_address_phone" placeholder="开票地址">
          </div>
        </div>
      </div>
    </article>
    <footer slot="modal-footer" class="modal-footer">
      <button type="button" class="btn button_search"
              :disabled="!(row.f_taxpayer_id&&row.f_paper_name&&row.f_address_phone)"
              @click='confirm'>确认收费
      </button>
    </footer>
  </modal>
</template>

<script>
  /**
   *发票冲红管理
   */
  import {PagedList} from 'vue-client'
  import Vue from 'vue'
  export default {
    title: '结算开票',
    data () {
      return {
        criteriaShow: false,
        f_filialeids: Vue.$login.f.orgids,
        model: new PagedList('rs/sql/openSellelent', 50, {},),
        f_outlets: Vue.$login.f.f_parentname,
        f_filiale: Vue.$login.f.f_fengongsi,
        f_operator: Vue.$login.f.name,
        f_orgstr: Vue.$login.f.orgpathstr,
        jurisdiction: this.$login.r,
        f_filialeids: Vue.$login.f.f_orgids,
        selecteds: [],
        f_userinfo_id:'',
        whethertaxs:[{label:'不征税',value:'不征税'},{label:'征税',value:'征税'}],
        show:false,
        info:{},

      }
    },
    ready () {
      this.$refs.paged.$refs.cri.model.startDate = this.$login.toStandardDateString() + ' 00:00:00'
      this.$refs.paged.$refs.cri.model.endDate = this.$login.toStandardDateString() + ' 23:59:59'
      console.log('this.$login.f', Vue.$login,this.jurisdiction.includes('发票冲红'))
    },
    methods: {
      getorg (val) {
        this.f_orgid = this.$login.convertToIn(val)
        this.f_filialeids = val[0]
      },
      cancel(){
        this.show=false
        this.info={}
      },
      async kp () {
        if ([...new Set(this.selecteds.map(item => item.f_userinfo_code))].length > 1) {
          let success=0
          let errors=0
          this.$showAlert("所选择用户不是同一用户,将自动进行批量开票,请勿乱动","warning",2000)

          for(let i=0;i<this.selecteds.length;i++) {
            let param = {
              id: this.selecteds[i].id,
            }
            let getEticket = await this.$resetpost('rs/logic/combineData', {data:param, charge_type:  this.selecteds[i].f_type}, {resolveMsg: null, rejectMsg: '组织发票数据出错'})
            // 请求电子票
            console.log("getEticket",getEticket)
            await this.$resetpost('rs/logic/thirdOpenEticket', {id: getEticket.data.eticketid}, {resolveMsg: null, rejectMsg: null}).then((ret)=>{
              success=success+1
            }).catch((error) => {
              errors=errors+1
            })
          }
          this.selecteds = []
          this.$refs.paged.$refs.cri.search()
          this.$showAlert(`本次批量开票成功${success}条,失败${errors}条，失败的票据请在发票管理重新开票`,"success",3000)
        }else{
          console.log("长度为",this.selecteds.length,this.selecteds)

          if(this.selecteds.length==1){
            let param={
              id:this.selecteds[0].id,
            }
            await this.$showMessage('当前操作为补开发票，请确认是否开票', ['confirm', 'cancel']).then((res)=> {
              if (res == 'confirm') {
                this.$InvoiceService.openEticketGen(param, this.selecteds[0].f_type).then((res) => {
                  this.$refs.paged.$refs.cri.search()
                })
              }
            })
          }else{
            await this.$showMessage('当前操作为多条收费记录合并开票', ['confirm', 'cancel']).then((res)=>{
              if(res == 'confirm'){
                console.log('基本信息.lo',this.$login.f)
                let param={
                  f_filiale: this.$login.f.parentname,
                  f_operator: this.$login.f.name,
                  f_orgstr: this.$login.f.orgs,
                  f_filialeids: this.$login.f.orgid,
                  f_userinfo_id:this.f_userinfo_id,
                  f_charge_type:this.selecteds[0].f_type,
                  sell_id:[]
                }
                let temp=''
                for(let i=0;i<this.selecteds.length;i++){
                  temp+=`'${this.selecteds[i].id}',`
                  param.sell_id.push(this.selecteds[i].id)
                }
                console.log('tems', temp.substr(0, temp.length - 1), temp)
                param.ids= temp.substring(0, temp.lastIndexOf(','))
                console.log(param)
                console.log("看看参数",param.ids)
                this.$InvoiceService.openSellIvoice(param).then((res) => {
                  this.$refs.paged.$refs.cri.search()
                })
              }
            })
          }
        }
      },
      async selfSearch (args) {
        if (!this.f_orgid) {
          this.getorg([this.$login.f.orgid])
        }
        args.condition=` f_orgid='${this.f_filialeids}' and ${args.condition}`
        await this.model.search(args.condition, this.model)
        this.f_userinfo_id=this.model.rows[0].f_userinfo_id
        console.log("res",this.model)
      },
      search () {
        this.$refs.paged.$refs.cri.search()
        this.$dispatch('refresh')
      },
      addOrRemove (is) {
        this.selecteds = []
        if (!is) { // 添加本页所有数据到ids中
          this.model.rows.forEach((row) => {
            this.selecteds.push(row)
          })
          this.selectAll = true
        } else { // 从ids中取消所有的数据
          this.selecteds = []
          this.selectAll = false
        }
        console.log("selectdde",this.selecteds)
      }
    },
    computed: {
      chargetypes(){
        return [{label: '全部', value: ''},...this.$appdata.getParam('收费类型')]
      },
    },
    watch:{
      selecteds(){
        console.log("框框的长度",this.selecteds.length)
      }
    }
  }
</script>
