<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_name">
              <el-input
                v-model="ruleForm['channel_name']"
                :disabled="this.actionType === actionTypes.PREVIEW"
                placeholder="名称"
                maxlength="20"
              />
            </el-form-item>

            <el-form-item label="业务公司" prop="companyNo">
              <search-work-company
                :value="ruleForm.companyNo"
                :disabled="this.actionType === actionTypes.PREVIEW"
                :placeholder="'业务公司'"
                @searchChange="companyNosearchChange"
              />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <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>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>

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

export default {
  name: 'Sort',
  mixins: [resToken, companyHelper],
  props: ['showModel', 'id', 'sitem', 'actionType'],
  data() {
    return {
      actionTypes,
      loading: false,
      showModelThis: this.showModel,
      pid_name: '',
      ruleForm: {
        companyNo: '',
        channel_name: ''
      },
      rulesThis: this.rules,
      rules: {
        companyNo: [{ required: true, trigger: 'blur', message: '请选择业务公司' }],
        channel_name: [{ 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 {
            id,
            companyNo,
            channel_name
          } = this.sitem

          this.ruleForm = {
            id,
            companyNo: companyNo || '',
            channel_name: channel_name || ''
          }
        }
      })
    },
    rate_change(e, key) {
      this.ruleForm[key] = e + ''
      this.$refs.ruleForm.validateField(key)
    },
    async companyNosearchChange(e) {
      const { id, code, label } = e
      this.ruleForm['companyNo'] = code || ''
      this.$refs.ruleForm.validateField('companyNo')
    },
    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>
