<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">用户姓名</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">换表类型</label>
            <input type="text" v-model="model.f_type" disabled=disabled style="width: 60%" class="input_search">
          </div>
          <div class="col-sm-4">
            <label class="font_normal_body">换表费&emsp;</label>
            <input type="text" v-model="model.f_changemeter_fee" disabled=disabled style="width: 60%" class="input_search">
          </div>
        </div>

        <div class="row">
          <div class="col-sm-4">
            <label class="font_normal_body">旧表号&emsp;</label>
            <input type="text" v-model="model.f_meternumber_old" disabled=disabled style="width: 60%" class="input_search">
          </div>
          <div class="col-sm-4">
            <label class="font_normal_body">新表号&emsp;</label>
            <input type="text" v-model="model.f_meternumber" 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_meter_brand_old" 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_meter_brand" disabled=disabled style="width: 60%" class="input_search">
          </div>
          <div class="col-sm-4">
            <label class="font_normal_body">补气量&emsp;</label>
            <input type="text" v-model="model.f_remanent_gas" 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_remanent_money" 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">
            <!--            <v-select :value.sync="model.f_changemeter_state" v-model="model.f_changemeter_state"-->
            <!--                      :options='changestate' placeholder='换表状态'-->
            <!--                      :value-single="true"-->
            <!--                      :search="false"-->
            <!--                      close-on-select></v-select>-->

          </div>

        </div>
        <div class="row">
          <div class="col-sm-4">
            <label class="font_normal_body">换表人&emsp;</label>
            <input type="text" v-model="model.f_change_operator" style="width: 60%" class="input_search">
          </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>
            <!--                      @click="selectoperator"-->
          </div>
          <div class="col-sm-4">
            <label class="font_normal_body">换表日期</label>
            <datepicker  placeholder="换表日期" style="width: 60%"
                         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-8">
            <label class="font_normal_body">地&emsp;&emsp;址</label>
            <input type="text" v-model="model.f_address" disabled=disabled style="width: 80%" class="input_search">
          </div>
        </div>
        <!--        <div class="row">-->
        <!--          <div class="col-sm-4">-->
        <!--            <label class="font_normal_body">下发日期</label>-->
        <!--            <datepicker id="f_hand_date"-->
        <!--                        placeholder="下发日期"-->
        <!--                        style="width:60%"-->
        <!--                        v-model="model.f_hand_date"-->
        <!--                        :value.sync="model.f_hand_date"-->
        <!--                        :format="'yyyy-MM-dd HH:mm:ss'"-->
        <!--            >-->
        <!--            </datepicker>-->
        <!--          </div>-->
        <!--         -->

        <!--        </div>-->

        <div class="row">
          <div class="col-sm-8">
            <label class="font_normal_body">换表原因</label>
            <input type="text" v-model="model.f_comments"  style="width: 80%" class="input_search">
          </div>
        </div>
        <div class="row">
          <div class="col-sm-8">
            <label class="font_normal_body">其他备注</label>
            <input type="text" v-model="model.f_othereason"  style="width: 80%" 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 {
        uploadText: '换表',
        // 构建数组来存储对象
        editbank: true,
        recordList: [],
        foperator: [],
        changestate: [{label: '有效', value: '有效'}, {label: '无效', value: '无效'}],
        showfiles: false,
        // meterstate:[{label:'已抄',value:'已抄'},{label:'未抄',value:'未抄'}],
        // handstate:[{label:'有效',value:'有效'},{label:'冲正',value:'冲正'},{label:'作废',value:'作废'},{label:'待审核',value:'待审核'}],
        fileNameSet: {
          'f_change_operator': '换表人',
          'f_operate_date': '换表日期',
          'f_comments': '换表原因',
          'f_othereason': '其他备注'
          // 'f_newtable_state':'新表状态'
        }
      }
    },
    props: {
      oldmodel: Object,
      model: Object
    },

    ready () {
      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)
        })
      },
      cancel () {
        this.$dispatch('close')
      },

      uploadFiles () {
        this.showfiles = !this.showfiles
      },
      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_changetables',
              oldvalue: this.oldmodel[item],
              newvalue: this.model[item],
              state: '有效',
              modifitime: Util.toStandardTimeString(),
              operator: this.$login.f.name,
              changemeter_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/saveChangeMeterRecord', {data: {recordlist: this.recordList, newData: this.model}})
        // let changetablesData = {
        //   id: this.model.id,
        //   f_change_operator: this.model.f_change_operator,
        //   f_comments: this.model.f_comments,
        //   f_othereason: this.model.f_othereason,
        //   f_operate_date: this.model.f_operate_date
        // }
        let sqldata = `update t_changetables set
            f_change_operator ='${this.model.f_change_operator}',
            f_comments ='${this.model.f_comments}',
            f_othereason ='${this.model.f_othereason}',
            f_operate_date='${this.model.f_operate_date}'
        where  id ='${this.model.id}'`
        console.log('换表记录修正保存', sqldata)
        await this.$resetpost('rs/logic/runSQL', {data: {sql: sqldata}}, {resolveMsg: null, rejectMsg: null})
        // await this.$resetpost('rs/entity/t_changetables', changetablesData, {resolveMsg: '修改成功', rejectMsg: '未通过，修改出错!!!'})
        // console.log('this.model', this.model)
        this.$dispatch('close')
      }
    },
    computed: {
      meterstate () {
        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>
