<template>
  <div class="auto">
    <div class="form-group col-lg-12">
      <label for="fplxdm" class="col-sm-2 control-label">发票类型:</label>
      <div class="col-sm-3" :class="[$v.invoicetype.required ? 'select-error' : '']">
        <v-select :value.sync="model.fplxdm" v-model="model.fplxdm"  :value-single="true"
                  :options='invoicetype' placeholder='发票类型'
                  close-on-select v-ref:invoicetype></v-select>
      </div>
    </div>
    <div class="form-group col-lg-12">
      <label for="ghdwmc" class="col-sm-2 control-label">购货单位名称:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.ghdwmc" class="form-control" placeholder="" v-el:ghdwmc>
      </div>
      <label for="ghdwsbh" class="col-sm-2 control-label">购货单位识别号:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.ghdwsbh" class="form-control" placeholder="" v-el:ghdwsbh>
      </div>

    </div>
    <div class="form-group col-lg-12">
      <label for="ghdwyhzh" class="col-sm-2 control-label">购货单位银行账号:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.ghdwyhzh" class="form-control" placeholder="" v-el:ghdwyhzh>
      </div>
      <label for="ghdwdzdh" class="col-sm-2 control-label">购货单位地址电话:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.ghdwdzdh" class="form-control" placeholder="" v-el:ghdwdzdh>
      </div>
    </div>
    <div class="form-group col-lg-12">
      <label for="spmc" class="col-sm-2 control-label">开票商品名称:</label>
      <div class="col-sm-3" :class="[$v.invoicetype.required ? 'select-error' : '']">
        <v-select :value.sync="model.detail[0].spmc" v-model="model.detail[0].spmc"  :value-single="true"
                  :options='radetname' placeholder='开票商品名称'
                  close-on-select v-ref:invoicetype @change="spmcChange"></v-select>
      </div>
      <label for="dw" class="col-sm-2 control-label">商品单位:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.detail[0].dw" class="form-control" placeholder="" v-el:dw disabled>
      </div>
    </div>
    <div class="form-group col-lg-12">
      <label for="ghdwmc" class="col-sm-2 control-label">数量:</label>
      <div class="col-sm-3">
        <input type="number"  v-model="model.detail[0].spsl" class="form-control" placeholder="0" v-el:spsl>
      </div>
      <label for="ghdwyhzh" class="col-sm-2 control-label">单价:</label>
      <div class="col-sm-3">
        <input type="number"  v-model="model.detail[0].dj" class="form-control" placeholder="0.00" v-el:dj>
      </div>
    </div>
    <div class="form-group col-lg-12">
      <label for="ghdwmc" class="col-sm-2 control-label">金额:</label>
      <div class="col-sm-3">
        <input type="number"  v-model="model.detail[0].je" class="form-control" placeholder="0.00" v-el:je>
      </div>
      <label for="ghdwyhzh" class="col-sm-2 control-label">税率:</label>
      <div class="col-sm-3">
        <input type="number" step="0.01"  v-model="model.detail[0].sl" class="form-control" placeholder="" v-el:sl disabled>
      </div>
    </div>
    <div class="form-group col-lg-12">
      <label for="ghdwmc" class="col-sm-2 control-label">开票人:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.kpr" class="form-control" placeholder="" v-el:kpr>
      </div>
      <label for="ghdwyhzh" class="col-sm-2 control-label">日期:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.f_operate_date" class="form-control" placeholder="" v-el:f_operate_date disabled>
      </div>
    </div>
    <div class="form-group col-lg-12">
      <label for="ghdwmc" class="col-sm-2 control-label">部门:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.f_outlets" class="form-control" placeholder="" v-el:f_outlets disabled>
      </div>
      <label for="ghdwyhzh" class="col-sm-2 control-label">操作人:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.f_operator" class="form-control" placeholder="" v-el:f_operator disabled>
      </div>
    </div>
    <div class="col-lg-12" style="position: relative">
      <button class="btn" style="position: absolute;right: 20%;" @click="confirm">开票</button>
    </div>
  </div>
</template>

<script>
  import Vue from 'vue'
  import co from 'co'
  let etickedGen = async function (self) {
    try {
      self.model.fplxdm = self.model.fplxdm === '专票' ? '004' : self.model.fplxdm
      console.log('参数001: ', self.model)
      await Vue.resetpost('http://127.0.0.1:8003/Goldtax', self.model, {resolveMsg: null, rejectMsg: '自定义开票失败！！！'}, 0)
      // yield Vue.resetpost('rs/entity/t_custominvoice', self.model, {resolveMsg: '开票成功', rejectMsg: '保存开票信息失败'})
      // self.initModel()
    } catch (e) {
      let err = {status: 623, msg: '自定义开票失败！！！'}
      throw err
    }
  }

  let getEticketInfo = async function (self) {
    let getEticketInfo = await self.$resetpost('rs/logic/getEticketInfo', {}, {resolveMsg: null, rejectMsg: '获取电子票参数出错！！！'})
    self.eticketParam = getEticketInfo.data
    self.model.fhr = getEticketInfo.data.checker
  }

  export default {
    title: '自定义开票',
    name: 'CustomInvoice',
    data () {
      return {
        model: {
          // 发票类型
          type: '发票开具',
          kpzdbs: Vue.$login.f.taxnum,
          fpqqlsh: this.dateFormat('yyyyMMddhhmmssSSSS'), // 发票请求流水号  前台自己生成
          kplx: '0',
          tspz: '00',
          qdbz: '0',
          zsfs: '0',
          fplxdm: '', // 发票类型
          ghdwsbh: '', // 购货单位识别号
          ghdwdzdh: '', // 购货单位地址电话
          ghdwmc: '', // 购货单位名称
          ghdwyhzh: '', // 购货单位银行账号
          detail: [{
            fphxz: '0',
            spmc: '', // 开票商品名称
            spsm: '',
            ggxh: '',
            dw: '', // 商品单位
            spsl: '', // 数量
            dj: '', // 单价
            je: '', // 金额
            sl: '', // 税率
            hsbz: '',
            spbm: '',
            zxbm: '',
            yhzcbs: '',
            lslbs: '',
            zzstsgl: ''
          }],
          hjse: '',
          jshj: '',
          kce: '',
          bz: '',
          skr: Vue.$login.f.name, // 受款人
          fhr: '', // 复核人
          kpr: Vue.$login.f.name, // 开票人
          tzdbh: '',
          yfpdm: '',
          yfphm: '',
          f_operate_date: this.$login.toStandardDateString(), // 日期
          f_outlets: Vue.$login.f.f_parentname ? Vue.$login.f.f_parentname : '', // 部门
          f_operator: Vue.$login.f.f_parentname ? Vue.$login.f.name : ''  // 操作人
        },
        eticketParam: null
      }
    },
    ready () {
      getEticketInfo(this)
    },
    methods: {
      initModel () {
        this.model = {
          type: '发票开具',
          kpzdbs: Vue.$login.f.taxnum,
          fpqqlsh: this.dateFormat('yyyyMMddhhmmssSSSS'), // 发票请求流水号  前台自己生成
          kplx: '0',
          tspz: '00',
          qdbz: '0',
          zsfs: '0',
          fplxdm: '', // 发票类型
          ghdwsbh: '', // 购货单位识别号
          ghdwdzdh: '', // 购货单位地址电话
          ghdwmc: '', // 购货单位名称
          ghdwyhzh: '', // 购货单位银行账号
          detail: [{
            fphxz: '0',
            spmc: '', // 开票商品名称
            spsm: '',
            ggxh: '',
            dw: '', // 商品单位
            spsl: '', // 数量
            dj: '', // 单价
            je: '', // 金额
            sl: '', // 税率
            hsbz: '',
            spbm: '',
            zxbm: '',
            yhzcbs: '',
            lslbs: '',
            zzstsgl: ''
          }],
          hjse: '',
          jshj: '',
          kce: '',
          bz: '',
          skr: Vue.$login.f.name, // 受款人
          fhr: '', // 复核人
          kpr: Vue.$login.f.name, // 开票人
          tzdbh: '',
          yfpdm: '',
          yfphm: '',
          f_operate_date: this.$login.toStandardDateString(), // 日期
          f_outlets: Vue.$login.f.f_parentname ? Vue.$login.f.f_parentname : '', // 部门
          f_operator: Vue.$login.f.f_parentname ? Vue.$login.f.name : ''  // 操作人
        },
          this.eticketParam = null
      },
      spmcChange () {
        // 商品名称变更，获取对应的单价金额税率
        if (this.model.detail[0].spmc) {
          if (this.model.detail[0].spmc === '天然气整改工程') {
            this.model.detail[0].dw = this.eticketParam.configEticket.trqzggc.dw,
              this.model.detail[0].sl = this.eticketParam.configEticket.trqzggc.sl,
              this.model.detail[0].spbm = this.eticketParam.configEticket.trqzggc.spbm
          } else if (this.model.detail[0].spmc === '安全用气须知牌') {
            this.model.detail[0].dw = this.eticketParam.configEticket.aqyqxzp.dw,
              this.model.detail[0].sl = this.eticketParam.configEticket.aqyqxzp.sl,
              this.model.detail[0].spbm = this.eticketParam.configEticket.aqyqxzp.spbm
          } else if (this.model.detail[0].spmc === '自闭阀') {
            this.model.detail[0].dw = this.eticketParam.configEticket.zbf.dw,
              this.model.detail[0].sl = this.eticketParam.configEticket.zbf.sl,
              this.model.detail[0].spbm = this.eticketParam.configEticket.zbf.spbm
          } else if (this.model.detail[0].spmc === '购气薄') {
            this.model.detail[0].dw = this.eticketParam.configEticket.gqb.dw,
              this.model.detail[0].sl = this.eticketParam.configEticket.gqb.sl,
              this.model.detail[0].spbm = this.eticketParam.configEticket.gqb.spbm
          } else if (this.model.detail[0].spmc === '燃气表') {
            this.model.detail[0].dw = this.eticketParam.configEticket.rqb.dw,
              this.model.detail[0].sl = this.eticketParam.configEticket.rqb.sl,
              this.model.detail[0].spbm = this.eticketParam.configEticket.rqb.spbm
          } else if (this.model.detail[0].spmc === '民用天然气') {
            this.model.detail[0].dw = this.eticketParam.configEticket.mytrq.dw,
              this.model.detail[0].sl = this.eticketParam.configEticket.mytrq.sl,
              this.model.detail[0].spbm = this.eticketParam.configEticket.mytrq.spbm
          } else if (this.model.detail[0].spmc === 'IC卡') {
            this.model.detail[0].dw = this.eticketParam.configEticket.IC.dw,
              this.model.detail[0].sl = this.eticketParam.configEticket.IC.sl,
              this.model.detail[0].spbm = this.eticketParam.configEticket.IC.spbm
          }
        }
      },
      confirm () {
        console.log('开票: ', this.model)
        if (!this.model.fplxdm) {
          alert('请选择发票类型')
          return
        }
        if (!this.model.ghdwmc) {
          alert('请输入购货单位名称')
          return
        }
        if (this.model.fplxdm === '专票') {
          if (!this.model.ghdwsbh) {
            alert('请输入购货单位识别号')
            return
          }
          if (!this.model.ghdwdzdh) {
            alert('请输入购货单位地址电话')
            return
          }
          if (!this.model.ghdwyhzh) {
            alert('请输入购货单位银行账号')
            return
          }
        }
        if (!this.model.detail[0].spmc) {
          alert('请选择开票商品名称')
          return
        }
        if (!this.model.detail[0].spsl) {
          alert('请输入数量')
          return
        }
        if (!this.model.detail[0].dj) {
          alert('请输入单价')
          return
        }
        if (!this.model.detail[0].je) {
          alert('请输入金额')
          return
        }

        if (!this.model.kpr) {
          alert('请输入开票人')
          return
        }
        etickedGen(this)
        // this.initModel()
      },
      dateFormat (fmt) {
        let date = new Date()
        var o = {
          'M+': date.getMonth() + 1, // 月份
          'd+': date.getDate(), // 日
          'h+': date.getHours(), // 小时
          'm+': date.getMinutes(), // 分
          's+': date.getSeconds(), // 秒
          'q+': Math.floor((date.getMonth() + 3) / 3) // 季度
        }
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
        for (var k in o) { if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))) }
        if (/(S+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getMilliseconds() + '' + Math.floor(Math.random() * 10)).substr(4 - RegExp.$1.length))
        return fmt
      }
    },
    computed: {
      invoicetype () {
        return this.$appdata.getParam('票据类型')
      },
      radetname () {
        return this.$appdata.getParam('商品名称')
      }
    }
  }
</script>

<style scoped>

</style>
