<template>
  <work-busy :is-busy="showWork" v-show="showWork"></work-busy>

  <div class="col-sm-12" style="margin: 20px 0px;">
    <criteria-paged :model="model" v-ref:cp>
      <criteria partial='criteria' @condition-changed='search' 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 class="textNoLineBreak">序号</th>
            <th class="textNoLineBreak">客户编号</th>
            <th class="textNoLineBreak">用户姓名</th>
            <th class="textNoLineBreak">电话</th>
            <th class="textNoLineBreak">证件类型</th>
            <th class="textNoLineBreak">证件号码</th>
            <th class="textNoLineBreak">表号</th>
            <th class="textNoLineBreak">地址</th>
            <th class="textNoLineBreak">当前状态</th>
            <th class="textNoLineBreak">
              <button
                v-if="$parent.$parent.$parent.selectdata.defname === '工程施工'"
                type="button"
                class="btn btn-info head-but"
                @click="$parent.$parent.$parent.openSign()"
              >签字照片
              </button>
              <p v-else>操作</p>
            </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_credentials }}</nobr>
            </td>
            <td style="text-align: center;">
              <nobr>{{ row.f_idnumber }}</nobr>
            </td>
            <td style="text-align: center;">
              <nobr>{{ row.f_meternumber }}</nobr>
            </td>
            <td>
              <nobr>{{ row.f_address }}</nobr>
            </td>
            <td>
              <nobr>{{ row.f_sub_state }}</nobr>
            </td>
            <td>
              <nobr>
                <button
                  type="button" name="button" class="btn btn-link"
                  @click="$parent.$parent.$parent.openModel(row)"
                >
                  查看
                </button>
                <button
                  type="button" name="button" class="btn btn-link"
                  @click="$parent.$parent.$parent.deleteUserinfo(row)"
                  v-if="$parent.$parent.$parent.showDeleteUserInfo"
                  :disabled="row.f_sub_state === '已销户'"
                >
                  销户
                </button>
                <button
                  type="button" name="button" class="btn btn-link"
                  @click="$parent.$parent.$parent.deleteChaiChuInfo(row.id)"
                  v-if="$parent.$parent.$parent.showDeleteButton"
                  :disabled="$parent.$parent.$parent.mark === 1"
                >
                  删除
                </button>
              </nobr>
            </td>
          </tr>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
  <modal v-if="showModel" :show.sync="showModel" v-ref:modal :large="true" :backdrop="false">
    <header slot="modal-header" class="modal-header">
      <button type="button" class="close" @click="closeModel"><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">
        <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>
      <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="userinfo.f_userinfo_id"
                 :value.sync="userinfo.f_userinfo_id"
                 :readonly="true"
                 placeholder="客户名称"/>
        </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="userinfo.f_user_name"
                 :value.sync="userinfo.f_user_name"
                 :readonly="true"
                 placeholder="客户名称"/>
        </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="userinfo.f_user_phone"
                 :value.sync="userinfo.f_user_phone"
                 :readonly="true"
                 placeholder="客户电话"/>
        </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="userinfo.f_credentials"
                 :value.sync="userinfo.f_credentials"
                 :readonly="true"
                 placeholder="证件类型"/>
        </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="userinfo.f_idnumber"
                 :value="userinfo.f_idnumber"
                 :readonly="true"/>
        </div>
      </div>
      <!-- 地址信息 -->
      <div class="form-group col-sm-12">
        <label class="col-sm-2 control-label">地&emsp;&emsp;址：</label>
        <div class="col-sm-10">
          <input class="form-control input_view"
                 placeholder="地址"
                 v-model="userinfo.f_address"
                 :value.sync="userinfo.f_address"
                 :readonly="true"/>
        </div>
      </div>
      <!-- 表具信息 -->
      <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">
        <label class="col-sm-4 control-label">气表品牌：</label>
        <div class="col-sm-8">
          <input class="form-control input_view"
                 placeholder="气表品牌"
                 v-model="userinfo.f_gasbrand"
                 :value.sync="userinfo.f_gasbrand"
                 :readonly="true"/>
        </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"
                 placeholder="气表型号"
                 v-model="userinfo.f_gasmodel"
                 :value.sync="userinfo.f_gasmodel"
                 :readonly="true"/>
        </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 class="form-control input_view" style=""
                 placeholder="表号"
                 v-model="userinfo.f_meternumber"
                 :value.sync="userinfo.f_meternumber"
                 :readonly="true"/>
        </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="userinfo.f_metertitles"
                 :value="userinfo.f_metertitles"
                 :readonly="true"/>
        </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="userinfo.f_meter_base"
                 :value="userinfo.f_meter_base"
                 :readonly="true"/>
        </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="userinfo.f_delivery_date"
                 :value="userinfo.f_delivery_date"
                 :readonly="true"/>
        </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="userinfo.f_balance_amount"
                 :value="userinfo.f_balance_amount"
                 :readonly="true"/>
        </div>
      </div>
      <div class="form-group col-sm-6" v-if="selectdata.defname === '核对气量'">
        <label class="col-sm-4 control-label">累购气量：</label>
        <div class="col-sm-8">
          <input class="form-control input_view" style=""
                 type="number"
                 v-model="inputNumber.f_gas_total"
                 :value="inputNumber.f_gas_total"
                 :readonly="nowData.f_sub_state === '已销户'"/>
        </div>
      </div>
      <div class="form-group col-sm-6" v-if="selectdata.defname === '核对气量'">
        <label class="col-sm-4 control-label">累购金额：</label>
        <div class="col-sm-8">
          <input class="form-control input_view" style=""
                 type="number"
                 v-model="inputNumber.f_money_total"
                 :value="inputNumber.f_money_total"
                 :readonly="nowData.f_sub_state === '已销户'"/>
        </div>
      </div>
      <div v-for="(i, item) in devicesInfo" class="form-group col-sm-12 panel panel-info">
        <div class="panel-heading head col-sm-12" style="background-color: #e8f4ff;margin-bottom: 10px">
          <div class="col-sm-12 text-left">设备{{ $index + 1 }}信息</div>
        </div>
        <div class="form-group col-sm-3">
          <label class="col-sm-4 control-label">设备：</label>
          <div class="col-sm-8">
            <input class="form-control input_view" style=""
                   placeholder="设备类型"
                   v-model="item.f_devices_type"
                   :value="item.f_devices_type"
                   readonly/>
          </div>
        </div>
        <div class="form-group col-sm-3">
          <label class="col-sm-4 control-label">编号：</label>
          <div class="col-sm-8">
            <input class="form-control input_view" style=""
                   placeholder="设备编号"
                   v-model="item.f_devices_no"
                   :value="item.f_devices_no"
                   readonly/>
          </div>
        </div>
        <div class="form-group col-sm-3">
          <label class="col-sm-4 control-label">品牌：</label>
          <div class="col-sm-8">
            <input class="form-control input_view" style=""
                   placeholder="设备品牌"
                   v-model="item.f_brand"
                   :value="item.f_brand"
                   readonly/>
          </div>
        </div>
        <div class="form-group col-sm-3">
          <label class="col-sm-4 control-label">型号：</label>
          <div class="col-sm-8">
            <input class="form-control input_view" style=""
                   placeholder="设备型号"
                   v-model="item.f_devices_model"
                   :value="item.f_devices_model"
                   readonly/>
          </div>
        </div>
        <div class="form-group col-sm-3">
          <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="item.f_devices_num"
                   :value="item.f_devices_num"
                   readonly/>
          </div>
        </div>
        <div class="form-group col-sm-3">
          <label class="col-sm-4 control-label">功率：</label>
          <div class="col-sm-8">
            <input class="form-control input_view" style=""
                   placeholder="设备功率"
                   type="number"
                   v-model="item.f_devices_tonnage"
                   :value="item.f_devices_tonnage"
                   readonly/>
          </div>
        </div>
        <div class="form-group col-sm-3">
          <label class="col-sm-4 control-label">日期：</label>
          <div class="col-sm-8">
            <input class="form-control input_view" style=""
                   placeholder="安装日期"
                   v-model="item.f_install_date"
                   :value="item.f_install_date"
                   readonly/>
          </div>
        </div>
        <div class="form-group col-sm-3">
          <label class="col-sm-4 control-label">单位：</label>
          <div class="col-sm-8">
            <input class="form-control input_view" style=""
                   placeholder="功率单位"
                   v-model="item.f_devices_tonnage_unit"
                   :value="item.f_devices_tonnage_unit"
                   readonly/>
          </div>
        </div>
      </div>
    </article>
    <footer slot="modal-footer" class="modal-footer">
      <template v-if="selectdata.defname === '核对气量'">
        <button
          type="button" name="button" class="btn btn-info"
          @click="closeModel()"
        >取消</button>
        <button
          type="button" name="button" class="btn btn-info"
          @click="saveGasNumber(userinfo)"
          :disabled="nowData.f_sub_state === '已销户'"
        >保存</button>
      </template>
    </footer>
  </modal>
  <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="closeModel"><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="x">
    <modal v-if="showDeleteModel" :show.sync="showDeleteModel" v-ref:modal :large="true" :backdrop="false">
      <header slot="modal-header" class="modal-header">
        <button type="button" class="close" @click="closeModel"><span>&times;</span></button>
        <h4 class="modal-title">销户原因</h4>
      </header>
      <article slot="modal-body" class="modal-body clearfix">
        <div class="left col-sm-12 col-xs-12" :class="[$x.f_cancel_cause.required ? 'has-error' : '']">
          <div class="form-group col-sm-12">
            <label class="col-sm-2 control-label">销户原因：</label>
            <div class="col-sm-10">
              <input class="form-control input_view" style=""
                     v-validate:f_cancel_cause="['required']"
                     v-model="userinfo.f_cancel_cause"
                     :value.sync="userinfo.f_cancel_cause"
                     placeholder="销户原因"/>
            </div>
          </div>
        </div>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <button type="button" class="btn btn-primary" @click="deleteUser" :disabled="!$x.valid">确认</button>
      </footer>
    </modal>
  </validator>
</template>
<script>
import {PagedList} from 'vue-client'
import {HttpResetClass} from 'vue-client'

export default {
  title: '拆除用户信息',
  props: {
    selectdata: {
      type: Object
    },
    mark: {
      type: Number,
      default: 0        // 0：正常  1：禁用
    }
  },
  data() {
    return {
      model: new PagedList('rs/sql/getChaiChuUserinfo', 20, {
        data: {
          f_process_id: this.selectdata.f_process_id
        }
      }),
      showWork: false,
      userinfo: {},
      showDeleteModel: false,
      Sign: false,
      fileUrl: null,
      showModel: false,
      devicesInfo: {},
      nowData:{},
      inputNumber: {
        f_gas_total: null,
        f_money_total: null
      }
    }
  },
  ready() {
    this.search()
  },
  methods: {
    async openModel (row) {
      this.nowData = row
      this.getDeviceList(row)
      let data = {
        f_userinfo_id: row.f_userinfo_id
      }
      let http = new HttpResetClass()
      let res = await http.load(
        'POST',
        `rs/sql/getUserAllInfo`,
        {data: data},
        {resolveMsg: null, rejectMsg: '用户信息查询失败！！！'}
      )
      if (res.data.length > 0){
        this.userinfo = res.data[0]
      }

      this.inputNumber.f_gas_total = row.f_gas_total
      this.inputNumber.f_money_total = row.f_money_total

      this.showModel = !this.showModel
    },
    async getDeviceList(row){
      let data = {
        tablename: 't_devices',
        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.devicesInfo = res.data
    },
    async deleteUserinfo(row) {
      this.userinfo = row
      this.showDeleteModel = !this.showDeleteModel
    },
    async deleteUser (row) {
      let res = await this.$showMessage('此操作不可撤回，您确定要删除这条记录吗？')
      if (res === 'cancel') {
        return
      }
      let data = {
        login: this.$login.f,
        userinfo: this.userinfo,
        selectdata: this.selectdata
      }
      res = await this.$resetpost('rs/logic/applyDeleteUserinfo', {data:data}, {
        resolveMsg: null,
        rejectMsg: '删除失败！！！'
      })

      this.$dispatch('breakControl', this.selectdata)
    },
    deleteChaiChuInfo(index) {
      this.$resetdelete(`rs/entity/t_apply_chai`, {id: index}, {
        resolveMsg: '删除成功',
        rejectMsg: '删除失败'
      }).then((res) => {
        this.$dispatch('breakControl', this.selectdata)
      })
    },
    async saveGasNumber(row) {
      let data = {
        userinfo: row,
        inputNumber: this.inputNumber,
        selectdata: this.selectdata
      }
      let res = await this.$resetpost('rs/logic/applySaveInfo', {data: data}, {
        resolveMsg: '保存成功！！！',
        rejectMsg: '保存失败！！！'
      })
      this.$dispatch('breakControl', this.selectdata)
    },
    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 search() {
      this.$refs.cp.$refs.cri.search()
    },
    // 关闭文件上传对话框
    closeModel() {
      this.showWork = false
      this.showModel = false
      this.showDeleteModel = false
      this.userinfo = {}
      this.devicesInfo = {}
      this.Sign = false
      // 将选的文件清空
      this.$refs.file.$el.querySelector('input').value = ''
      this.search()
    }
  },
  events: {
    'beforeFileUpload'(file) {
      this.showWork = true
    }
  },
  computed: {
    showDeleteUserInfo() {
      return this.selectdata.f_apply_type === '设备拆除' && this.selectdata.defname === '核对气量'
    },
    showDeleteButton() {
      return this.selectdata.f_apply_type === '设备拆除' && this.selectdata.defname === '报建受理'
    },
  },
  watch: {}
}
</script>
<style scoped>
.textNoLineBreak {
  white-space: nowrap;
}

.head-but {
  margin-left: 5px;
  height: 34px;
  /*background-color: #6aa6e2;*/
  border-radius: 4px;
  font-family: PingFang;
  color: #ffffff;
}
</style>
