<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.open_eTicket()">开票</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>序号</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">{{$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>
          <td>
            <button class="btn btn-link" @click="$parent.$parent.$parent.kp(row)">开具发票</button>
          </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" style="width: 100px">开票名称:</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" :class="[!invoice_is_pax ? 'has-error' : '']">
            <nobr class="col-sm-4"><label class="control-label" style="width: 100px">是否征税:</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-6 form-group form-input-group"  :class="[!row.f_paper_type || !row.f_paper_type === '' ? 'has-error' : '']">
            <label class="control-label" style="width: 100px">发票种类:</label>
            <v-select v-model="row.f_paper_type"
                      placeholder='请选择'
                      :value.sync="row.f_paper_type"
                      close-on-select
                      :width="60"
                      :value-single="true"
                      :search="false"
                      :options='invoice_types' clear-button>
            </v-select>
          </div>
          <div class="col-sm-12 form-group form-input-group">
            <label class="control-label" style="width: 100px">开户行及账号:</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" >
            <label class="control-label" style="width: 100px">纳税人识别号:</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" style="width: 100px">开票地址电话:</label>
            <input class="form-control col-sm-2"
                   v-model="row.f_address_phone" placeholder="开票地址">
          </div>
          <div class="col-sm-12 form-group form-input-group">
            <label class="control-label" style="width: 100px">邮箱推送:</label>
            <input class="form-control col-sm-2"
                   v-model="row.f_email" placeholder="邮箱推送">
          </div>
          <div class="col-sm-12 form-group form-input-group">
            <label class="control-label" style="width: 100px">备注:</label>
            <input class="form-control col-sm-2"
                   v-model="row.f_remarks" placeholder="备注">
          </div>
        </div>
      </div>
    </article>
    <footer slot="modal-footer" class="modal-footer">
      <button type="button" class="btn button_search"
              :disabled="!(row.f_paper_name&&row.f_paper_type&&invoice_is_pax&&row.f_paper_type!== '')"
              @click='confirm'>确认开票
      </button>
    </footer>
  </modal>
  <ticket-print :show="eticket_show" @toggle="eticket_toggle" v-ref:eticketbill></ticket-print>
</template>

<script>
  /**
   *发票冲红管理
   */
  import {HttpResetClass, 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,
        invoice_types: this.$appdata.getParam('发票种类'),
        invoice_is_pax: '征税',
        is_pax: [{label: '征税', value: '征税'}, {label: '不征税', value: '不征税'}],
        selecteds: [],
        eticket_show: false,
        eticket_msg: false,
        invoice_show_gas: '否',
        show_gas: [{label: '是', value: '是'}, {label: '否', value: '否'}],
        row: {},
        disabled: false,
        printstyle: this.$appdata.getParam('打印电子格式') ? this.$appdata.getParam('打印电子格式') : [{label: '电子发票', value: '电子发票'}],
        mid: [{label: '全部', value: ''}, {label: '是', value: ' > 0 '}, {label: '否', value: ' is null'}]
      }
    },
    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'
    },
    methods: {
      async kp (row) {
        let HttpReset = new HttpResetClass()
        let _row = await HttpReset.load('POST', 'rs/sql/sale_getUser', {
          data: {
            orderitem: 'f_userinfo_code',
            condition: `f_userinfo_code = '${row.f_userinfo_code}'`
          }
        }, {resolveMsg: null, rejectMsg: '获取地址失败!'})
        this.row = _row.data[0]
        this.row.id = row.id
        this.row.f_type = row.f_type
        this.invoice_show_gas = this.row.f_collection_type === '按气量' ? '是' : '否'
        // if (!this.row.f_taxpayer_id) {
        //   this.row.f_taxpayer_id = this.row.f_idnumber
        // }
        if (!this.row.f_paper_name) {
          this.row.f_paper_name = this.row.f_user_name
        }
        if (!this.row.f_address_phone) {
          this.row.f_address_phone = this.row.f_address + ' ' + this.row.f_user_phone
        }
        this.eticket_msg = true
      },
      closeEticket () {
        this.eticket_msg = false
        this.disabled = false
      },
      getorg (val) {
        this.f_orgid = this.$login.convertToIn(val)
        this.f_filialeids = val[0]
      },
      cancel () {
        this.show = false
        this.info = {}
      },
      async open_eTicket () {
        if (this.selecteds.length > 1) {
          this.$showAlert('选中条数大于一条，请重新修改！！', 'warning', 2000)
          return
        }
        let HttpReset = new HttpResetClass()
        let row = await HttpReset.load('POST', 'rs/sql/sale_getUser', {
          data: {
            orderitem: 'f_userinfo_code',
            condition: `f_userinfo_code = '${[...new Set(this.selecteds.map(item => item.f_userinfo_code))][0]}'`
          }
        }, {resolveMsg: null, rejectMsg: '获取地址失败!'})
        this.row = row.data[0]
        this.invoice_show_gas = this.row.f_collection_type === '按气量' ? '是' : '否'
        // if (!this.row.f_taxpayer_id) {
        //   this.row.f_taxpayer_id = this.row.f_idnumber
        // }
        if (!this.row.f_paper_name) {
          this.row.f_paper_name = this.row.f_user_name
        }
        if (!this.row.f_address_phone) {
          this.row.f_address_phone = this.row.f_address + ' ' + this.row.f_user_phone
        }
        if (this.selecteds.map(item => item.f_type).toString() === '其他收费') {
          this.disabled = true
        }

        this.eticket_msg = true
      },
      confirm () {
        // this.row.f_print = this.invoice_is_pax
        this.row.invoice_is_pax = this.invoice_is_pax
        this.eticket_msg = false
        this.disabled = false
        this.eticket_show = true
        this.$refs.eticketbill.openETicket(this.row.id, this.row, this.row.f_type, this.row.f_paper_type, false, this.invoice_is_pax)
      },
      eticket_toggle () {
        this.eticket_show = false
        this.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.addOrRemove(true)
      },
      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>
