<template>
  <div class="flex">
    <div class="div-block col-lg-12">
      <label for="fplxdm" class="col-sm-3" style="margin-top: 7px">发票类型:</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 id="fplxdm"></v-select>
      </div>
    </div>
    <div class="div-block col-lg-12">
      <label for="ghdwsbh" class="col-sm-3" style="margin-top: 7px">购货单位识别号:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.ghdwsbh" class="form-control" placeholder="" v-el:ghdwsbh id="ghdwsbh">
      </div>
      <label for="ghdwdzdh" class="col-sm-3" style="margin-top: 7px">购货单位地址电话:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.ghdwdzdh" class="form-control" placeholder="" v-el:ghdwdzdh id="ghdwdzdh">
      </div>
    </div>
    <div class="div-block col-lg-12">
      <label for="ghdwmc" class="col-sm-3" style="margin-top: 7px">购货单位名称:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.ghdwmc" class="form-control" placeholder="" v-el:ghdwmc id="ghdwmc">
      </div>
      <label for="ghdwyhzh" class="col-sm-3" style="margin-top: 7px">购货单位银行账号:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.ghdwyhzh" class="form-control" placeholder="" v-el:ghdwyhzh id="ghdwyhzh">
      </div>
    </div>
    <div class="div-block col-lg-12">
      <label for="spmc" class="col-sm-3" style="margin-top: 7px">开票商品名称:</label>
      <div class="col-sm-3" :class="[$v.invoicetype.required ? 'select-error' : '']">
        <v-select :value.sync="model.spmc" v-model="model.spmc"  :value-single="true"
                  :options='radetname' placeholder='开票商品名称'
                  close-on-select v-ref:invoicetype id="spmc"></v-select>
      </div>
      <label for="dw" class="col-sm-3" style="margin-top: 7px">商品单位:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.dw" class="form-control" placeholder="" v-el:dw id="dw">
      </div>
    </div>
    <div class="div-block col-lg-12">
      <label for="spsl" class="col-sm-3" style="margin-top: 7px">数量:</label>
      <div class="col-sm-3">
        <input type="number"  v-model="model.spsl" class="form-control" placeholder="0" v-el:spsl id="spsl">
      </div>
      <label for="dj" class="col-sm-3" style="margin-top: 7px">单价:</label>
      <div class="col-sm-3">
        <input type="number"  v-model="model.dj" class="form-control" placeholder="0.00" v-el:dj id="dj">
      </div>
    </div>
    <div class="div-block col-lg-12">
      <label for="je" class="col-sm-3" style="margin-top: 7px">金额:</label>
      <div class="col-sm-3">
        <input type="number"  v-model="model.je" class="form-control" placeholder="0.00" v-el:je id="je">
      </div>
      <label for="sl" class="col-sm-3" style="margin-top: 7px">税率:</label>
      <div class="col-sm-3">
        <input type="number" step="0.01"  v-model="model.sl" class="form-control" placeholder="" v-el:sl id="sl">
      </div>
    </div>
    <div class="div-block col-lg-12">
      <label for="kpr" class="col-sm-3" style="margin-top: 7px">开票人:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.kpr" class="form-control" placeholder="" v-el:kpr id="kpr">
      </div>
      <label for="f_operate_date" class="col-sm-3" style="margin-top: 7px">日期:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.f_operate_date" class="form-control" id="f_operate_date" placeholder="" v-el:f_operate_date disabled>
      </div>
    </div>
    <div class="div-block col-lg-12">
      <label for="f_outlets" class="col-sm-3" style="margin-top: 7px">部门:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.f_outlets" class="form-control" id="f_outlets" placeholder="" v-el:f_outlets disabled>
      </div>
      <label for="f_operator" class="col-sm-3" style="margin-top: 7px">操作人:</label>
      <div class="col-sm-3">
        <input type="text"  v-model="model.f_operator" class="form-control" id="f_operator" placeholder="" v-el:f_operator disabled>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'

export default {
  title: '自定义开票',
  name: "CustomInvoice",
  props: ['recordid'],  // 收费记录id
  data () {
    return {
      model:{
        recordid: this.recordid?this.recordid:'', //收费记录id
        fplxdm: '', // 发票类型
        ghdwsbh: '', //购货单位识别号
        ghdwdzdh: '', // 购货单位地址电话
        ghdwmc: '', // 购货单位名称
        ghdwyhzh: '', // 购货单位银行账号
        spmc: '', // 开票商品名称
        dw: '', // 商品单位
        spsl: '', // 数量
        dj: '', // 单价
        je: '', // 金额
        sl: '', // 税率
        kpr: '', // 开票人
        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:'',  // 操作人
        type: '发票开具',
        kpzdbs: '002',
        fpqqlsh: this.dateFormat("yyyyMMddhhmmssSSSS"), // 发票请求流水号  前台自己生成
        kplx: '0',
        tspz: '00',
        qdbz: '0',
        zsfs: '0',
        kce: '0',
        bz: '',
        skr: '', //受款人
        fhr: '', //复核人
        fphxz: '0',
        spsm: '',
        ggxh: '',
        hsbz: '',
        spbm: '',
        zxbm: '',
        yhzcbs: '',
        lslbs: '',
        zzstsgl: '',
      },
    }
  },
  ready () {
    console.log("登录信息: ", Vue.$login.f)
  },
  watch: {
    'recordid' (val) {
      if(val) {
        this.model.recordid = val
      }
    }
  },
  methods: {
    initModel() {
      this.model = {
        recordid: this.recordid?this.recordid:'', //收费记录id
        fplxdm: '', // 发票类型
        ghdwsbh: '', //购货单位识别号
        ghdwdzdh: '', // 购货单位地址电话
        ghdwmc: '', // 购货单位名称
        ghdwyhzh: '', // 购货单位银行账号
        spmc: '', // 开票商品名称
        dw: '', // 商品单位
        spsl: '', // 数量
        dj: '', // 单价
        je: '', // 金额
        sl: '', // 税率
        kpr: '', // 开票人
        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:'',  // 操作人
        type: '发票开具',
        kpzdbs: '002',
        fpqqlsh: '', // 发票请求流水号  前台自己生成
        kplx: '0',
        tspz: '00',
        qdbz: '0',
        zsfs: '0',
        kce: '0',
        bz: '',
        skr: '', //受款人
        fhr: '', //复核人
        fphxz: '0',
        spsm: '',
        ggxh: '',
        hsbz: '',
        spbm: '',
        zxbm: '',
        yhzcbs: '',
        lslbs: '',
        zzstsgl: '',
      }
    },
    async confirm() {
      console.log("开票: ", this.model)
      if(this.model.fplxdm == "" || this.model.fplxdm == null) {
        this.$showAlert("请选择发票类型","info", 1000)
        return
      }
      if(this.model.ghdwsbh==""||this.model.ghdwsbh==null) {
        this.$showAlert("请输入购货单位识别号","info", 1000)
        return
      }
      if(this.model.ghdwdzdh==""||this.model.ghdwdzdh==null) {
        this.$showAlert("请输入购货单位地址电话","info", 1000)
        return
      }
      if(this.model.ghdwmc==""||this.model.ghdwmc==null) {
        this.$showAlert("请输入购货单位名称","info", 1000)
        return
      }
      if(this.model.ghdwyhzh==""||this.model.ghdwyhzh==null) {
        this.$showAlert("请输入购货单位银行账号","info", 1000)
        return
      }
      if(this.model.spmc==""||this.model.spmc==null) {
        this.$showAlert("请选择开票商品名称","info", 1000)
        return
      }
      if(this.model.dw==""||this.model.dw==null) {
        this.$showAlert("请输入商品单位","info", 1000)
        return
      }
      if(this.model.spsl==""||this.model.spsl==null) {
        this.$showAlert("请输入数量","info", 1000)
        return
      }
      if(this.model.dj==""||this.model.dj==null) {
        this.$showAlert("请输入单价","info", 1000)
        return
      }
      if(this.model.je==""||this.model.je==null) {
        this.$showAlert("请输入金额","info", 1000)
        return
      }
      if(this.model.sl==""||this.model.sl==null) {
        this.$showAlert("请输入税率","info", 1000)
        return
      }
      if(this.model.kpr==""||this.model.kpr==null) {
        this.$showAlert("请输入开票人","info", 1000)
        return
      }
      try {
        await this.$resetpost('http://127.0.0.1:8003/Goldtax', this.model, {resolveMsg: null, rejectMsg: '自定义开票失败！！！'}, 0)
        await this.$resetpost('rs/entity/t_custominvoice', self.model, {resolveMsg: null, rejectMsg: '保存开票信息失败'})
        this.initModel()
        this.$emit('invoice-success')
      } catch (e) {
        this.$emit('invoice-danger')
      }
    },
    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() {
      let res = this.$appdata.getParam('票据类型')
      console.log("票据类型: ", res)
      return res
    },
    radetname() {
      let res = this.$appdata.getParam('商品名称')
      console.log("商品名称: ", res)
      return res
    }
  },
}
</script>

<style scoped>
  .div-block{
    display: inline-block;
    width: 100%;
    max-height: 45px;
  }
</style>
