<template>
  <div class="col-sm-12" style="margin: 20px 0px;">
    <criteria-paged :model="model" v-ref:cp>
      <criteria partial='criteria' @condition-changed='$parent.searchCondition' v-ref:cri>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
        </div>
      </criteria>
      <data-grid :model="model" v-ref:grid partial='list' class="list_area table_sy" style="padding: 0px">
        <template partial='head'>
          <tr>
            <th>序号</th>
            <th>客户编号</th>
            <th>客户名称</th>
            <th>客户电话</th>
            <th>地址信息</th>
            <th>表号</th>
            <th>气表品牌</th>
            <th>气表型号</th>
            <th>
              <button
                v-if="$parent.$parent.$parent.selectdata.defname === '工程施工'"
                type="button"
                class="btn btn-info head-but"
                @click="$parent.$parent.$parent.openSign()"
              >签字照片
              </button>
            </th>
          </tr>
        </template>
        <template partial='body'>
          <tr>
            <td style="text-align: center;">
              <nobr>{{ $index + 1 }}</nobr>
            </td>
            <td style="text-align: center;">
              <nobr>{{ row.f_userinfo_id }}</nobr>
            </td>
            <td style="text-align: center;">
              <nobr>{{ row.f_user_name }}</nobr>
            </td>
            <td style="text-align: center;">
              <nobr>{{ row.f_user_phone }}</nobr>
            </td>
            <td style="text-align: center;">
              <nobr>{{ row.f_address }}{{ row.f_address_detail }}</nobr>
            </td>
            <td style="text-align: center;">
              <nobr>{{ row.f_meternumber }}</nobr>
            </td>
            <td style="text-align: center;">
              <nobr>{{ row.f_meter_brand }}</nobr>
            </td>
            <td style="text-align: center;">
              <nobr>{{ row.f_meter_style }}</nobr>
            </td>
            <td style="text-align: center;">
              <nobr>
                <button
                  type="button" name="button" class="btn btn-link"
                  @click="$parent.$parent.$parent.openModal(row)">
                  {{ $parent.$parent.$parent.mark === 1 || row.f_user_state === '正常' ? '查看' : '修正' }}
                </button>
              </nobr>
            </td>
          </tr>
        </template>
      </data-grid>
    </criteria-paged>
  </div>

  <modal v-if="Sign" :show.sync="Sign" v-ref:modal :large="true" :backdrop="false">
    <header slot="modal-header" class="modal-header">
      <button type="button" class="close" @click="closeUserFile"><span>&times;</span></button>
      <h4 class="modal-title">用户签字</h4>
    </header>
    <article slot="modal-body" class="modal-body clearfix">
      <div class="left col-sm-6 col-xs-6">
        <img-self :src="fileurl" width="100%" height="100%"></img-self>
      </div>
    </article>
    <footer slot="modal-footer" class="modal-footer">
      <template v-if="mark !== 1 && userinfo.f_user_state !== '正常'">
      </template>
    </footer>
  </modal>
  <validator name="v">
    <modal v-if="showModal" :show.sync="showModal" v-ref:modal :large="true" :backdrop="false">
      <header slot="modal-header" class="modal-header">
        <button type="button" class="close" @click="closeUserFile"><span>&times;</span></button>
        <h4 class="modal-title">安装明细</h4>
      </header>
      <article slot="modal-body" class="modal-body clearfix">
        <!-- 表具信息 -->
        <div class="form-group col-sm-12 panel panel-info" v-if="showUserFile">
          <div class="panel-heading head col-sm-12" style="background-color: #e8f4ff;margin-bottom: 10px">
            <div class="col-sm-6 text-left">旧表具信息</div>
          </div>
          <div class="form-group col-sm-6" :class="[$v.f_gasbrand_id.required ? 'has-error' : '']">
            <label class="col-sm-4 control-label">气表品牌：</label>
            <div class="col-sm-8">
              <input type="text" v-show="false" v-model="userfile.f_gasbrand_id" :value.sync="userfile.f_gasbrand_id"
                     v-validate:f_gasbrand_id="['required']">
              <input-select
                class="select select_list"
                :value.sync="userfile.f_gasbrand_id"
                v-model="userfile.f_gasbrand_id"
                :options="meterBrandList"
                @change="gasbrandChange()"
                :disable="mark === 1 || userinfo.f_user_state === '正常' || gasbrandDisable"
                :valueSingle="true"></input-select>
            </div>
          </div>
          <div class="form-group col-sm-6" :class="[$v.f_gasmodel_id.required ? 'has-error' : '']">
            <label class="col-sm-4 control-label">气表型号：</label>
            <div class="col-sm-8">
              <input type="text" v-show="false" v-model="userfile.f_gasmodel_id" :value.sync="userfile.f_gasmodel_id"
                     v-validate:f_gasmodel_id="['required']">
              <input-select
                class="select select_list"
                :value.sync="userfile.f_gasmodel_id"
                v-model="userfile.f_gasmodel_id"
                :options.sync="gasModelList"
                :disable="mark === 1 || userinfo.f_user_state === '正常' || gasbrandDisable"
                :valueSingle="true"></input-select>
            </div>
          </div>
          <div class="form-group col-sm-6" :class="[$v.f_meternumber.required ? 'has-error' : '']">
            <label class="col-sm-4 control-label">表&emsp;&emsp;号：</label>
            <div class="col-sm-8">
              <input class="form-control input_view" style=""
                     placeholder="表号" v-validate:f_meternumber="['required']"
                     v-model="newUserfile.f_meternumber"
                     :value.sync="userfile.f_meternumber"
                     :readonly="mark === 1 || userinfo.f_user_state === '正常'"/>
            </div>
          </div>
          <div class="form-group col-sm-6">
            <label class="col-sm-4 control-label">表&ensp;封&ensp;号：</label>
            <div class="col-sm-8">
              <input class="form-control input_view" style=""
                     placeholder="表封号"
                     v-model="newUserfile.f_metertitles"
                     :value="userfile.f_metertitles"
                     :readonly="mark === 1 || userinfo.f_user_state === '正常'"/>
            </div>
          </div>
          <div class="form-group col-sm-6">
            <label class="col-sm-4 control-label">表&ensp;读&ensp;数：</label>
            <div class="col-sm-8">
              <input class="form-control input_view" style=""
                     type="number"
                     placeholder="表读数"
                     v-model="newUserfile.f_meter_base"
                     :value="userfile.f_meter_base"
                     :readonly="mark === 1 || userinfo.f_user_state === '正常'"/>
            </div>
          </div>
          <div class="form-group col-sm-6">
            <label class="col-sm-4 control-label">初始底数：</label>
            <div class="col-sm-8">
              <input class="form-control input_view" style=""
                     type="number"
                     v-model="newUserfile.f_initial_base"
                     :value="userfile.f_initial_base"
                     placeholder="初始底数"
                     :readonly="mark === 1 || userinfo.f_user_state === '正常'"/>
            </div>
          </div>
          <div class="form-group col-sm-6">
            <label class="col-sm-4 control-label">表&emsp;&emsp;向：</label>
            <div class="col-sm-8">
              <input-select
                class="select select_list"
                :value.sync="userfile.f_aroundmeter"
                v-model="newUserfile.f_aroundmeter"
                :options="aroundmeters"
                :disable="mark === 1 || userinfo.f_user_state === '正常'"
                :valueSingle="true"></input-select>
            </div>
          </div>
          <div class="form-group col-sm-6">
            <label class="col-sm-4 control-label">安装位置：</label>
            <div class="col-sm-8">
              <input-select
                class="select select_list"
                :value.sync="userfile.f_position"
                v-model="newUserfile.f_position"
                :options="positions"
                :disable="mark === 1 || userinfo.f_user_state === '正常'"
                :valueSingle="true"></input-select>
            </div>
          </div>
          <div class="form-group col-sm-6">
            <label class="col-sm-4 control-label">安&ensp;装&ensp;人：</label>
            <div class="col-sm-8">
              <input class="form-control input_view" style=""
                     v-model="newUserfile.f_install_person"
                     :value.sync="installperson(userfile.f_install_person)"
                     placeholder="安装人"
                     readonly/>
            </div>
          </div>
          <div class="form-group col-sm-6">
            <label class="col-sm-4 control-label">安装日期：</label>
            <div class="col-sm-8">
              <input class="form-control input_view" style=""
                     v-model="newUserfile.f_install_date"
                     :value.sync="installdate(userfile.f_install_date)"
                     placeholder="安装日期"
                     readonly/>
            </div>
          </div>
          <div class="form-group col-sm-6">
            <button type="button" class="btn btn-primary" @click="showNewUserFile = !showNewUserFile">换表</button>
          </div>
          <div class="form-group col-sm-12 panel panel-info" v-if="showNewUserFile || newUserfile.f_new_gasbrand_id">
            <div class="panel-heading head col-sm-12" style="background-color: #e8f4ff;margin-bottom: 10px">
              <div class="col-sm-6 text-left">新表具信息</div>
            </div>
            <div class="form-group col-sm-6" :class="[$v.f_gasbrand_id.required ? 'has-error' : '']">
              <label class="col-sm-4 control-label">新气表品牌：</label>
              <div class="col-sm-8">
                <input type="text" v-show="false" v-model="newUserfile.f_new_gasbrand_id"
                       :value.sync="newUserfile.f_new_gasbrand_id">
                <input-select
                  class="select select_list"
                  :value.sync="newUserfile.f_new_gasbrand_id"
                  v-model="newUserfile.f_new_gasbrand_id"
                  :options="meterBrandList"
                  @change="gasbrandChange()"
                  :disable="mark === 1 || gasbrandDisable"
                  :valueSingle="true"></input-select>
              </div>
            </div>
            <div class="form-group col-sm-6">
              <label class="col-sm-4 control-label">新气表型号：</label>
              <div class="col-sm-8">
                <input type="text" v-show="false" v-model="newUserfile.f_new_gasmodel_id"
                       :value.sync="newUserfile.f_new_gasmodel_id">
                <input-select
                  class="select select_list"
                  :value.sync="newUserfile.f_new_gasmodel_id"
                  v-model="newUserfile.f_new_gasmodel_id"
                  :options.sync="newgasModelList"
                  :disable="mark === 1 || gasbrandDisable"
                  :valueSingle="true"></input-select>
              </div>
            </div>
            <div class="form-group col-sm-6">
              <label class="col-sm-4 control-label">新&ensp;表&ensp;号：</label>
              <div class="col-sm-8">
                <input class="form-control input_view" style=""
                       placeholder="新表号"
                       v-model="newUserfile.f_new_meternumber"
                       :value.sync="newUserfile.f_new_meternumber"
                       @change="meternumberValidate()"
                       :readonly="mark === 1"/>
              </div>
            </div>
            <div class="form-group col-sm-6">
              <label class="col-sm-4 control-label">新表封号：</label>
              <div class="col-sm-8">
                <input class="form-control input_view" style=""
                       placeholder="新表封号"
                       v-model="newUserfile.f_new_metertitles"
                       :value="newUserfile.f_new_metertitles"
                       :readonly="mark === 1"/>
              </div>
            </div>
            <div class="form-group col-sm-6">
              <label class="col-sm-4 control-label">新表读数：</label>
              <div class="col-sm-8">
                <input class="form-control input_view" style=""
                       type="number"
                       placeholder="新表读数"
                       v-model="newUserfile.f_new_meter_base"
                       :value="newUserfile.f_new_meter_base"
                       :readonly="mark === 1"/>
              </div>
            </div>
            <div class="form-group col-sm-6">
              <label class="col-sm-4 control-label">新初始底数：</label>
              <div class="col-sm-8">
                <input class="form-control input_view" style=""
                       type="number"
                       v-model="newUserfile.f_new_initial_base"
                       :value="newUserfile.f_new_initial_base"
                       placeholder="新初始底数"
                       :readonly="mark === 1"/>
              </div>
            </div>
            <div class="form-group col-sm-6">
              <label class="col-sm-4 control-label">新&ensp;表&ensp;向：</label>
              <div class="col-sm-8">
                <input-select
                  class="select select_list"
                  :value.sync="newUserfile.f_new_aroundmeter"
                  v-model="newUserfile.f_new_aroundmeter"
                  :options="aroundmeters"
                  :disable="mark === 1"
                  :valueSingle="true"></input-select>
              </div>
            </div>
            <div class="form-group col-sm-6">
              <label class="col-sm-4 control-label">新安装位置：</label>
              <div class="col-sm-8">
                <input-select
                  class="select select_list"
                  :value.sync="newUserfile.f_new_position"
                  v-model="newUserfile.f_new_position"
                  :options="positions"
                  :disable="mark === 1"
                  :valueSingle="true"></input-select>
              </div>
            </div>
          </div>
        </div>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <template v-if="mark !== 1">
          <button type="button" class="btn btn-primary" @click="saveUserFile" :disabled="!$v.valid">确认</button>
        </template>
      </footer>
    </modal>
  </validator>
</template>
<script>
import Vue from 'vue'
import {PagedList} from 'vue-client'
import {HttpResetClass} from 'vue-client'
import {isEmpty} from '../../../Util'
// Date格式化
Date.prototype.Format = function (fmt) {
  var o = {
    'M+': this.getMonth() + 1, // 月份
    'd+': this.getDate(), // 日
    'H+': this.getHours(), // 小时
    'm+': this.getMinutes(), // 分
    's+': this.getSeconds(), // 秒
    'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
    'S': this.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.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)))
  }
  return fmt
}

export default {
  title: '安装明细',
  props: {
    selectdata: {
      type: Object
    },
    mark: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      gasbrandDisable: false,
      showModal: false,
      model: new PagedList('rs/sql/getApplyUserinfo', 20, null),
      areaList: [],
      area: {},
      userinfo: {},
      userfile: {},
      newUserfile: {},
      meterBrandList: [],
      gasModelList: [],
      newgasModelList: [],
      Sign: false,
      fileurl: null,
      showUserinfo: {},
      showNewUserFile: false
    }
  },
  ready() {
    this.search()
    this.getMeterBrand()
  },
  methods: {
    // 保存档案
    async saveUserFile() {
      let data = {
        user: this.$login.f,
        userinfo: this.userinfo,
        userfile: this.userfile,
        newUserfile: this.newUserfile,
        selectdata: this.selectdata
      }
      let res = await this.$resetpost('rs/logic/ApplyAddNweUserfiles', {data: data}, {
        resolveMsg: null,
        rejectMsg: '新表具信息报错！！！'
      })
      this.$dispatch('breakControl', this.selectdata)
    },
    ifDisable() {
      if (this.selectdata.f_apply_type === '非民用报建' && this.selectdata.defname === '工程施工') {
        this.gasbrandDisable = true
      }
    },
    // 检查表号是否存在
    async meternumberValidate() {
      if (!this.newUserfile.f_gasbrand_id) {
        this.newUserfile.f_meternumber = ''
        this.$showMessage('请先选择气表品牌！！！')
        return
      }
      if (this.newUserfile.f_new_gasbrand_id && this.newUserfile.f_new_meternumber) {
        let data = {
          f_meternumber: this.newUserfile.f_new_meternumber,
          f_gasbrand_id: this.newUserfile.f_new_gasbrand_id
        }
        if (this.newUserfile.f_user_id) {
          data.f_user_id = this.newUserfile.f_user_id
        }
        let res = await this.$resetpost('rs/logic/meterbrandsNumberValidate', {data: data}, {
          resolveMsg: null,
          rejectMsg: '表号验证失败！！'
        })
        console.log(`查询结果${res.data}`)
        console.log(res.data)
        if (res.data === '表号已存在' || res.data === '表号无效') {
          this.newUserfile.f_meternumber = ''
          this.$showMessage(res.data)
        }
      }
    },
    // 选择气表品牌
    async gasbrandChange() {
      // 清空已保存的气表型号
      // this.newUserfile.f_new_gasmodel_id = null
      if (isEmpty(this.newUserfile.f_new_gasbrand_id)) {
        return
      }

      let data = {
        f_gasbrand_id: this.newUserfile.f_new_gasbrand_id
      }
      let http = new HttpResetClass()
      let res = await http.load(
        'POST',
        `rs/sql/getGasModel`,
        {data: data},
        {resolveMsg: null, rejectMsg: '气表型号查询失败！！！'}
      )

      this.newgasModelList = res.data

      data = {
        tablename: 't_gasbrand',
        condition: `id = '${this.newUserfile.f_new_gasbrand_id}'`
      }
      res = await http.load(
        'POST',
        `rs/sql/applySingleTable`,
        {data: data},
        {resolveMsg: null, rejectMsg: '表具类型查询失败！！！'}
      )
      this.newUserfile.f_new_meter_classify = res.data[0].f_meter_type
    },
    openSign() {
      this.getFiles()
      this.Sign = true
    },
    async getFiles() {
      let http = new HttpResetClass()
      console.log('=========pc签字查看===============')
      let data = {
        tablename: 't_files',
        condition: `f_blobid = '${this.selectdata.f_process_id}' and defname = '工程施工' and fremarks = '报装手机签字文件'`
      }
      let res = await http.load('POST', `rs/sql/applySingleTable`, {data: data}, {
        warnMsg: null,
        resolveMsg: null
      })
      if (res.data.length > 0) {
        console.log('=========签字地址===============', res.data[0])
        let URL = res.data[0].f_downloadpath.substring(res.data[0].f_downloadpath.lastIndexOf(":\\") + 2)
        res.data[0].f_downloadURL = "http://" + location.host + "/" + URL
        console.log("=====查看文件地址=====", res.data[0].f_downloadURL)
        this.fileurl = res.data[0].f_downloadURL
      }
    },
    // 打开安装明细
    async openModal(row) {
      this.ifDisable()
      if (row) {
        console.log('------------------修正---------------------', row)
        this.userinfo = row


        let data = {
          tablename: 't_userfiles',
          condition: `f_userinfo_id = ${row.f_userinfo_id}`
        }
        let http = new HttpResetClass()
        let res = await http.load(
          'POST',
          `rs/sql/applySingleTable`,
          {data: data},
          {resolveMsg: null, rejectMsg: '表档案查询失败！！！'}
        )
        this.userfile = res.data[0]
        let newdata = {
          tablename: 't_userfiles_modify',
          condition: `f_userinfo_id = ${row.f_userinfo_id}`
        }
        let newRes = await http.load(
          'POST',
          `rs/sql/applySingleTable`,
          {data: newdata},
          {resolveMsg: null, rejectMsg: '更换表档案查询失败！！！'}
        )
        if (newRes.data.length > 0) {

          this.newUserfile = newRes.data[0]
          if (!isEmpty(this.newUserfile.f_new_gasbrand_id)) {
            let data = {
              f_gasbrand_id: this.newUserfile.f_new_gasbrand_id
            }
            let res = await http.load(
              'POST',
              `rs/sql/getGasModel`,
              {data: data},
              {resolveMsg: null, rejectMsg: '气表型号查询失败！！！'}
            )
            this.newgasModelList = res.data
          }
        }

        if (!isEmpty(this.userfile.f_gasbrand_id)) {
          let data = {
            f_gasbrand_id: this.userfile.f_gasbrand_id
          }
          let res = await http.load(
            'POST',
            `rs/sql/getGasModel`,
            {data: data},
            {resolveMsg: null, rejectMsg: '气表型号查询失败！！！'}
          )

          this.gasModelList = res.data
        }
      }
      console.log('cssss', this.newUserfile)
      this.showModal = true
    },
    // 获取气表品牌
    async getMeterBrand() {
      let data = {}

      let http = new HttpResetClass()
      let res = await http.load(
        'POST',
        `rs/sql/getMeterBrand`,
        {data: data},
        {resolveMsg: null, rejectMsg: '气表品牌查询失败！！！'}
      )

      this.meterBrandList = res.data
      this.meterBrandList.push({label: '无', value: 'null'})
    },
    searchCondition(args) {

      args.condition = args.condition + `and ui.f_userinfo_id = '${this.selectdata.f_userinfo_id}'`

      this.model.search(args.condition, args.model)
    },
    // 查询
    async search() {
      this.$refs.cp.$refs.cri.search()
    },
    // 关闭安装明细
    closeUserFile() {
      this.showModal = false
      this.area = {}
      this.userinfo = {}
      this.userfile = {}
      this.gasModelList = {}
      this.newgasModelList = {}
      this.newUserfile = {}
      this.Sign = {}
      this.search()
    }
    //关闭导入弹框
  },
  events: {
  },
  computed: {
    showAddress() {
      return this.selectdata.f_apply_type === '民用报建'
    },
    showUserInfo() {
      return this.selectdata.f_apply_type === '民用报建' && this.selectdata.f_apply_nature !== '分户挂表'
    },
    showUserFile() {
      return (this.selectdata.f_apply_type === '民用报建' && this.selectdata.f_apply_nature !== '分户挂表' && this.selectdata.defname === '工程施工') ||
        this.selectdata.f_apply_type === '非民用报建' ||
        this.selectdata.f_apply_type === '设备变更' ||
        this.selectdata.f_apply_type === '分户挂表'
    },
    showInstall() {
      return this.showUserFile && (this.selectdata.defname === '工程施工' || this.selectdata.defname === '施工通气' || this.selectdata.defname === '点火通气')
    },
    // 证件类型
    credentialsList() {
      return this.$appdata.getParam('证件类型')
    },
    // 安装人
    installperson() {
      return function (f_install_person) {
        if (isEmpty(f_install_person)) {
          return this.$login.f.name
        }
        return f_install_person
      }
    },
    // 安装时间
    installdate() {
      return function (f_install_date) {
        if (isEmpty(f_install_date)) {
          return new Date().Format('yyyy-MM-dd HH:mm:ss')
        }
        return f_install_date
      }
    },
    // 表向
    aroundmeters() {
      return this.$appdata.getParam('左右表')
    },
    // 安装位置
    positions() {
      return this.$appdata.getParam('安装位置')
    }
  },
  watch: {
    'newUserfile.f_new_gasmodel_id'(oval, nval) {
      console.log("ssssssssssss", nval, oval)
    }
  }
}
</script>
<style scoped>
.head-but {
  margin-left: 5px;
  height: 34px;
  /*background-color: #6aa6e2;*/
  border-radius: 4px;
  font-family: PingFang;
  color: #ffffff;
}

/*清除model中的浮动*/
.clearfix:after, .clearfix:before {
  display: table;
}

.clearfix:after {
  clear: both;
}

.input_view {
  background-color: #ffffff;
  border-radius: 2px;
  border: solid 1px #c7c7c7 !important;
  color: #333333 !important;
  font-size: 15px !important;
}

.input_view[readonly] {
  border: 1px solid #DDD !important;
  color: #ACA899 !important;
}

.input_view:disabled {
  border: 1px solid #DDD !important;
  color: #ACA899 !important;
}
</style>
