<template>
  <div class="flex-auto" style="overflow-y: scroll;height: 59%">
    <validator name='v'>
      <form novalidate class="form-horizontal select-overspread ">
        <ul class="nav nav-tabs">
          <li class="active a-tabs"><a href="#">重点用户管理</a></li>
        </ul>
        <div>
          <div class="row">
            <div class="col-sm-4 form-group" id="f_user_id">
              <label for="f_userinfo_code" class="font_normal_body">表&emsp;&emsp;号</label>
              <input class="input_search" style="width:60%" type="text"
                     v-model="model.f_meternumber" :readonly="true"
                     placeholder="输入表号">
            </div>
            <div class="col-sm-4 form-group" id="f_user_id">
              <label for="f_userinfo_code" class="font_normal_body">卡&emsp;&emsp;号</label>
              <input class="input_search" style="width:60%" type="text"
                     v-model="model.f_card_id" :readonly="true"
                     placeholder="输入卡号">
            </div>
            <div class="col-sm-4 form-group" id="f_user_id" :class="[$v.f_userinfo_code.required ? 'has-error' : 'has-success']">
              <label for="f_userinfo_code" class="font_normal_body">用户编号</label>
              <input class="input_search" style="width:60%" type="text" class="input_search" style="width:60%"
                     v-model="model.f_userinfo_code" :readonly="true"
                     v-validate:f_userinfo_code='{required: true }' placeholder="输入编号">
            </div>
            <div class="col-sm-4 form-group" id="name"  :class="[$v.f_user_name.required ? 'has-error' : 'has-success']">
              <label for="f_user_name" class=" font_normal_body">用户姓名</label>
              <input class="input_search" style="width:60%" type="text" class="input_search" style="width:60%"   v-validate:f_user_name='{required: true }'
                     v-model="model.f_user_name" readonly
                     placeholder="用户姓名">

            </div>
            <div class="col-sm-4 form-group" id="phone" :class="[$v.f_user_phone.required ? 'has-error' : 'has-success']">
              <label for="f_user_phone" class=" font_normal_body">联系方式</label>
              <input class="input_search" style="width:60%" type="text" class="input_search" style="width:60%"  v-validate:f_user_phone='{required: true }'
                     v-model="model.f_user_phone" readonly
                     placeholder="联系方式">

            </div>
            <div class="col-sm-4 form-group" id="f_limit_times" :class="[$v.f_limit_times.required ? 'has-error' : 'has-success']">
              <!--              <label for="f_limit_times" class=" font_normal_body">限购次数</label>-->
              <label class="font_normal_body">限购次数</label>
              <input class="input_search" style="width:60%" type="number"
                     v-model="model.f_limit_times"   v-validate:f_limit_times='{required: true }'
                     placeholder="限购次数">

            </div>
            <div class="col-sm-2 form-group" id="f_limit">
              <!--              <label for="f_limit_times" class=" font_normal_body">限购次数</label>-->
              <v-select
                v-model="f_limit"
                :value.sync="f_limit"
                :options='limites'
                width="100%"
                close-on-select>
              </v-select>
            </div>
            <div class="col-sm-3 form-group" id="f_limit_gas" style="right: 3%;">
              <input class="input_search" style="width:60%" type="number" v-if="f_limit=='限购气量'"
                     v-model="model.f_limit_value"
                     v-scale="[model.f_limit_value, 2]"
                     @click="gg()"
                     placeholder="限购气量">
              <input class="input_search" type="number" v-if="f_limit=='限购金额'"
                     v-model="model.f_limit_amount"
                     v-scale="[model.f_limit_amount, 2]"
                     @click="gg()"
                     placeholder="限购金额">

            </div>
          </div>
          <div class="row">
            <div class="col-sm-3 form-group" :class="[$v.f_opr_state.required ? 'has-error' : '']">
              <label class="font_normal_body col-sm-4">操作状态</label>
              <input type="text" v-show="false" v-model="model.f_opr_state" v-validate:f_opr_state='{required: true }'>
              <v-select width="60%"
                        v-model="model.f_opr_state"
                        :value.sync="model.f_opr_state"
                        :options="fstate"
                        :value-single="true"
                        close-on-select></v-select>
            </div>
          <div class="row">
            <div class="col-sm-3 form-group" :class="[$v.f_level.required ? 'has-error' : '']">
              <label class="font_normal_body col-sm-4">管控级别</label>
              <input type="text" v-show="false" v-model="model.f_level" v-validate:f_level='{required: true }'>
              <v-select width="60%"
                        v-model="model.f_level"
                        :value.sync="model.f_level"
                        :options="flevel"
                        :value-single="true"
                        close-on-select></v-select>
            </div>
            <div class="col-sm-3 form-group" :class="[$v.f_function.required ? 'has-error' : '']">
              <label class="font_normal_body col-sm-4">限制功能</label>
              <input type="text" v-show="false"  v-model="$refs.fun.selectedItems" v-validate:f_function='{required: true }'>
              <v-select
                width="60%"
                :value.sync="model.f_function"
                v-model="model.f_function"
                :options="fFunction"
                :multiple="true"
                v-ref:fun
                :close-on-select="false"
              >
              </v-select>

            </div>
          </div>
          <div class="row">
            <div class="col-sm-12 form-group" id="address">
              <label for="f_address" class=" font_normal_body">地&emsp;&emsp;址</label>
              <input class="input_search" type="text" class="input_search" style="width:87%" v-model="model.f_address"
                     readonly
                     placeholder="地址">
            </div>
            <div class="col-sm-12 form-group" id="f_comments" :class="[$v.f_comments.required ? 'has-error' : 'has-success']">
              <label for="f_comments" class=" font_normal_body">移入原因</label>
              <input  type="text"   v-model="model.f_comments" v-show="false"
                     v-validate:f_comments='{required: true }' >
              <v-select
                width="60%"
                :value.sync="model.f_comments"
                v-model="model.f_comments"
                :options="fcomments"
                :value-single="true"
                :disabled="model.f_black_state == '移入拉黑审核' || model.f_black_state == '已拉黑' ||  model.f_black_state == '移出拉黑审核'"
                :close-on-select="false"
              >
              </v-select>
            </div>
            <div class="col-sm-12 form-group" id="f_comments" :class="[$v.f_yc_comments.required ? 'has-error' : 'has-success']" title="参数名称：【移出重点用户原因】"
                 v-if=" model.f_black_state == '已拉黑' ||  model.f_black_state == '移出拉黑审核'">
              <label for="f_comments" class=" font_normal_body">移出原因</label>
              <input  type="text"   v-model="model.f_yc_comments" v-show="false"
                     v-validate:f_yc_comments='{required: true }' >
              <v-select
                width="60%"
                :value.sync="model.f_yc_comments"
                v-model="model.f_yc_comments"
                :options="yccomments"
                :value-single="true"
                :disabled="model.f_black_state == '移出拉黑审核'"
                :close-on-select="false"
              >
              </v-select>
            </div>
            <div class="col-sm-12 form-group" id="f_remark">
              <label for="f_remark" class=" font_normal_body">备&emsp;&emsp;注</label>
              <input class="input_search" type="text" class="input_search" style="width:87%" v-model="model.f_remark"
                     placeholder="备注" >
            </div>
            <div class="col-sm-12 form-group" id="f_audit_comments" v-if="model.f_black_state == '移出拉黑审核' || model.f_black_state == '移入拉黑审核'" :class="[$v.f_audit_comments.required ? 'has-error' : 'has-success']">
              <label for="f_audit_comments" class=" font_normal_body">审核意见</label>
              <input class="input_search" type="text" class="input_search" style="width:87%" v-model="model.f_audit_comments"
                     placeholder="审核意见" v-validate:f_audit_comments='{required: true }' >
            </div>

          </div>

          <div style="text-align:right;height:auto;margin-top:6px;">
            <button class="button_search" type="button" @click="confirm('移入重点用户')" v-show="model.f_black_state == '未拉黑'" :disabled='!$v.valid'>
              提交移入重点用户审核
            </button>
            <button class="button_search"  type="button" @click="confirm('移出重点用户')"
                    v-show="model.f_black_state == '已拉黑'" :disabled='!$v.valid'>
              提交移出重点用户审核
            </button>
              <button class="button_search" type="button" @click="audit('通过')"
                      v-show="model.f_black_state == '移出拉黑审核' || model.f_black_state == '移入拉黑审核'" :disabled='!$v.valid'>
              {{model.f_opr_state}}重点用户通过
            </button>
            <button class="button_search"  type="button" @click="audit('不通过')"
                    v-show="model.f_black_state == '移出拉黑审核' || model.f_black_state == '移入拉黑审核'" :disabled='!$v.valid'>
              {{model.f_opr_state}}重点用户不通过
            </button>
            <button class="button_export"  type="button" @click="close()">取消</button>
          </div>
          </div>
        </div>
      </form>
    </validator>
  </div>
</template>
<script>
  import {HttpResetClass} from 'vue-client'
  import Vue from 'vue'
  let initGen = async function (self, val) {
  // 获取低保气价
    let http = new HttpResetClass()
    let res = await http.load('POST', 'rs/sql/sale_getUserAndVersion', {data: {condition: `f_userinfo_code='${self.data.f_userinfo_code}'`, f_orgid: self.$login.f.orgid}}, {
      resolveMsg: null,
      rejectMsg: null
    })
    console.log(res.data)
    console.log('用户id:' + res.data[0].f_userinfo_id)
    self.model = Object.assign({}, self.model, val)
    self.model.f_userinfo_code = res.data[0].f_userinfo_code
    self.model.f_function = self.model.f_function ? self.model.f_function : []
    if (self.model.f_function && typeof (self.model.f_function) === 'string') {
      self.model.f_function = JSON.parse(self.model.f_function)
    }
    self.model.f_meternumber = res.data[0].f_meternumber
    // self.model.f_final_opr_state=res.data[0].f_final_opr_state
    self.model.f_card_id = res.data[0].f_card_id
    self.model.f_userinfo_id = res.data[0].f_userinfo_id
    self.model.f_user_phone = res.data[0].f_user_phone
    self.model.f_address = res.data[0].f_address
    self.model.f_user_phone = res.data[0].f_user_phone
    self.model.version = res.data[0].version
    self.model.f_limit_value = res.data[0].f_limit_value
    self.model.f_limit_times = res.data[0].f_limit_times
    self.model.f_limit_amount = res.data[0].f_limit_amount
    self.model.f_black_state = res.data[0].f_black_state
    if (res.data[0].f_black_state == '移出拉黑审核' || res.data[0].f_black_state == '已拉黑') {
      self.model.id = res.data[0].id2
      self.model.f_record_id = res.data[0].f_record_id2
      self.model.f_opr_state = '移出'
    } else {
      self.model.id = res.data[0].id1
      self.model.f_record_id = res.data[0].f_record_id1
      self.model.f_opr_state = '移入'
    }
    self.model.f_remark = ''
    if (res.data[0].f_black_state == '移出拉黑审核') {
      self.model.f_yc_comments = res.data[0].f_yc_comments
      self.model.f_remark = res.data[0].f_yc_remark
    }
    if (res.data[0].f_black_state == '移出拉黑审核' || res.data[0].f_black_state == '移入拉黑审核' || res.data[0].f_black_state == '已拉黑') {
      self.model.f_comments = res.data[0].f_black_comments
      self.model.f_remark = res.data[0].f_black_remark
    }
  }

  export default {
    title: '重点用户',
    props: {
      f_islow_income: {
        type: String,
        default: '1'
      },
      data: {
        type: Object
      }
    },
    data () {
      return {
        valve: false,
        pricename: [{value: '', label: ''}],
        model: {
          noLimitGas: '',
          f_opr_state: '移入',
          f_final_opr_state: '移出',
          f_function: ['限制购气'],
          f_level: '高',
          f_userinfo_code: '',
          f_userinfo_id: '',
          f_user_name: '',
          f_address: '',
          f_user_phone: '',
          f_comments: '',
          f_audit_comments: '',
          f_remark: '',
          version: null,
          // 限购次数
          f_limit_times: '',
          // 限购气量
          f_limit_value: '',
          // 限购金额
          f_limit_amount: '',
          f_black_state: '',
          f_yc_comments: ''
        },
        // 编辑控制
        isBlackList: false,
        f_filialeid: '',  // 所属公司
        f_filialename: '',  // 所属公司名称
        f_limit: '限购气量',
        limites: [{label: '限购气量', value: '限购气量'}, {label: '限购金额', value: '限购金额'}],
        fstate: [{label: '移入', value: '移入'}, {label: '移出', value: '移出'}],
        flevel: [{label: '高', value: '高'}, {label: '中', value: '中'}, {label: '低', value: '低'}],
        fFunction: this.$appdata.getParam('限制功能') ? [...this.$appdata.getParam('限制功能')] : [],
        nolimitGas: [{label: '是', value: '0'}, {label: '否', value: '1'}],
        fcomments: this.$appdata.getParam('移入原因') ? [...this.$appdata.getParam('移入原因')] : [],
        yccomments: this.$appdata.getParam('移出重点用户原因') ? [...this.$appdata.getParam('移出重点用户原因')] : []
      }
    },
    ready () {
      initGen(this, this.data)
    },
    watch: {
      'data' (val) {
        initGen(this, val)
      }
    },
    methods: {
      gg () {
        if (this.f_limit[0] === '限购气量') {
          this.model.f_limit_amount = ''
        } else {
          this.model.f_limit_value = ''
        }
      },
      getinfo (type) {
        var condition = `${type} = '${this.model[type]}'`
        this.$resetpost('rs/sql/sale_getUserAndVersion', {data: {condition: condition, f_orgid: this.$login.f.orgid}}, {
          resolveMsg: null,
          rejectMsg: null
        }).then((res) => {
          console.log(res.data[0])
          this.model = res.data[0]
          this.$parent.blodid = this.model.f_userinfo_id
          this.model.f_function = this.model.f_function ? JSON.parse(this.model.f_function) : []
        })
      },
      async confirm (type) {
        if (type == '移出重点用户' && !this.$login.r.find(value => value == '取消拉黑')) {
          this.$showMessage('你没有移出重点用户的权限,请联系管理员!')
          return
        }
        if (type == '移入重点用户' && !this.$login.r.find(value => value == '新增黑名单')) {
          this.$showMessage('你没有移入重点用户的权限,请联系管理员!')
          return
        }
        let data = {
          f_operat_type: type,
          f_state: '有效',
          f_user_id: this.model.f_user_id,
          f_user_name: this.model.f_user_name,
          f_comments: type == '移入重点用户' ? this.model.f_comments : this.model.f_yc_comments,
          f_remark: this.model.f_remark,
          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,
          f_operate_date: this.$login.toStandardTimeString(),
          f_describe: `${this.$login.f.name}对客户${this.model.f_user_name}进行${type}`,
          f_userinfo_id: {
            f_userinfo_id: this.model.f_userinfo_id,
            f_user_state: '正常',
            version: this.model.version
          },
          f_limit_times: this.model.f_limit_times,
          f_limit_amount: this.model.f_limit_amount,
          f_limit_value: this.model.f_limit_value,
          f_opr_state: this.model.f_opr_state,
          f_level: this.model.f_level,
          f_function: this.model.f_function
        }
        console.log('查看参数data', data)
        let res = await this.$resetpost('rs/logic/backlistOperate', data, {resolveMsg: null, rejectMsg: null})
        let operateType = type == '移入重点用户' ? '移入重点用户审核提交' : '移出重点用户审核提交'
        if (res.data.code == 200) {
          this.$showMessage(`${operateType}成功`)
        }
        if (res.data.code == 500) {
          this.$showMessage(`${operateType}失败,用户已移入重点用户,请先移出重点用户`)
          return
        }
        if (res.data.code == 501) {
          this.$showMessage(`${operateType}失败,用户不是重点用户,请先移入重点用户`)
          return
        }
        if (res.data.code === 503) {
          this.$showMessage(`${operateType}失败,该用户已提交移出重点用户审核，请联系管理员处理`)
          return
        }
        this.initModel()
        this.$dispatch('success')
      },
      async audit (type) {
        // if (!this.$login.r.find(value => value == '黑名单审核')) {
        //   this.$showMessage('你没有审核重点用户的权限,请联系管理员!')
        //   return
        // }
        let data = {
          f_audit_comments: this.model.f_audit_comments,
          f_audit_operator: this.$login.f.name,
          f_audit_operatorid: this.$login.f.id,
          id: this.model.id,
          f_userinfo_id: this.model.f_userinfo_id,
          f_record_id: this.model.f_record_id,
          f_audit_date: this.$login.toStandardTimeString(),
          type: type,
          f_operat_type: this.model.f_opr_state + '重点用户'
        }
        console.log('查看参数data', data)
        await this.$resetpost('rs/logic/blacklistAudit', data, {resolveMsg: '审核成功', rejectMsg: '审核失败'})
        this.initModel()
        this.$dispatch('success')
      },
      getorg (id, name) {
        this.f_filialeid = id[0]
        this.f_filialename = name[0]
      },
      initModel () {
        this.model = {
          f_userinfo_code: '',
          f_userinfo_id: '',
          f_user_name: '',
          f_address: '',
          f_function: [],
          f_remark: '',
          f_user_phone: '',
          f_comments: '',
          version: null,
          isblacklist: 'false'
        }
      },
      close () {
        this.$dispatch('close')
      }
    },
    computed: {
      // parsedFFunction: {
      //   get() {
      //     if (this.model.f_function) {
      //       try {
      //         return JSON.parse(this.model.f_function)
      //       } catch (error) {
      //         console.error('Failed to parse f_function:', error)
      //         return []
      //       }
      //     }
      //     return []
      //   },
      //   set(value) {
      //     this.model.f_function = JSON.stringify(value)
      //   }
      // }
    },
    mounted () {
      // if (this.model.f_opr_state === '移入') {
      //   this.model.f_opr_state = '移出';
      // }
    }
  }
</script>
<style lang="less">
  #gas-price-form {
    .form-group {
      margin-left: 0px;
      margin-right: 0px;
    }
  }

</style>
