<template>
  <el-dialog
    :title="text === 'add' ? '新增' : '审核'"
    :visible.sync="dialog"
    :before-close="beforeClose"
    @close="onClose"
    @open="onOpen"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="单位账号" prop="parentUserId">
        <el-select v-model="parentId" placeholder="单位账号" :disabled="disabledFlag" @change="onChange">
          <el-option v-for="item in parentIdList" :key="item.id" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item v-show="flag" label="账号余额">
        <el-input v-model="form.unitUserBalance" :disabled="true" />
      </el-form-item>
      <el-form-item label="操作类型" prop="rechargeType">
        <el-select v-model="form.rechargeType" placeholder="操作类型" :disabled="disabledFlag">
          <el-option v-for="item in rechargeTypeList" :key="item.id" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="充值金额" prop="rechargeNum">
        <el-input v-model="form.rechargeNum" :disabled="disabledFlag" @change="onChangeInput" />
      </el-form-item>
      <el-form-item label="充值后">
        <el-input v-model="unitUserBalanceOld" :disabled="true" />
      </el-form-item>
      <el-form-item label="记账金额" prop="payNum">
        <el-input v-model="form.payNum" :disabled="disabledFlag" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.rechargeRemark" :disabled="disabledFlag" />
      </el-form-item>
    </el-form>
    <span v-show="text === 'add'" slot="footer" class="dialog-footer">
      <el-button @click="onChangeDialog()">取 消</el-button>
      <el-button type="primary" @click="onSubmit()">确 定</el-button>
    </span>
    <span v-show="text === 'audit'" slot="footer" class="dialog-footer">
      <el-button @click="onSmsSmsChangeExamine(2)">拒 绝</el-button>
      <el-button type="primary" @click="onSmsSmsChangeExamine(1)">通 过</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { custCustApiGetCustApiByParentUserIdAPI, smsSmsChangeExamineAPI, smsSmsChangeSaveAPI } from '@/api/Account/UnitRecharge'

export default {
  // eslint-disable-next-line
  props: ['dialog', 'text', 'table-change'],

  data() {
    return {
      form: {
        parentUserId: '',
        rechargeUserType: 1,
        unitUserBalance: 0,
        rechargeType: '',
        rechargeNum: 0,
        payNum: 0,
        rechargeRemark: ''
      },

      unitUserBalanceOld: '',
      parentId: '',
      rules: {
        parentUserId: [
          { required: true, message: '请输入单位账号', trigger: 'blur' }
        ],
        rechargeType: [
          { required: true, message: '请输入操作类型', trigger: 'blur' }
        ],
        rechargeNum: [
          { required: true, message: '请输入充值金额', trigger: 'blur' }
        ],
        payNum: [
          { required: true, message: '请输入记账金额', trigger: 'blur' }
        ]
      },

      rechargeTypeList: [
        {
          id: 1,
          label: '充值',
          value: 1
        },
        {
          id: 2,
          label: '减扣',
          value: 2
        }
      ],
      parentIdList: [
        {
          id: 1,
          label: '短信服务中心',
          value: 66
        },
        {
          id: 2,
          label: '市场部',
          value: 96
        },
        {
          id: 3,
          label: '博物馆',
          value: 99
        }
      ],

      flag: false,
      tableChangeFlag: false,
      disabledFlag: false
    }
  },

  methods: {
    onClose() {
      this.form = {
        rechargeUserType: 1,
        rechargeNum: 0,
        payNum: 0
      }
      this.flag = false
      this.unitUserBalanceOld = ''
      this.disabledFlag = false
    },
    beforeClose() {
      this.$emit('changeDialog', '')
    },
    onChangeDialog() {
      this.$emit('changeDialog', '')
    },
    onSubmit() {
      this.$refs.form.validate(async flag => {
        if (flag) {
          await smsSmsChangeSaveAPI(this.form)
          this.$emit('sbmit', '')
          this.$emit('changeDialog', '')
        }
      })
    },
    async onChange() {
      console.log(this.parentId)
      this.flag = true
      this.form.parentUserId = this.parentId
      this.form.unitUser = this.parentId
      this.form.unitUserBalance = this.parentId === 66 ? 638732150 : 999989374
      await custCustApiGetCustApiByParentUserIdAPI({ parentId: this.parentId })
    },
    onChangeInput(value) {
      this.unitUserBalanceOld = Number(this.form.unitUserBalance) + Number(value)
      if (this.form.rechargeType === 2) this.unitUserBalanceOld = Number(this.form.unitUserBalance) - Number(value)
    },
    onOpen() {
      if (this.text === 'audit') {
        this.flag = true
        this.disabledFlag = true
        this.form = {
          parentUserId: this.tableChange[0].userId,
          rechargeUserType: 1,
          unitUser: this.tableChange[0].name,
          unitUserBalance: this.tableChange[0].unitUserBalance,
          rechargeType: this.tableChange[0].rechargeType,
          rechargeNum: this.tableChange[0].rechargeNum,
          payNum: this.tableChange[0].payNum,
          rechargeRemark: this.tableChange[0].rechargeRemark,
          id: this.tableChange[0].id,
          rechargeAfterNum: this.tableChange[0].rechargeAfterNum
        }

        this.unitUserBalanceOld = this.form.unitUserBalance
        console.log(this.form)
      }
      if (this.text === 'add') {
        this.parentId = ''
      }
    },
    async onSmsSmsChangeExamine(value) {
      this.form.examineStatus = value
      await smsSmsChangeExamineAPI(this.form)
      this.$emit('changeDialog', '')
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-dialog__footer {
  text-align: center;
}
::v-deep .el-select {
  width: 100%;
}
::v-deep .el-form-item {
  margin-bottom: 30px;
}
</style>
