<template>
  <div class="auto">
    <validator name='a' @valid="addressValid(true)" @invalid="addressValid(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-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_total_gas" disabled=disabled style="width: 60%" class="input_search">
          </div>
          <div class="col-sm-4">
            <label class="font_normal_body">&nbsp;&nbsp;补气量&nbsp;</label>
            <input type="text" v-model="model.f_pregas" style="width: 60%" class="input_search">
          </div>
          <div class="col-sm-4">
            <label class="font_normal_body">气表品牌</label>
            <input type="text" v-model="model.f_meter_brand" 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_preamount" disabled=disabled style="width: 60%" class="input_search">
          </div>
          <div class="col-sm-4">
            <label class="font_normal_body">&nbsp;&nbsp;&nbsp;&nbsp;次数&nbsp&nbsp</label>
            <input type="text" v-model="model.f_times" disabled=disabled style="width: 60%" class="input_search">
          </div>
          <div class="col-sm-4">
            <label class="font_normal_body">写卡状态</label>
            <input type="text" v-model="model.f_write_card" 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_state" disabled=disabled style="width: 60%" class="input_search">
          </div>
          <div class="col-sm-4">
            <label class="font_normal_body">补气类型</label>
            <input type="text" v-model="model.f_type" disabled=disabled style="width: 60%" class="input_search">
          </div>

        </div>
        <div class="row">
          <div class="col-sm-4">
            <label class="font_normal_body">&nbsp&nbsp补气员&nbsp</label>
            <v-select :value.sync="model.f_write_operator" v-model="model.f_write_operator"  :value-single="true"
                      :options='inputtores' placeholder='补气员' 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 class="col-sm-4">
            <label class="font_normal_body">所属部门</label>
            <input type="text" v-model="model.f_depname" disabled=disabled style="width: 60%" class="input_search">
          </div>
          <div class="col-sm-12">
            <label  class="font_normal_body" >补气原因</label>
            <textarea class="form-control" v-model="model.f_comments" rows="3" ></textarea>
          </div>
        </div>
      </form>
      <div style="float: right">
        <button class="button_search" @click="save">保存</button>
        <button class="button_clear" @click="cancel">取消</button>
      </div>
      <split-price :showdata="model" :pricesplit="priceSplit" v-if="priceSplit" ></split-price>
    </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";
  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:'机表扣费'}],

        // meterstate:[{label:'已抄',value:'已抄'},{label:'未抄',value:'未抄'}],
        // fstate:[{label:'有效',value:'有效'},{label:'冲正',value:'冲正'},{label:'作废',value:'作废'},{label:'待审核',value:'待审核'}],
        fileNameSet: {
          'f_userinfo_code': '客户编号',
          'f_card_id': '卡号',
          'f_operate_date': '补气日期',
          'f_total_gas': '表累购气量',
          'f_pregas': '补气量',
          'f_preamount': '补气金额',
          'f_times': '次数',
          'f_type': '补气类型',
          'f_state': '补气状态',
          'f_write_operator': '补气执行人',
          'f_operator': '操作人员',
          'f_meter_brand': '气表品牌',
          'f_write_card': '写卡状态',
          'f_comments': '补气原因',
          'f_depname': '所属部门'
        }
      }
    },
    props: {
      oldmodel: Object,
      model: Object
    },

    ready () {
      /* console.log('logininfo',this.$login.r) */
      if (this.$login.r.includes('维护银行转账信息')) {
        this.editbank = true
      }
      // console.log(this.props.model)
      this.oldmodel = JSON.parse(JSON.stringify(this.model))
      readyGen(this)
    },
    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
      },
      async getenddate () {
        var end_date = await this.$resetpost('rs/logic/getEndDate', {data: {f_userinfo_id: this.model.f_userinfo_id, f_input_date: this.model.f_input_date}})
        this.model.f_end_date = end_date.data
      },
      addressValid (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_fillgas',
              oldvalue: this.oldmodel[item],
              newvalue: this.model[item],
              state: '有效',
              modifitime: Util.toStandardTimeString(),
              operator: this.$login.f.name,
              fillgas_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/saveFillgasRecord', {data: {recordlist: this.recordList, newData: this.model}})
        await this.$resetpost('rs/entity/t_fillgas', this.model, {
          resolveMsg: '修改成功',
          rejectMsg: '未通过，修改出错!!!'
        })
        console.log('this.model', this.model)
        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.f_input_date' (val) {
        if (val) {
          this.getenddate()
        }
      },
      'model'(){
        this.initoperator()
      }
    }
  }
</script>
