<template>
  <div class="auto">
    <validator name='a' @valid="billValid(true)" @invalid="billValid(false)">
      <form class="form-horizontal">
        <div class="row">
          <div class="col-sm-4">
            <label class="font_normal_body">客户姓名&nbsp;</label>
            <input type="text" v-model="model.f_user_name" disabled=disabled style="width: 60%" class="input_search">
          </div>
          <div class="col-sm-4">
            <label class="font_normal_body">&nbsp;客户编号</label>
            <input type="text" v-model="model.f_userinfo_code" disabled=disabled style="width: 60%" class="input_search">
          </div>
          <div class="col-sm-4">
            <label class="font_normal_body">&nbsp;卡号&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <input type="text" v-model="model.f_card_id" disabled=disabled style="width: 60%" class="input_search">
          </div>
        </div>
        <div class="row">
          <div class="col-sm-4">
            <label class="font_normal_body">收据号码</label>
            <input type="text" v-model="model.f_bill_number" disabled=disabled style="width: 60%" class="input_search">
          </div>
          <div class="col-sm-8">
            <label class="font_normal_body">操作日期</label>
            <datepicker id="f_operate_date"
                        placeholder="操作日期"
                        style="width:80%"
                        v-model="model.f_operate_date"
                        :value.sync="model.f_operate_date"
                        :format="'yyyy-MM-dd HH:mm:ss'"
            >
            </datepicker>
          </div>
        </div>
        <div class="row">
          <div class="col-sm-4">
            <label class="font_normal_body">业务类型</label>
            <input type="text" v-model="model.f_type" style="width: 60%" class="input_search">
          </div>
          <div class="col-sm-4">
            <label class="font_normal_body">实收金额</label>
            <input type="number" v-model="model.f_paid_money"  v-scale="[model.f_paid_money,2]" style="width: 60%" class="input_search">
          </div>
          <div class="col-sm-4">
            <label class="font_normal_body">实收气量</label>
            <input type="number" v-model="model.f_gas"  v-scale="[model.f_gas,2]" style="width: 60%" class="input_search">
          </div>
        </div>
        <div class="row">
          <div class="col-sm-4">
            <label class="font_normal_body">收据类型</label>
            <v-select :value.sync="model.f_bill_type" v-model="model.f_bill_type"
                      :options='billtype' placeholder='收据类型'
                      :value-single="true"
                      :search="false"
                      close-on-select></v-select>
          </div>
          <div class="col-sm-4">
            <label class="font_normal_body">收据状态</label>
            <v-select :value.sync="model.f_bill_state" v-model="model.f_bill_state"
                      :options='billstate' placeholder='收据状态'
                      :value-single="true"
                      :search="false"
                      close-on-select></v-select>
          </div>
          <div class="col-sm-4 form-group">
            <label class="font_normal_body" title="参数:操作人员">操作人员</label>
            <v-select :value.sync="model.f_operatorid"
                      v-model="model.f_operatorid"
                      :value-single="true"  @change="selectoperator()"
                      :options='foperator'
                      close-on-select></v-select>
          </div>
        </div>

        <div class="row">
          <div class="col-sm-4">
            <label class="font_normal_body">所属部门</label>
            <input type="text" v-model="model.f_depname" style="width: 60%" class="input_search">
          </div>
        </div>
      </form>
      <div style="float: right">
        <button class="button_search" @click="save">保存</button>
        <button class="button_clear" @click="cancel">取消</button>
      </div>
    </validator>
    <modal :show.sync="showfiles" width="80%" style="width:auto;"  v-ref:modal middle backdrop="false">
      <article slot="modal-body">
        <upload :blodid="model.f_userinfo_id"  :businessid="model.id" v-ref:upload  fusetype="发票" isremark="true" style="width:auto" ></upload>
      </article>
      <footer slot="modal-footer" class="modal-footer">
      </footer>
    </modal>
    <!--文件上传按钮-->
    <button class="button_search" style="width: max-content" @click="uploadFiles()">上传附件</button>
  </div>
</template>
<script>
  import Vue from 'vue'
  import {HttpResetClass} from 'vue-client'
  import * as Util from "../../../../../Util";
  var data
  let readyGen = async function (self) {
    await  self.$LoadParams.loadParam(self.f_filialeid)
    await    self.$GetSaleParam.initinputtor()
    await self.initoperator()
  }
  export default {
    title: '基本信息',

    data () {
      return {
        // 构建数组来存储对象
        editbank: true,
        showfiles: false,
        priceSplit: false,
        recordList: [],
        foperator:[],
        // fstate:[{label:'有效',value:'有效'},{label:'无效',value:'无效'},{label:'冲正',value:'冲正'},{label:'撤销',value:'撤销'}],
        // ftype:[{label:'其他补气',value:'其他补气'},{label:'换表补气',value:'换表补气'},{label:'机表补费',value:'机表补费'},{label:'补费',value:'补费'},{label:'清零补气',value:'清零补气'},{label:'扣费',value:'扣费'},{label:'扣气',value:'扣气'},{label:'物联网补气',value:'物联网补气'},{label:'物联网扣气',value:'物联网扣气'},{label:'机表扣费',value:'机表扣费'}],
        billtype: [{label: '正常', value: '正常'}, {label: '冲正', value: '冲正'}, {label: '补打', value: '补打'}],
        billstate: [{label: '有效', value: '有效'}, {label: '作废', value: '作废'}, {label: '撤销', value: '撤销'}],
        // meterstate:[{label:'已抄',value:'已抄'},{label:'未抄',value:'未抄'}],
        // fstate:[{label:'有效',value:'有效'},{label:'冲正',value:'冲正'},{label:'作废',value:'作废'},{label:'待审核',value:'待审核'}],
        fileNameSet: {
          'f_bill_number': '收据号码',
          'f_operate_date': '操作日期',
          'f_paid_money': '实收金额',
          'f_type': '业务类型',
          'f_bill_type': '收据类型',
          'f_bill_state': '收据状态',
          'f_operator': '操作人员',
          'f_depname': '所属部门'
        }
      }
    },
    props: {
      oldmodel: Object,
      model: Object
    },

    ready () {
      readyGen(this)
      this.oldmodel = JSON.parse(JSON.stringify(this.model))
    },
    created () {
    },
    methods: {
      initoperator(){
        this.foperator=[]
        console.log("this.$login",this.$login,this.oldmodel,this.model)
        let arr = this.$GetSaleParam.inputtors.filter((res) => {
          if (res.rolestr != null && res.rolestr != '') {
            return res.rolestr.indexOf('营业员') > -1 && res.f_department_name == this.model.f_depname
          }
        })
        arr.forEach((res) => {
          this.foperator.push({label: res.name, value: res.id})
        })
      },
      selectoperator () {
        let id = this.model.f_operatorid
        if(!this.model.f_operatorid){
          return
        }
        let http = new HttpResetClass()
        http.load('POST', 'rs/sql/saleSingleTable',
          {data:{tablename: 't_user',
              condition: `id = '${id}'`}},
          {resolveMsg: null, rejectMsg: null}).then((res) => {
          console.log('kanak,res', res)
          if(res.data.length>0){
            this.model.f_operator = res.data[0].name
          }else{
            this.model.f_operatorid=''
            this.$showAlert('操作员信息未找到','warning',2000)
          }
          console.log('this.f_operatorid', this.model.f_operator)
        })
      },
      // 自定义划价
      fileterprice (row) {
        this.priceSplit = true
        console.log('row==>', row)
      },
      cancel () {
        this.$dispatch('close')
      },
      uploadFiles () {
        this.showfiles = !this.showfiles
      },

      billValid (val) {
      },
      async save () {
        for (var item in this.model) {
          if (this.model[item] instanceof Array) {
            this.model[item] = this.model[item].length > 0 ? this.model[item][0] : ''
          }
          if (this.model[item] != this.oldmodel[item]) {
            var car = {
              chinesename: this.fileNameSet[item],
              fieldname: item,
              tablename: 't_billdetail',
              oldvalue: this.oldmodel[item],
              newvalue: this.model[item],
              state: '有效',
              modifitime: Util.toStandardTimeString(),
              operator: this.$login.f.name,
              bill_id: this.model.id,
              f_operator : this.$login.f.name,
              f_operatorid : this.$login.f.id,
              f_orgid : this.$login.f.orgid,
              f_orgname : this.$login.f.orgs,
              f_depid : this.$login.f.depids,
              f_depname : this.$login.f.deps
            }
            this.recordList.push(car)
            // 该方法并未对chinesename fieldname operator等进行判断赋值，此暂写为定值
            console.log(car)
          }
        }

        await this.$resetpost('rs/logic/saveBillRecord', {data: {recordlist: this.recordList, newData: this.model}},
          {
            resolveMsg: '修改成功',
            rejectMsg: '未通过，修改出错!!!'
          })
        this.$dispatch('close')
      }
    },
    computed: {
      fstate () {
        return this.$appdata.getParam('补气状态')
      },
      ftype () {
        return this.$appdata.getParam('补气类型')
      },

      inputtores () {
        // 获取补气操作员
        console.log('获取补气员', this.$login.f)
        let rs = new Array()
        if (this.$login.f.f_gasman.length > 0) {
          for (let i = 0; i < this.$login.f.f_gasman.length; i++) {
            let temp = {
              label: this.$login.f.f_gasman[i].name,
              value: this.$login.f.f_gasman[i].name
            }
            rs.push(temp)
          }
        }
        // let rs = this.$appdata.getParam('补气员')
        return rs
      }

    },
    watch: {
      'model'(){
        this.initoperator()
      }
    }
  }
</script>
