<template>
    <div class="flex-row">
        <div class="span" style="overflow-y: auto">
            <validator name='v'>
                <form novalidate class="form-horizontal">
                    <!--          <div class="row" style="margin-top:10px;">-->
                    <!--            -->
                    <!--          </div>-->
                    <div style="margin-top:10px;" v-for="row in model.otherdetail">
                        <div class="row">
                            <div class="col-sm-4" :class="{'has-success':row.f_brand_spec,'select-error':!row.f_brand_spec}">
                                <label   for="f_brand_spec" class="font_normal_body " title="">设备别名</label>
                                <v-select   id="f_brand_spec"
                                            placeholder='设备别名'
                                            v-model="row.f_brand_spec"
                                            :value.sync="row.f_brand_spec"
                                            :options='brandspec'
                                            close-on-select clear-button>
                                </v-select>
                            </div>

                            <div class="col-sm-4" :class="{'has-success':row.f_expire_date,'select-error':!row.f_expire_date}">
                                <label   for="f_expire_date" class="font_normal_body " title="">&nbsp;到期时间</label>
                                <datepicker style="width:60%" placeholder="缴费到期时间"
                                            v-model="row.f_expire_date"
                                            :value.sync="row.f_expire_date"
                                            :format="'yyyy-MM-dd'"
                                ></datepicker>
                            </div>
                            <div class="col-sm-3" :class="{'has-success':row.f_unitprice,'has-error':!row.f_unitprice}">
                                <label style="" for="f_unitprice" class="font_normal_body">*金&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;额</label>
                                <input class="input_search"  style="width:40%" type="number" min='1' @blur.prevent="getcollection()"
                                       v-validate:f_unitprice='{required: true, dctest: [0, ">" ] }'
                                       v-model="row.f_unitprice"
                                       placeholder="金额" v-next-el='sk' v-el:sl>
                            </div>
                            <div style="padding-top:10px">
                                <button type="button" class="glyphicon glyphicon-minus btn-danger" @click.stop="delthisdetail($index)"></button>&nbsp;&nbsp;&nbsp;
                                <button type="button" class="glyphicon glyphicon-plus btn-success" @click.stop="addadetail()"></button>
                            </div>
                        </div>
                        <!--            <div class="row" style="margin-top:10px;" v-if="row.f_brand_spec[0]=='其他费用'">-->
                        <!--              <div class="col-sm-4" :class="[$v.f_fee_type.required ? 'has-error' : 'has-success']">-->
                        <!--                <input v-validate:f_fee_type='{required: true}' v-model="row.f_fee_type" v-show="false"/>-->
                        <!--                <label for="f_fee_type" class="font_normal_body ">其他收费类型</label>-->
                        <!--                <v-select id="f_fee_type"-->
                        <!--                          v-model="row.f_fee_type"-->
                        <!--                          placeholder='请选择'-->
                        <!--                          :value.sync="row.f_fee_type"-->
                        <!--                          :options='feetype'-->
                        <!--                          close-on-select clear-button>-->
                        <!--                </v-select>-->
                        <!--              </div>-->

                        <!--              <div class="col-sm-4">-->
                        <!--                <label for="f_fee_time" class="font_normal_body">本次购买时间</label>-->
                        <!--                <v-select id="f_fee_time"-->
                        <!--                          v-model="row.f_fee_time"-->
                        <!--                          placeholder='请选择'-->
                        <!--                          :value.sync="row.f_fee_time"-->
                        <!--                          :options='feetime'-->
                        <!--                          close-on-select clear-button>-->
                        <!--                </v-select>-->
                        <!--              </div>-->
                        <!--            </div>-->

                    </div>


                    <div class="row" style="margin-top:10px;">
                        <div class="col-sm-4">
                            <label for="f_paytype" class="font_normal_body" title="参数名称：付款方式">收款方式</label>
                            <v-select id="f_payment"
                                      v-model="f_payment"
                                      placeholder='请选择'
                                      :value.sync="model.f_payment"
                                      :options='paytype'
                                      close-on-select clear-button>
                            </v-select>
                        </div>
                        <div class="col-sm-4">
                            <label for="f_print" class="font_normal_body">&nbsp;打印格式</label>
                            <v-select id="print"
                                      v-model="f_print"
                                      placeholder='请选择'
                                      :value.sync="model.f_print"
                                      :options='printstyle'
                                      close-on-select clear-button>
                            </v-select>
                        </div>
                        <div class="col-sm-4">
                            <label for="f_collection" class="font_normal_body">&nbsp;收&emsp;&emsp;款</label>
                            <label for="f_collection" class="font_normal_body">{{model.f_collection}}</label>
                        </div>
                    </div>
                    <div class="row" style="margin-top:10px;" v-if="model.f_payment[0].includes('POS')">
                        <div class="col-sm-4">
                            <label for="f_voucher_number" class="font_normal_body">&nbsp;&nbsp;&nbsp;凭证号</label>
                            <input type="text" class="input_search" style="width:60%" v-model="model.f_voucher_number" placeholder="凭证号">
                        </div>
                    </div>
                    <div class="row" style="margin-top:10px;">
                        <div class="col-sm-4">
                            <label for="f_voucher_number" class="font_normal_body">服务人员</label>
                            <v-select v-model="model.f_service_person"
                                      placeholder='请选择'
                                      :value.sync="model.f_service_person"
                                      :options='services'
                                      close-on-select clear-button>
                            </v-select>
                        </div>

                        <div class="col-sm-8" >
                            <label for="f_comments" class=" font_normal_body">&nbsp;备&emsp;&emsp;注</label>
                            <input type="text" class="input_search" style="width:80%" v-model="model.f_comments" rows="1" placeholder="备注">
                        </div>
                    </div>
                </form>
                <validate-bill v-if="hasValidateBill" :data="model" @validate-bill="validateBill"></validate-bill>
                <print-bill :show="print" :data='row' :bill-config='config' :bill-data='billData' v-on:toggle="close" @printok="printok" v-ref:printbill></print-bill>
                <!-- <print-other-charge-bill :show="print" v-ref:printbill :row='row' :logic='model' v-on:success="close" v-on:toggle="close"></print-other-charge-bill> -->
                <!-- <tax-other-bill :show="taxprint"  v-ref:taxprintbill :row='row' :logic='model' v-on:success="close" v-on:toggle="close"></tax-other-bill> -->
                <div style="text-align:right;height: 25%;">
                    <button class="button_search btn-gn" @click="confirm()" :disabled='!$v.valid || validateOk'>确认</button>
                    <button class="button_clear btn-gn" @click="clean()">取消</button>
                </div>
            </validator>

        </div>
    </div>
</template>
<script>
  let otherChargeGen = async function (self) {
    try {
      console.log('11111', self.model)
      let resid = await self.$LogicService.repairCharge(self.model, self.row)
      console.log('维修收费返回id', resid)
      if (self.config.dispatch) {
        await self.$showMessage(`是否添加派工单`, ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            console.log('其他收费xinxi', self.model)
            console.log('其他收费xinxi', self.row)
            let batch = {
              sf_id: resid.data,
              f_userinfo_code: self.row.f_userinfo_code,
              f_dispathch_name: self.row.f_user_name,
              f_phone: self.row.f_user_phone,
              f_dispathch_address: self.row.f_address,
              f_meternumber: self.row.f_meternumber,
              f_dispathch_type: '安装',
              f_source: '其他收费',
              f_money: self.model.f_collection,
              f_remarks: self.model.f_comments,
              f_operator_date: self.$login.toStandardTimeString(),
              f_orgid: self.$login.f.orgid,
              f_dispathch_state: '待分配'
            }
            console.log('111:', batch)
            self.$resetpost('rs/logic/savePatch', batch).then((res1) => {
              // this.params.rows.splice(index, 1)
              console.log('更新成功！', res1.data.id)
            })
          }
        })
      }
      if (self.config.hasPrint) {
        if (self.model.f_print[0] != '电子发票') {
          if (self.config.hasBillManage) {
            self.row.id = resid.data
            self.row.f_bill_type = '其他收费'
            self.row.f_bill_style = self.model.f_print[0]
            self.print = true
          } else {
            self.row.id = resid.data
            self.print = true
          }
        } else if (self.model.f_print[0] === '国税发票') {
          // TODO
          self.$dispatch('success')
        } else if (self.model.f_print[0] === '电子发票') {
          // TODO
          self.$dispatch('success')
        }
      } else {
        self.$dispatch('success')
      }
    } catch (error) {
      self.$showAlert(error, 'danger', 0)
      self.$dispatch('refresh')
    }
  }

  let asyncOtherCharge = async function (self) {
    await self.$getConfig(self, 'OtherCharge')
    self.model.f_print = self.config.printType instanceof Array ? self.config.printType : [self.config.printType]
    self.model.f_payment = [self.config.payment]
    self.model.f_brand_spec = [self.config.brandspec]

    self.model.f_use_type = self.config.billType
    self.model.f_bill_type = self.model.f_print
    self.hasValidateBill = self.config.hasBillManage
    self.setparam()
  }
  export default {
    title: '其他收费',
    data () {
      return {
        config: {
          dispatch: false,
          hasPrint: true,  // 默认打票
          hasBillManage: false,  // 默认不启用发票管理
          billType: '其他费用',   // 票据类型（燃气费，其他费用，调用的发票代码不同）
          printType: '普通收据', // 收据/电子票/专用发票/国税发票
          payment: '现金缴费',  // 付款方式
          brandspec: '维修费' // 收费类型
        },
        model: {
          f_print: [],
          f_payment: '现金缴费',
          f_number: 0,
          f_unitprice: 0,
          f_collection: 0,
          f_comments: '',
          f_voucher_number: '',
          f_service_person: '',
          // 用户其他费用信息
          t_userfees: '',
          f_fee_type: [],
          f_fee_time: ['12'],
          otherdetail: [{f_brand_spec: ['维修费']}]
        },
        print: false,
        taxprint: false,
        billData: {
          url: 'rs/report/otherCharge_bill',
          bill: ''
        },

        // 启用发票管理需要对票号进行验证
        hasValidateBill: false,
        validateOk: false,
        f_typenumber: '',
        typeNameList: [],
        typenumberList: [],
        brandspec: [],
        printstyle: this.$appdata.getParam('打印格式'),
        paytype: this.$appdata.getParam('付款方式'),
        feetype: this.$appdata.getParam('其他费用'),
        services: this.$appdata.getParam('服务人员')
      }
    },
    props: ['row'],
    ready () {
      // this.model.f_unitprice = this.$appdata.getSingleValue('置换气费')
      asyncOtherCharge(this)
    },
    watch: {
      'model.f_fee_type' (val) {
        if (val[0]) {
          this.userfees(this.row.f_userinfo_id, val[0])
        }
      }
    },
    methods: {
      async setparam () {
        console.log('开始获取设备')
        let data = await this.$resetpost('rs/sql/getrepair', {
          data: {
            condition: `1=1 and f_userinfo_id = '${this.row.f_userinfo_id}'`
          }
        }, {resolveMsg: null, rejectMsg: '获取设备失败!'})
        console.log('设备', data)
        let house = []
        house.push()
        for (let row of data.data) {
          console.log('开始保存设备')
          house.push({label: row.f_userinfodevices_name, value: row.id})
        }
        this.brandspec = house
      },
      confirm () {
        otherChargeGen(this)
      },
      clean () {
        this.$dispatch('refresh', this.row)
      },
      close () {
        this.taxprint = false
        this.print = false
        this.clean()
      },
      printok () {
        this.$dispatch('success')
      },
      validateBill (val) {
        this.validateOk = !val.isOk
        this.billData.bill = val.bill
      },
      getcollection () {
        let money = 0
        this.model.otherdetail.forEach((item) => {
          if (item.f_unitprice) {
            money += (item.f_unitprice - 0)
          }
        })

        if (money != 0) {
          this.model.f_collection = money.toFixed(4)
        }
      },
      addadetail () {
        this.model.otherdetail.push({f_brand_spec: ['材料费']})
      },
      delthisdetail (index) {
        if (this.model.otherdetail.length > 1) {
          this.model.otherdetail.splice(index, 1)
          this.getcollection()
        }
      },
      async userfees (userinfoid, feetype) {
        this.model.t_userfees = await this.$resetpost('rs/sql/saleSingleTable', {
          data: {
            tablename: 't_userfees',
            condition: `f_userinfo_id=${userinfoid} and f_fee_type='${feetype}'`
          }
        }, {resolveMsg: null, rejectMsg: '查询客户其他费用失败!'})
      }
    },
    computed: {
      feetime () {
        return [{label: '一个月', value: '1'}, {label: '三个月', value: '3'}, {label: '六个月', value: '6'}, {
          label: '一年',
          value: '12'
        }]
      },
      gettypename () {
        return this.typeNameList
      },
      gettypenumber () {
        return this.typenumberList
      }
    }
  }
</script>

<style>
</style>
