<template>
  <div @keyup.enter="search" class="flex-row" id="unit">
    <div class="basic-main">
      <criteria-paged :model="model" v-ref:paged>
        <criteria @condition-changed='$parent.selfSearch' partial='criteria' v-ref:cri>
          <div class="form-horizontal select-overspread container-fluid auto" novalidate partial>
            <div class="row">
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">组织机构</label>
                <res-select :initresid='$parent.$parent.curorgid'
                            @res-select="$parent.$parent.getorg"
                            is-mul="false"
                            restype='organization'>
                </res-select>
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">客户姓名</label>
                <input class="input_search" condition="f_user_name like '%{}%'" placeholder="客户姓名" style="width:60%"
                       style="width:60%"
                       type="text" v-model="model.f_user_name"
                />
              </div>
              <div class="col-sm-2 form-group ">
                <label class="font_normal_body">客户编号</label>
                <input class="input_search" condition="f_userinfo_code like '%{}%'" placeholder="客户编号" style="width:60%"
                       style="width:60%"
                       type="text" v-model="model.f_building"
                />
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">客户地址</label>
                <input class="input_search" condition="f_address like '%{}%'" placeholder="客户地址" style="width:60%"
                       style="width:60%"
                       type="text" v-model="model.f_address"
                />
              </div>
              <div class="form-group col-sm-3 button-range" style="float: left">
                <button @click="search()" class="button_search button_spacing">查询</button>
                <button @click="$parent.$parent.submit()" class="button_export button_spacing"
                        style="width: max-content">合并档案
                </button>
              </div>
            </div>
          </div>
        </criteria>
        <data-grid :model="model" class="list_area table_sy" partial='list' v-ref:grid>
          <template partial='head'>
            <tr>
              <th style="padding: 0;border: 0">选择</th>
              <th>
                <nobr>客户编号</nobr>
              </th>
              <th>
                <nobr>客户姓名</nobr>
              </th>
              <th>
                <nobr>客户电话</nobr>
              </th>
              <th>
                <nobr>客户地址</nobr>
              </th>
              <th>
                <nobr>客户状态</nobr>
              </th>
              <th>
                <nobr>表号</nobr>
              </th>
              <th>
                <nobr>表型号</nobr>
              </th>
              <th>
                <nobr>表状态</nobr>
              </th>
            </tr>
          </template>
          <template partial='body'>
            <tr>
              <td style="text-align: center;padding: 0;border: 0">
                <input :checked="$parent.$parent.$parent.ischecked(row)"
                       @change="$parent.$parent.$parent.selectOne($event,row,$index)"
                       style="width: 22px;height: 22px;"
                       type='checkbox' v-show="row.f_user_state === '正常'"/>
              </td>
              <td style="text-align:center"><nobr>{{row.f_userinfo_code}}</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}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_user_state}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_meternumber}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_meter_brand}}{{row.f_meter_style}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_table_state}}</nobr></td>
            </tr>
          </template>
          <template partial='foot'></template>
        </data-grid>
      </criteria-paged>
    </div>
  </div>
  <!--<modal :show.sync="showmergeinfo" width="80%" title="选择合并后用户" v-ref:modal large backdrop="false">
    <article slot="modal-body" class="modal-body">
      <file-merge-info :mergeinfo="selecteddata" v-ref=""></file-merge-info>
    </article>
    <footer slot="modal-footer" class="modal-footer">
    </footer>
  </modal>-->
  <modal :show.sync="showmergeinfo" width="80%" title="选择合并后用户" v-ref:modal large backdrop="false">
    <article slot="modal-body" class="modal-body">
      <file-merge-info :mergeinfo=selecteddata></file-merge-info>
    </article>
    <footer slot="modal-footer" class="modal-footer">
    </footer>
  </modal>
</template>

<script>
  import { PagedList } from 'vue-client'
  import {HttpResetClass} from 'vue-client'

  export default {
    data () {
      return {
        model: new PagedList('rs/sql/filemanage_getmergeuser', 20, {}),
        selecteddata: {
          data: []
        },
        showmergeinfo: false
      }
    },
    title: '档案合并',
    ready () {
      this.search()
    },
    computed: {
      ischecked () {
        return function (row) {
          return this.selecteddata.data.includes(row)
        }
      }
    },
    events: {
      init (val) {
        this.showmergeinfo = false
        this.search()
        let msg = `成功将 ${val.oldcode} 档案信息合并至 ${val.newcode}`
        if (val.cancelcode.length > 0) {
          msg = `${msg} ,并对${val.cancelcode} 销户`
        }
        this.$showMessage(msg)
      }
    },
    methods: {

      search () {
        this.$refs.paged.$refs.cri.search()
      },

      async selfSearch (args) {
        this.selecteddata.data = []
        this.rowsdata = []
        this.all = false
        if (!this.f_orgid) {
          this.getorg([this.$login.f.orgid])
        }

        args.condition = `${args.condition} and f_filialeid = '${this.f_filialeids}'`
        await this.model.search(args.condition, args.model)
        this.length = this.model.count
      },

      // 清空
      clear () {
        this.$refs.paged.$refs.cri.model = {}
      },
      getorg (val) {
        this.f_orgid = this.$login.convertToIn(val)
        this.f_filialeids = val[0]
      },
      selectOne (event, row, i) {
        if (this.all) {
          this.all = !this.all
        }
        if (event.target.checked) {
          this.selecteddata.data.push(row)
          if (this.length === this.selecteddata.data.length) {
            this.all = true
          }
        } else {
          this.all = false
          this.selecteddata.data.splice(this.selecteddata.data.indexOf(row), 1)
        }
      },
      submit () {
        if (this.selecteddata.data.length < 2) {
          this.$showAlert('请选择至少两户档案', 'danger')
          return
        }
        this.showmergeinfo = true
      }
    }

  }
</script>
