<template>
  <div class="auto">
    <validator name="v">
      <form class="form-horizontal" novalidate>
        <div class="row" style="margin-top:15px;">
          <div class="col-sm-3 form-group" :class="[$v.f_opr_state.required ? 'has-error' : '']">
            <label class="font_normal_body col-sm-3">操作状态</label>
            <input type="text" v-show="false" v-model="KeyModel.f_opr_state" v-validate:f_opr_state='{required: true }'>
            <v-select width="60%"
                      v-model="KeyModel.f_opr_state"
                      :value.sync="KeyModel.f_opr_state"
                      :options="fstate"
                      :value-single="true"
                      close-on-select></v-select>
          </div>
          <div class="col-sm-3 form-group" :class="[$v.f_opr_state.required ? 'has-error' : '']" v-if="false">
            <label class="font_normal_body col-sm-3">操作状态1</label>
            <input type="text" v-show="false" v-model="KeyModel.f_final_opr_state" v-validate:f_opr_state='{required: true }'>
            <v-select width="60%"
                      v-model="KeyModel.f_final_opr_state"
                      :value.sync="KeyModel.f_final_opr_state"
                      :options="fstate"
                      :value-single="true"
                      close-on-select></v-select>
          </div>
          <div class="col-sm-3 form-group" id="f_comments" :class="[$v.f_comments.required ? 'has-error' : 'has-success']" v-if="KeyModel.f_opr_state == '移入'">
            <label for="f_comments" class=" font_normal_body">移入原因</label>
            <input  type="text"   v-model="KeyModel.f_comments" v-show="false"
                    v-validate:f_comments='{required: true }' >
            <v-select
              width="60%"
              :value.sync="KeyModel.f_comments"
              v-model="KeyModel.f_comments"
              :options="fcomments"
              :value-single="true"
              :close-on-select="false"
            >
            </v-select>
          </div>
<!--          <div class="col-sm-3 form-group" v-if="KeyModel.f_opr_state == '移出'">-->
<!--            <label class="font_normal_body col-sm-3">移出原因</label>-->
<!--            <input type="text" v-model="KeyModel.f_comments" class="input_search" placeholder="请输入移入原因" >-->
<!--          </div>-->
          <div v-if="KeyModel.f_opr_state == '移入'">
            <div class="col-sm-3 form-group" :class="[$v.f_level.required ? 'has-error' : '']">
              <label class="font_normal_body col-sm-3">管控级别</label>
              <input type="text" v-show="false" v-model="KeyModel.f_level" v-validate:f_level='{required: true }'>
              <v-select width="60%"
                        v-model="KeyModel.f_level"
                        :value.sync="KeyModel.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-3">限制功能</label>
              <input type="text" v-show="false" v-model="KeyModel.f_function" v-validate:f_function='{required: true }'>
              <v-select width="60%"
                        v-model="KeyModel.f_function"
                        :options="fFunction"
                        :multiple=true
                        :value.sync="KeyModel.f_function"
                        close-on-select></v-select>
            </div>
          </div>
          <div v-if="KeyModel.f_opr_state === '移出'">
            <div class="col-sm-3 form-group">
              <label class="font_normal_body col-sm-3">取消限购</label>
              <input type="text" v-show="false" v-model="KeyModel.noLimitGas" >
              <v-select width="60%"
                        v-model="KeyModel.noLimitGas"
                        :options="nolimitGas"
                        :value-single="true"
                        close-on-select></v-select>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-sm-3 form-group">
            <label class="font_normal_body col-sm-3">备&emsp;&emsp;注</label>
            <input type="text" class="input_search" v-model="KeyModel.f_remark" placeholder="备注" style="vertical-align:middle;width: 70%">
          </div>
        </div>
      </form>
    </validator>
  </div>
  <div class="auto" v-if="KeyModel.f_opr_state === '移入'">
    <div style="color: #0d8ddb; font-size: 25px; margin-top: 15px; margin-bottom: 10px; margin-left: 25px">限购配置</div>
    <validator name='v'>
      <form novalidate class="form-horizontal">
        <div class="row">
          <div class="col-sm-3 form-group" :class="[$v.f_third_pay.required ? 'has-error' : '']">
            <label class="font_normal_body col-sm-3" >第三方缴费</label>
            <input type="text" v-show="false" v-model="$refs.f_third_pay.selectedItems"
                   v-validate:f_third_pay='{required: true }'>
            <v-select width="60%"
                      :value.sync="model.f_third_pay"
                      v-model="model.f_third_pay"
                      :options='thirdPays'
                      :value-single="true" v-ref:f_third_pay
                      close-on-select></v-select>
          </div>
          <div class="col-sm-3 form-group" :class="[$v.f_limit_style.required ? 'has-error' : '']">
            <label class="font_normal_body col-sm-3" >限制类型</label>
            <input type="text" v-show="false" v-model="$refs.f_limit_style.selectedItems"
                   v-validate:f_limit_style='{required: true }'>
            <v-select width="60%"
                      :value.sync="model.f_limit_style"
                      v-model="model.f_limit_style"
                      :options='limitstyle'
                      :value-single="true" v-ref:f_limit_style
                      close-on-select></v-select>
          </div>
          <div class="col-sm-3 form-group" :class="[$v.f_time_type.required ? 'has-error' : '']">
            <label class="font_normal_body col-sm-3" >周期类型</label>
            <input type="text" v-show="false" v-model="$refs.f_time_type.selectedItems"
                   v-validate:f_time_type='{required: true }'>
            <v-select width="60%"
                      :value.sync="model.f_time_type"
                      v-model="model.f_time_type"
                      :options='timeTypes'
                      :value-single="true" v-ref:f_time_type
                      close-on-select></v-select>
          </div>
          <div class="col-sm-3 form-group" :class="[$v.f_time_value.required ? 'has-error' : '']">
            <label class="font_normal_body col-sm-3" >周期时长</label>
            <input type="text" class="input_search" v-model="model.f_time_value" placeholder="周期时长"  v-validate:f_time_value='{required: true }' style="vertical-align:middle;width: 60%">
          </div>
        </div>

        <div class="row" style="margin-top:10px;">
          <div class="col-sm-3 form-group" :class="[$v.f_status.required ? 'has-error' : '']">
            <label class="font_normal_body col-sm-3" >生效状态</label>
            <input type="text" v-show="false" v-model="$refs.f_status.selectedItems"
                   v-validate:f_status='{required: true }'>
            <v-select width="60%"
                      :value.sync="model.f_status"
                      v-model="model.f_status"
                      :options='fstatus'
                      :value-single="true" v-ref:f_status
                      close-on-select></v-select>
          </div>
          <div class="col-sm-3 form-group" :class="[$v.f_limit_times.required ? 'has-error' : '']" v-if="model.f_limit_style !=='按总量'">
            <label class="font_normal_body col-sm-3" >限制次数</label>
            <input type="text" class="input_search" v-model="model.f_limit_times"  style="vertical-align:middle;width: 60%" v-validate:f_limit_times='{required: true }' placeholder="限制次数">
          </div>
          <div class="col-sm-3 form-group" :class="[$v.f_limit_value.required ? 'has-error' : '']" v-if="model.f_limit_style !=='按总量' && model.f_limit_type ==='气量'">
            <label class="font_normal_body col-sm-3" >单次限购</label>
            <input type="text" class="input_search" v-model="model.f_limit_value"  style="vertical-align:middle;width: 60%" v-validate:f_limit_value='{required: true }' placeholder="单次限购">
          </div>
          <div class="col-sm-3 form-group" :class="[$v.f_limit_amount.required ? 'has-error' : '']" v-if="model.f_limit_style !=='按总量' && model.f_limit_type ==='金额'">
            <label class="font_normal_body col-sm-3" >单次限购</label>
            <input type="text" class="input_search" v-model="model.f_limit_amount"  style="vertical-align:middle;width: 60%" v-validate:f_limit_amount='{required: true }' placeholder="单次限购">
          </div>
        </div>

        <div class="row" style="margin-top:10px;" v-if="model.f_limit_style ==='按总量'">
          <div class="col-sm-3 form-group" :class="[$v.f_limit_value.required ? 'has-error' : '']" v-if="model.f_limit_type ==='气量'">
            <label class="font_normal_body col-sm-3" >总限购量</label>
            <input type="text" class="input_search" v-model="model.f_limit_value" style="vertical-align:middle;width: 60%" v-validate:f_limit_value='{required: true }' placeholder="单次限购">
          </div>
          <div class="col-sm-3 form-group" :class="[$v.f_limit_amount.required ? 'has-error' : '']" v-if="model.f_limit_type ==='金额'">
            <label class="font_normal_body col-sm-3" >总限购量</label>
            <input type="text" class="input_search" v-model="model.f_limit_amount"  style="vertical-align:middle;width: 60%" v-validate:f_limit_amount='{required: true }' placeholder="单次限购">
          </div>
        </div>

        <div class="row" style="margin-top:10px;">
          <div class="col-sm-3 form-group" :class="[$v.f_limit_type.required ? 'has-error' : '']">
            <label class="font_normal_body col-sm-3" >支付限制</label>
            <input type="text" v-show="false" v-model="$refs.f_limit_type.selectedItems"
                   v-validate:f_limit_type='{required: true }'>
            <v-select width="60%"
                      :value.sync="model.f_limit_type"
                      v-model="model.f_limit_type"
                      :options='limitTypes'
                      :value-single="true" v-ref:f_limit_type
                      close-on-select></v-select>
          </div>
          <div class="col-sm-3 form-group" :class="[$v.f_parameter_value.required ? 'has-error' : '']">
            <label class="font_normal_body col-sm-3" >执行时间</label>
            <input type="text" v-show="false" v-model="model.f_parameter_value"
                   v-validate:f_parameter_value='{required: true }'>
            <datepicker placeholder="执行时间" style="width:60%"
                        v-model="model.f_parameter_value"
                        :value.sync="model.f_parameter_value" v-ref:f_parameter_value
                        :format="'yyyy-MM-dd'">
            </datepicker>
          </div>
        </div>

      </form>
    </validator>
    <div style="text-align: right; height: 25%">
      <button class="button_search btn-gn" :disabled="!$v.valid" @click="confirm('移入重点用户')">确认</button>
      <button class="button_clear btn-gn" @click="clean()">取消</button>
    </div>
  </div>
  <div style="text-align: right; height: 25%" v-if="KeyModel.f_opr_state === '移出'">
    <button class="button_search btn-gn" :disabled="!$v.valid" @click="confirm('移出重点用户')">确认</button>
    <button class="button_clear btn-gn" @click="cancels">取消</button>
  </div>

</template>
<script>

import {HttpResetClass} from "vue-client";

let getLimitDataGen = async function (self) {
  let param = {
    tablename: 't_limit_gas'
  }
  if (self.f_price_id) {
    param.condition = ` f_price_id = '${self.f_price_id}'  and isnull(f_state,'') != '无效'`
  } else if  (self.f_userinfo_id) {
    param.condition = ` f_userinfo_id = '${self.f_userinfo_id}' and isnull(f_state,'') != '无效'`
  }
  let http = new HttpResetClass()
  let res = await http.load('POST', 'rs/sql/saleSingleTable', {data: param}, {resolveMsg: null, rejectMsg: '获取限购配置失败！！'})
  console.log('获取的限购数据', res)
  if (res.data.length > 0 ) {
    self.model = Object.assign({}, self.model, res.data[0])
    self.model.f_third_pay = self.model.f_third_pay + ''
    self.limitshow = true
  } else {
    self.limitshow = false
  }
  initGen(self)
}

let initGen = async function (self) {
  // 获取低保气价
  let http = new HttpResetClass()
  let res = await http.load('POST', 'rs/sql/sale_getUserAndVersion', {data: {condition: `u.f_userinfo_id='${self.f_userinfo_id}'`, 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.f_user_name = res.data[0].f_user_name
  self.model.version = res.data[0].version
}
export default {
  title: '添加重点用户',
  name: 'insert-key-user',

  data () {
    return {
      KeyModel: {
        f_final_opr_state: '移出',
        f_opr_state:'移入',
        f_comments: '',
        f_function: '限制购气',
        f_level:'',
        f_remark:''
      },
      model: {
        version:'',
        f_user_name:'',
        f_third_pay: '0',
        f_time_type: '按月',
        f_limit_times: '',
        f_time_value: '0',
        f_limit_type: '气量',
        f_limit_style:'',
        f_limit_value: '',
        f_limit_amount: '',
        f_status:'',
        f_parameter_value: this.$login.toStandardDateString()
      },
      thirdPays: [{label: '支持', value: '1'},{label: '不支持', value: '0'}],
      timeTypes: [{label: '按月', value: '按月'},{label: '按日', value: '按日'}],
      limitTypes: [{label: '气量', value: '气量'},{label: '金额', value: '金额'}],
      limitstyle: [{label: '按次数', value: '按次数'},{label: '按总量', value: '按总量'}],
      fstatus: [{label: '有效', value: '有效'},{label: '无效', value: '无效'}],
      fstate: [{label: '移入', value: '移入'},{label: '移出', value: '移出'}],
      flevel: [{label: '高', value: '高'},{label: '中', value: '中'},{label: '低', value: '低'}],
      nolimitGas:[{label: '是', value:'0'},{label: '否', value:'1'}],
      fFunction: [{label: '限制购气', value:'限制购气'},{label: '禁止缴费退款', value:'禁止缴费退款'},{label: '禁止开阀', value:'禁止开阀'},{label: '禁止换表', value:'禁止换表'},{label: '禁止补退气', value:'禁止补退气'},{label: '下发强制关阀', value:'下发强制关阀'}],
      fcomments:this.$appdata.getParam('移入原因') ? [ ...this.$appdata.getParam('移入原因')] : []
    }
      },
  props: ['show', 'f_price_id', 'f_userinfo_id','row','userOperationState'],
  ready () {
    this.getLimitData()
  },
  watch: {
    'f_price_id' () {
      this.getLimitData()
    },
    'f_userinfo_id' () {
      this.getLimitData()
    }
  },
  methods: {
    cancels() {
      this.$emit('cancel')
    },
    getLimitData () {
      console.log('获取限购配置。。', this.f_price_id, this.f_userinfo_id)
      if (this.f_price_id || this.f_userinfo_id) {
        getLimitDataGen(this)
      }
    },
    async confirm (type) {
      console.log('确认移入重点用户', this.$login.r)
      if (this.userOperationState === '移入') {
        this.$showMessage(`${type}失败, 用户已被移入重点用户, 请先移出重点用户`);
        return;
      }
      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: this.KeyModel.f_comments,
        f_remark: this.KeyModel.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_parameter_value:this.model.f_parameter_value,
        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.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.KeyModel.f_opr_state,
        f_final_opr_state: this.KeyModel.f_final_opr_state,
        f_level: this.KeyModel.f_level,
        f_function:this.KeyModel.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')
      this.$emit('cancel')
    },
    clean () {
      this.$info('取消操作')
      this.$dispatch('refresh', this.row)
      this.$emit('cancel')
    },
  },
  computed: {
    isDisabled() {
      return this.userOperationState === '移入'
    }
  }
}

</script>
<style>

</style>
