<template>
  <div class="flex-row">
    <div  class="basic-main">
      <div class="span">
        <criteria-paged :model='model' v-ref:paged>
          <criteria :model='model' partial='criteria' v-ref:criteria @condition-changed='$parent.search'>
            <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
              <div class="row">
                <res-select-group :initres="$parent.$parent.initres"  :show-component="['company']" @re-res="$parent.$parent.getRes" v-ref:sel></res-select-group>
                <div class="col-sm-2 ">
                  <label class="font_normal_body">客户编号</label>
                  <input @keyup.enter="search" style="width:60%" type="text" class="input_search "
                         v-model="model.f_userinfo_code"
                         placeholder='客户编号'
                         condition="f_userinfo_code = '{}'"
                  >
                </div>
                <div class="col-sm-2 ">
                  <label class="font_normal_body">企业名称</label>
                  <input @keyup.enter="search" style="width:60%" type="text" class="input_search"
                         v-model="model.f_user_name"
                         placeholder='企业名称'
                         condition="f_paper_name like '%{}%'"
                  >
                </div>

                <div
                  class="col-sm-2"
                  class="form-group">
                  <label class="font_normal_body">开始日期</label>
                  <datepicker placeholder="开始日期" v-model="model.startDate" :value.sync="model.startDate" style="width:60%"
                              :format="'yyyy-MM-dd 00:00:00'" condition="f_operate_date >= '{}'">
                  </datepicker>
                </div>
                <div
                  class="col-sm-2"
                  class="form-group">
                  <label class="font_normal_body">结束日期</label>
                  <datepicker placeholder="结束日期" v-model="model.endDate" :value.sync="model.endDate" style="width:60%"
                              :format="'yyyy-MM-dd 23:59:59'" condition="f_operate_date <= '{}'">
                  </datepicker>
                </div>
                <div
                  class="col-sm-2"
                  class="form-group">
                  <label class="font_normal_body">审核状态</label>
                  <v-select :options='$parent.$parent.auditstates' :value.sync="model.f_user_state"
                            close-on-select condition="f_audit_state='{}'"
                            :value-single="true"
                            placeholder='审核状态' v-model="model.f_user_state"></v-select>
                </div>
                <div class="span" style="float:right">
                <button class="button_search button_spacing"  @click="search()" v-el:cba>查询</button>
                <button class="button_clear button_spacing"  @click="$parent.$parent.clearmsg">清空</button>
                <div style="float: right"  class="button_spacing" :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"    @click="$parent.$parent.hidden()"></div>
              </div>
              </div>
               <div class="row" v-show="$parent.$parent.criteriaShow">
                </div>

              </div>
          </criteria>
          <data-grid :model="model" partial='list' v-ref:grid class="list_area table_sy">
            <template partial='head'>
              <tr>
                <th v-for="(key,value ) in $parent.$parent.$parent.getfield">
                  {{value}}
                </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"  v-for="(key,value ) in $parent.$parent.$parent.getfield">
                  {{row[key]}}
                </td>
                <td>
                  <button  @click.stop="$parent.$parent.$parent.mapShowdata(row)"
                           class="btn btn-link">查看
                  </button>
                </td>
                <td>
                  <button  @click.stop="$parent.$parent.$parent.mapShowauditdata(row,'审核通过')"
                          class="btn btn-link" v-if="row.f_audit_state === '未审核'">通过
                  </button>
                  <button  @click.stop="$parent.$parent.$parent.mapShowauditdata(row,'未通过')"
                          class="btn btn-link" v-if="row.f_audit_state === '未审核'">驳回
                  </button>
                </td>
              </tr>
            </template>
            <template partial='foot'></template>
          </data-grid>
        </criteria-paged>
      </div>
      <modal :show.sync="mapShow" v-if="mapShow"  large backdrop="false" :width="900">
        <header slot="modal-header" class="modal-header">
        </header>
        <article slot="modal-body" class="modal-body">
          <div class="auto" v-if="imgInfo.length">
            <div class="form-group" v-for="row in imgInfo">
              <label class="col-sm-3 control-label">{{row.key}}</label>
              <div class="col-sm-9">
                <img-self :src="row.value" v-if="row.value"  alt="图片加载失败" :width="100" :height="100"></img-self>
              </div>
            </div>
          </div>
        </article>
        <footer slot="modal-footer" class="modal-footer">
          <button type="button" class="button_clear btn-gn" @click='mapShow = false'>关闭</button>
        </footer>
      </modal>
      <modal :show.sync="mapShowsuggest" v-if="mapShowsuggest"  large backdrop="false" :width="900">
        <header slot="modal-header" class="modal-header">
          <button type="button" class="close" @click="mapShowsuggest = false"><span>&times;</span></button>
        </header>
        <article slot="modal-body" class="modal-body">
          <div class="form-group">
            <div class="col-sm-12" style="margin-top: 10px">
              <p>处理意见：</p>
              <textarea class="form-control" v-model="auditdata.f_suggest" rows="4" :placeholder="处理意见"></textarea>
            </div>
          </div>
        </article>
        <footer slot="modal-footer" class="modal-footer">
          <button type="button" class="button_clear btn-gn" @click="audit(auditdata, audittype)">确定</button>
        </footer>
      </modal>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import * as Util from '../../Util'
import {PagedList, HttpResetClass} from 'vue-client'

  let readyGen = async function (self) {
    await self.$LoadParams.loadParam()
  }
  export default {
    title: '企业信息审核',
    name: 'paperaudit',
    data () {
      return {
        model: new PagedList('rs/sql/paperAuditQuery', 20),
        criteriaShow: false,
        condition: '',
        auditdata: '',
        audittype: '',
        initres: {
          org: [this.$login.f.orgid],
          dep: [],
          user: []
        },
        styles: false,
        mapShow: false,
        mapShowsuggest: false,
        imgInfo: [],
        getfield: {
          'f_orgname': '组织机构',
          'f_userinfo_code': '客户编号',
          'f_paper_name': '企业名称',
          'f_contact_user': '联系人',
          'f_address_phone': '联系方式',
          'f_address_str': '详细地址',
          'f_taxpayer_id': '纳税人识别号',
          'f_paper_type': '票据类型',
          'f_paper_account': '开户行及账号',
          'f_operate_date': '操作日期',
          'f_audit_state': '审核状态'

        },
        obj: {
          userid: this.$login.f.id,
          source: `tool.getFullTree(this.getRights().where(row.getType() == $organization$))`,
          res: this.$login.f.f_fengongsi,
          resids: this.$login.f.orgid
        }
      }
    },
    ready () {
      this.$refs.paged.$refs.criteria.model.startDate = Util.toStandardDateString() + ' 00:00:00'
      this.$refs.paged.$refs.criteria.model.endDate = Util.toStandardDateString() + ' 23:59:59'
      readyGen(this)
    },
    events: {
    },
    methods: {
      mapShowauditdata (row, type) {
        this.auditdata = row
        this.audittype = type
        this.mapShowsuggest = true
      },
      mapShowdata (row) {
        let imgList = JSON.parse(row.f_imglist)
        this.imgInfo = imgList.map(item => {
          return {
            key: '材料' + (imgList.indexOf(item) + 1),
            value: item.imgUrl
          }
        })
        this.mapShow = true
      },
      research () {
        this.$refs.paged.$refs.criteria.search()
      },
      hidden () {
        this.criteriaShow = !this.criteriaShow
      },
      async search (args) {
        args.condition = `${args.condition} ` + this.orgcondition
        await this.model.search(args.condition, args.model)
      },
      clearmsg () {
        this.$refs.paged.$refs.criteria.model = {}
      },
      audit (row, val) {
        let condition = `f_user_name = '${row.f_user_name}' and f_userinfo_id != '${row.f_userinfo_id}'`
        let auditData = {
          id: row.id,
          f_userinfo_id: row.f_userinfo_id,
          f_userinfo_code: row.f_userinfo_code,
          f_paper_name: row.f_paper_name,
          f_contact_user: row.f_contact_user,
          f_address_phone: row.f_address_phone,
          f_address_str: row.f_address_str,
          f_taxpayer_id: row.f_taxpayer_id,
          f_paper_type: row.f_paper_type,
          f_paper_account: row.f_paper_account,
          f_audit_state: val,
          f_suggest: row.f_suggest,
          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.$resetpost('rs/logic/paperAudit', {data: auditData}, {
          resolveMsg: null,
          rejectMsg: '获取数据失败'
        }).then((res) => {
          if (val === '审核通过') {
            let synchronizeData = {
              f_userinfo_id: row.f_userinfo_id,
              f_paper_name: row.f_paper_name,
              f_contact_user: row.f_contact_user,
              f_address_phone: row.f_address_phone,
              f_address_str: row.f_address_str,
              f_taxpayer_id: row.f_taxpayer_id,
              f_paper_type: row.f_paper_type,
              f_paper_account: row.f_paper_account
            }
            this.$resetpost('rs/sql/queryOtherUser', {data: {condition: condition}}, {
              resolveMsg: null,
              rejectMsg: '获取数据失败'
            }).then((res2) => {
              let userinfoIds = this.convertToIn(res2.data)
              this.$showMessage(`是否同步更新${userinfoIds}`, ['confirm', 'cancel']).then((res1) => {
                if (res1 === 'confirm') {
                  synchronizeData.userinfoIds = res2.data
                  this.$resetpost('rs/logic/synchronizeData', {data: synchronizeData}, {
                    rejectMsg: '同步失败',
                    resolveMsg: '同步成功'
                  })
                }
              })
            })
          }
          this.auditdata = ''
          this.audittype = ''
          this.mapShowsuggest = false
          this.research()
        })
      },
      convertToIn (val) {
        let temp = ''
        if (val instanceof Array) {
          if (val.length >= 1) {
            val.forEach((res) => {
              temp += `${res.f_userinfo_code},`
            })
            temp = temp.substr(0, temp.length - 1)
          }
        }
        return temp
      },
      getRes (obj) {
        this.orgcondition = obj
      }
    },
    computed: {
      auditstates () {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('审核状态')]
      }
    },
    watch: {
    }
  }
</script>

<style scoped>
</style>

