<template>
  <div id="unit" class="flex-row">
    <div class="basic-main" @keyup.enter="search">
      <div class="flex" >
        <criteria-paged :model="model" v-ref:paged>
          <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri>
            <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
              <div class="row">
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body" title="限购时间">限购时间</label>
                  <datepicker :format="'yyyy-MM-dd'" :value.sync="model.startDate"
                              class="datepicker"
                              placeholder="限购开始时间"
                              style="width:60%"
                              condition="f_operate_date >= '{} 00:00:00'"
                              v-model="model.startDate">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body" title="限购时间">&nbsp;&nbsp;&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
                  <datepicker :format="'yyyy-MM-dd'" :value.sync="model.endDate"
                              class="datepicker"
                              placeholder="限购结束时间"
                              style="width:60%"
                              condition="f_operate_date <= '{} 23:59:59'"
                              v-model="model.endDate">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">客户编号</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_userinfo_code"
                         condition="f_userinfo_code = '{}' " placeholder="客户编号">
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">客户名称</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_user_name"
                         condition="f_user_name = '{}'" placeholder="客户名称">
                </div>
                <div class="span" style="float:right;">
                  <button class="button_search button_spacing" @click="search()">查询</button>
                  <button class="button_clear  button_spacing" @click="$parent.$parent.clear()">清空</button>
                  <button class="button_clear  button_spacing" @click="$parent.$parent.batchOper()">批量解除限购</button>
                  <export-excel :data="$parent.$parent.getCondition"
                                :field="$parent.$parent.getfield"
                                :header="$parent.$parent.other"
                                sqlurl="api/af-revenue/logic/openapi/exportfile"
                                sql-name="manage_quotaRecordQuery"
                                template-name='限购记录查询'
                                :choose-col="true"></export-excel>
                  <print-data :sum-field="$parent.$parent.getfield"
                              :model="$parent.model"
                              :field="$parent.$parent.getfield"
                              titletable="限购记录查询"
                              :defaultfield="$parent.$parent.defaultfield"
                              ></print-data>
                  <div
                    :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                    @click="$parent.$parent.hidden()"
                    class="button_spacing"
                    style="float: right"></div>
                </div>
              </div>
              <div class="row" v-if="$parent.$parent.criteriaShow">
                <res-select-group
                  :style="$parent.$parent.style"
                  :show-component="['company']"
                  :initres="$parent.$parent.initres"
                  @re-res="$parent.$parent.getRes"
                  v-ref:sel>
                </res-select-group>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">客户电话</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_user_phone"
                         condition="f_user_phone like '%{}%'" placeholder="客户电话">
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">客户地址</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_address"
                         condition="f_address like '%{}%'" placeholder='客户地址'>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">限购原因</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_operate_reason"
                         condition="f_operate_reason = '{}'" placeholder='限购原因'>
                </div>
              </div>
            </div>
          </criteria>
          <data-grid :model="model" class="list_area table_sy" class="list_area table_sy" :optional="true" partial='list' v-ref:grid>
            <template partial='head'>
              <tr>
                <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>
                <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' partial='list' v-ref:grid>
              <tr>
                <td style="text-align:center">{{ row.f_userinfo_code }}</td>
                <td style="text-align:center">{{ row.f_user_name }}</td>
                <td style="text-align:center">{{ row.f_user_phone }}</td>
                <td style="text-align:center">{{ row.f_address }}</td>
                <td style="text-align:center">{{ row.f_operate_people }}</td>
                <td style="text-align:center">{{ row.f_limit_source }}</td>
                <td style="text-align:center">{{ row.f_operate_date }}</td>
                <td style="text-align:center">{{ row.f_limit_type }}</td>
                <td style="text-align:center">{{ row.f_time_type }}</td>
                <td style="text-align:center">{{ row.f_time_value }}</td>
                <td style="text-align:center">{{ row.f_limit_times }}</td>
                <td style="text-align:center">{{ row.f_limit_value }}</td>
                <td style="text-align:center">{{ row.f_start_date }}</td>
                <td style="text-align:center">{{ row.f_operate_reason }}</td>
                <td style="text-align: center;">
                  <button type="button" name="button" class="button_search button_spacing width-60"
                          @click.stop="$parent.$parent.$parent.toDetail(row)">查看
                  </button>
                </td>
              </tr>
            </template>
            <template partial='foot'></template>
          </data-grid>
        </criteria-paged>
        <modal :show.sync="allQuotaRecordModalShow" backdrop="false"
               class="allQuotaRecordModalShow" width="80%">
          <header slot="modal-header" class="modal-header" style="text-align: center">
            <h4 class="modal-title">用户完整限购记录</h4>
          </header>
          <article slot="modal-body" class="modal-body">
            <data-grid class="list_area table_sy" :model="modalModel" v-ref:modalgrid :optional="true" partial='list'>
              <template partial='head'>
                <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>
                <th>
                  <nobr>限购次数</nobr>
                </th>
                <th>
                  <nobr>单次限购量</nobr>
                </th>
                <th>
                  <nobr>执行时间</nobr>
                </th>
                <th>
                  <nobr>限购原因</nobr>
                </th>
                <th>
                  <nobr>操作</nobr>
                </th>
              </template>
              <template partial='body'>
                <td style="text-align:center">{{row.f_userinfo_code}}</td>
                <td style="text-align:center">{{row.f_user_name }}</td>
                <td style="text-align:center">{{row.f_operate_people}}</td>
                <td style="text-align:center">{{row.f_limit_source}}</td>
                <td style="text-align:center">{{row.f_operate_date}}</td>
                <td style="text-align:center">{{row.f_limit_type}}</td>
                <td style="text-align:center">{{row.f_time_type}}</td>
                <td style="text-align:center">{{row.f_time_value}}</td>
                <td style="text-align:center">{{row.f_limit_times}}</td>
                <td style="text-align:center">{{row.f_limit_value}}</td>
                <td style="text-align:center">{{row.f_start_date}}</td>
                <td style="text-align:center">{{row.f_operate_reason}}</td>
                <td style="text-align: center;">
                  <button type="button" name="button" class="button_search button_spacing width-60"
                          @click.stop="$parent.$parent.$parent.secureQuotaShow(row)">作废限购
                  </button>
                </td>
              </template>
            </data-grid>
          </article>
          <footer slot="modal-footer" class="modal-footer">
            <button type="button" class="btn btn-default" @click='batchSecureQuota'>批量作废限购</button>
            <button type="button" class="btn btn-default" @click='allQuotaRecordModalClose'>关闭</button>
          </footer>
        </modal>
        <modal :show.sync="secureQuotaModalShow" v-ref:secureQuotaModalShow
               backdrop="false" class="secureQuotaModalShow" @modal-close="secureQuotaModalClose">
          <header slot="modal-header" class="modal-header" style="text-align: center">
          </header>
          <article slot="modal-body" class="modal-body">
            <div class="auto select-overspread form-horizontal">
              <div class="row auto" :class="[ !secureData.f_cancel_reason? 'has-error' : '']">
                <div class="col-sm-12 form-group form-input-group">
                  <label class="control-label">解除限购原因:</label>
                  <div class="col-sm-4">
                    <textarea class="form-control col-sm-4"
                              v-model="secureData.f_cancel_reason"
                              placeholder="解除限购原因">
                    </textarea>
                  </div>
                </div>
              </div>
            </div>
          </article>
          <footer slot="modal-footer" class="modal-footer">
            <div class="modal-footer">
              <button type="button" class="btn btn-default" :disabled="!secureData.f_cancel_reason"
                      @click='secureQuota'>确定
              </button>
              <button type="button" class="btn btn-default" @click='secureQuotaModalClose'>取消</button>
            </div>
          </footer>
        </modal>
      </div>
    </div>
  </div>
</template>
<script>
import {HttpResetClass, PagedList} from 'vue-client'
import exportConfig from './config/exportConfig'

export default {
  title: '限购记录',
  data () {
    return {
      condition: '1=1',
      model: new PagedList('api/af-revenue/sql/manage_quotaRecordQuery', 20, {
        startDate:'this.model.startDate',
        endDate:'this.model.endDate'
      }),
      orgConditionStr: '',
      criteriaShow: false,
      allQuotaRecordModalShow: false,
      initres: {
        org: [this.$login.f.orgid]
      },
      modalModel: new PagedList('api/af-revenue/sql/manage_singleTable', 100, {
        items: '"lg.*,ui.f_userinfo_code,ui.f_user_name"',
        tablename: '"t_limit_gas lg left join t_userinfo ui on lg.f_userinfo_id = ui.f_userinfo_id"',
        orderitem: '"lg.f_operate_date desc"'
      }),
      map: new Map(),
      secureData: {
        f_cancel_reason: ''
      },
      selectDataLen: 0,
      isBatch: '0',
      other: [],
      footer: [],
      sumsmodel: {},
      batchRowList: [],
      selectRowLen: [],
      f_cancel_people: [],
      toDetailObj: null,
      defaultfield: [],
      modalCondition: '1=1',
      secureQuotaModalShow: false,
      secureQuotaObj: null,
      issecures: [{label: '全部', value: ''}, {label: '是', value: '是'}, {label: '否', value: '否'}]
    }
  },
  ready () {
    this.$refs.paged.$refs.cri.model.startDate = this.$login.toStandardDateString()
    this.$refs.paged.$refs.cri.model.endDate = this.$login.toStandardDateString()
  },
  methods: {
    getotherfooter () {
      this.other = [];
      this.footer = [];
      let exportdata = this.getCondition;
      let otherInData = [];
      otherInData.push(`导出时间: ${this.$login.toStandardTimeString()}`);
      let footerData = [], exportfield = this.getExportField;
      footerData.push("合计");
      let self = this;
      for (var field in self.sumsmodel) {
        footerData.push(`${exportfield[field]}合计:${self.sumsmodel[field]}`);
      }
      this.footer.push(footerData);
      this.other.push(otherInData);
    },
    async batchOper () {
      this.isBatch = '3'
      if (this.$refs.paged.$refs.grid.getRowData().length > 0) {
        // 获取要批量删除的小区数据
        this.batchRowList = this.$refs.paged.$refs.grid.getRowData()
        this.selectRowLen = this.batchRowList.length
        this.secureQuotaModalShow = true
      } else {
        this.$showAlert('请至少选择一项！', 'warning', 2000)
      }
    },
    async batchSecureQuota () {
      this.isBatch = '1'
      if (this.$refs.modalgrid.getRowData().length != 0) {
        // 获取要批量删除的小区数据
        this.getRowData = this.$refs.modalgrid.getRowData()
        this.selectDataLen = this.getRowData.length
        console.log(this.selectDataLen + '-----------------------')
        this.secureQuotaModalShow = true
      } else {
        this.$showAlert('请至少选择一项！', 'warning', 2000)
      }
    },
    secureQuotaShow (row) {
      this.isBatch = '2'
      this.secureQuotaObj = row
      this.secureQuotaModalShow = true
    },
    allQuotaRecordModalClose () {
      this.toDetailObj = null
      this.allQuotaRecordModalShow = false
      this.$refs.modalgrid.selectInit()
    },
    secureQuotaModalClose () {
      this.secureQuotaObj = null
      this.secureQuotaModalShow = false
    },
    hidden () {
      this.criteriaShow = !this.criteriaShow
    },
    secureQuota () {
      let _this = this
      if (_this.isBatch === '1') {
        let msg = {
          resolveMsg: '批量作废限购成功！',
          rejectMsg: '批量作废限购失败！'
        }
        _this.$showMessage(`您确定要作废你选中的${_this.selectDataLen}条限购吗?`, ['confirm', 'cancel']).then(async (res) => {
          if (res === 'confirm') {
            console.log('所选中的限购信息:=======' + JSON.stringify(_this.getRowData))
            let data = {
              selectRow: _this.getRowData,
              f_cancel_people: _this.$login.f.name,
              f_cancel_peopleid: _this.$login.f.id,
              f_cancel_reason: _this.secureData.f_cancel_reason,
              f_operate_type: '1'
            }
            _this.secureQuotaModalShow = false
            await _this.$resetpost('api/af-revenue/logic/sale_operateQuotaRecord', {data: data}, msg).then(res => {
              if (res) {
                _this.$refs.paged.$refs.grid.selectInit()
                _this.modalSearch()
              }
            })

          }
        })
      } else if (_this.isBatch === '2') {
        _this.$showMessage('您确定要作废此条限购吗?', ['confirm', 'cancel']).then(async (res) => {
          if (res === 'confirm') {
            let data = {
              id: _this.secureQuotaObj.id,
              f_cancel_people: _this.$login.f.name,
              f_cancel_peopleid: _this.$login.f.id,
              f_cancel_reason: _this.secureData.f_cancel_reason,
              f_operate_type: '1'
            }
            _this.secureQuotaModalShow = false
            await _this.$resetpost('api/af-revenue/logic/sale_operateQuotaRecord', {data: data}, {
              resolveMsg: '作废限购成功！！！',
              rejectMsg: '作废限购失败！！！'
            }).then(res => {
              if (res) {
                _this.modalSearch()
              }
            })
          }
        })
      } else if (_this.isBatch === '3') {
        let msg = {
          resolveMsg: '批量作废限购成功！',
          rejectMsg: '批量作废限购失败！'
        }
        _this.$showMessage(`您确定要作废你选中的${_this.selectRowLen}条限购吗?`, ['confirm', 'cancel']).then(async (res) => {
          if (res === 'confirm') {
            console.log('所选中的限购信息:=======' + JSON.stringify(_this.batchRowList))
            let data = {
              selectRow: _this.batchRowList,
              f_cancel_people: _this.$login.f.name,
              f_cancel_peopleid: _this.$login.f.id,
              f_cancel_reason: _this.secureData.f_cancel_reason,
              f_operate_type: '1'
            }
            _this.secureQuotaModalShow = false
            await _this.$resetpost('api/af-revenue/logic/sale_operateQuotaRecord', {data: data}, msg).then(res => {
              if (res) {
                _this.$refs.paged.$refs.grid.selectInit()
                _this.search()
              }
            })
          }
        })
      }
    },
    async toDetail (row) {
      this.toDetailObj = row
      await this.modalSearch()
      this.allQuotaRecordModalShow = true
    },
    clear () {
      // 清空部门和人员
      Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
        this.$refs.paged.$refs.cri.model[key] = []
      })
    },
    modalSearch () {
      this.modalCondition = `lg.f_state = '有效' and lg.f_userinfo_id = '${this.toDetailObj.f_userinfo_id}'`
      this.modalModel.search(this.modalCondition, this.modalModel)
    },
    search () {
      this.$refs.paged.$refs.cri.search()
    },
    getRes (obj) {
      this.orgConditionStr = obj
    },
    selfSearch (args) {
      if (!this.orgConditionStr) {
        args.condition = `${args.condition}` + `and f_orgid = '${this.$login.f.orgid}'`
      } else {
        args.condition = `${args.condition}` + this.orgConditionStr
      }
      this.condition = args.condition
      this.model.search(args.condition, args.model)
    }
  },
  computed: {
    getCondition() {
      return {
        condition: this.condition
      }
    },
    getfield() {
      return exportConfig.quotaQueryConfig
    },
  },
  watch: {
    'secureQuotaModalShow' (val) {
      if (!val) {
        this.secureData.f_cancel_reason = ''
      }
    },
    'allQuotaRecordModalShow' (val) {
      if (!val) {
        this.search()
      }
    }
  }
}
</script>

