<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="12vh"
    width="600px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="showModelThis = false"
  >
    <el-card style="margin: -20px 0 0 0">
      <el-row :gutter="0">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :size="'small'"
            :rules="rulesThis"
            label-width="110px"
            class="demo-ruleForm"
            :disabled="isDetail"
          >
            <el-form-item label="支付渠道" prop="channel_id">
              <el-input
                v-model="ruleForm['channel_name']"
                disabled
                placeholder="名称"
                maxlength="20"
              />
            </el-form-item>

            <el-form-item label="一级分类" prop="cat_id">
              <el-input
                v-model="ruleForm['cat_name']"
                disabled
                placeholder="一类分类ID"
                maxlength="20"
              />
            </el-form-item>
            <el-form-item label="支付渠道毛利率" prop="rate" label-width="125px">
              <digital-input
                :values="ruleForm['rate']"
                :placeholder="'支付渠道毛利率'"
                :min="0"
                :disabled="this.actionType === actionTypes.PREVIEW"
                :max="100"
                :position="'right'"
                :precision="2"
                :size="'mini'"
                :controls="false"
                append="%"
                @reschange="number_change($event, 'rate')"
              />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right;">
          <div class="fr">
            <el-button
              v-if="id !== '007' && !isDetail"
              type="primary"
              :size="'small'"
              @click="submitForm"
            >保 存
            </el-button>
            <el-button :size="'small'" @click="showModelThis = false">{{
              id == "007" ? "关 闭" : "取 消"
            }}</el-button>
          </div>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>

<script>
import asyncRequest from '@/apis/service/serviceParam/paymentChannelGrossProfit'
import companyHelper from '@/mixins/companyHelper'
import resToken from '@/mixins/resToken'
import { actionTypes } from '@/utils/actionType'

export default {
  name: 'Sort',
  mixins: [resToken, companyHelper],
  props: ['showModel', 'id', 'sitem', 'actionType', 'paymentChannel'],
  data() {
    return {
      actionTypes,
      loading: false,
      showModelThis: this.showModel,
      pid_name: '',
      ruleForm: {
        cat_id: '',
        cat_name: '',
        channel_name: '',
        channel_id: ''
      },
      rulesThis: this.rules,
      rules: {
        cat_id: [{ required: true, trigger: 'blur', message: '一级分离不能为空' }],
        channel_id: [{ required: true, trigger: 'blur', message: '支付渠道不能为空' }],
        rate: [{ required: true, trigger: 'blur', message: '支付渠道毛利率不能为空' }]
      }
    }
  },
  computed: {
    title() {
      const base = '支付渠道'
      const prefix = this.actionsType === actionTypes.PREVIEW ? '查看' : this.actionType === actionTypes.UPDATE ? '设置' : '新建'
      return prefix + base
    }
  },
  watch: {
    showModel: function(val) {
      this.showModelThis = val
      if (val) {
        this.initForm()
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit('cancel')
      }
    }
  },
  methods: {
    async initForm() {
      this.loading = true
      this.rulesThis = this.rules
      await this.resetForm()
      this.loading = false
    },

    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields()
          this.$refs.ruleForm.clearValidate()

          const {
            cat_id,
            cat_name,
            rate
          } = this.sitem

          this.ruleForm = {
            cat_id: cat_id || '',
            cat_name: cat_name || '',
            channel_id: this.paymentChannel.id || '',
            channel_name: this.paymentChannel.channel_name || '',
            rate: rate || ''
          }
        }
      })
    },

    async number_change(e, key) {
      this.ruleForm[key] = e + '' || '0'
      this.$refs.ruleForm.validateField(key)
    },
    async submitForm() {
      // if (!this.isHasCurrentCompany()) return
      await this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          if (!this.loading) {
            const model = JSON.parse(JSON.stringify(this.ruleForm))
            const api = this.actionType === this.actionTypes.CREATE ? asyncRequest.create : asyncRequest.update
            const res = await api(model)
            this.loading = false
            if (res && res.code === 0) {
              this.showModelThis = false
              this.$emit('refresh')
            } else if (res && res.code >= 100 && res.code <= 104) {
              await this.logout()
            } else {
              this.$message.warning(res.message)
            }
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.sort {
}
</style>
