<template>
  <div class="SignatureManagement">
    <div class="header">
      <el-form :inline="true">
        <el-button class="btn" type="primary" @click="onAdd()">新增</el-button>
        <el-form-item>
          <el-select v-model="page.userId" placeholder="请选择单位">
            <el-option label="全部单位名称" value="" />
            <el-option label="短信服务中心" value="66" />
            <el-option label="市场部" value="96" />
            <el-option label="博物馆" value="99" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="page.opType" placeholder="全部类型">
            <el-option label="全部类型" value="" />
            <el-option label="充值" value="1" />
            <el-option label="扣减" value="2" />
            <el-option label="回收" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            @change="onChange"
          />
        </el-form-item>
        <el-button type="primary" @click="onSearch()">查询</el-button>
      </el-form>
    </div>
    <Table v-show="flag" :data="tableData" :list="tableList" :total="total" :loading="loading" @sizeChange="onSizeChange" @currentChange="onCurrentChange" @selectionChange="onSelectionChange">
      <template #type="data">
        <div v-show="data.item === 'accountyype'">短信用户</div>
        <div v-show="data.item === 'rechargeNum'">
          {{ data.scope.rechargeNum / 10000 }}
        </div>
        <div v-show="data.item === 'rechargeAfterNum'">
          {{ data.scope.rechargeAfterNum / 10000 }}
        </div>
        <div v-show="data.item === 'rechargeType'">
          {{ data.scope.rechargeType === 1 ? '充值' : '扣减' }}
        </div>
      </template>
    </Table>
    <el-dialog
      title="增加"
      :visible.sync="dialogVisible"
      :before-close="beforeClose"
      @open="onOpen"
      @close="onClose"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单位账号" prop="parentUserId">
          <el-select v-model="form.parentUserId" placeholder="单位账号" @change="onParent">
            <el-option label="短信服务中心" :value="66" />
            <el-option label="市场部" :value="96" />
            <el-option label="博物馆" :value="99" />
          </el-select>
        </el-form-item>
        <el-form-item v-show="form.unitUserBalance" label="账号余额">
          <el-input v-model="form.unitUserBalance" :disabled="true" />
        </el-form-item>
        <el-form-item label="短信账号" prop="parentUserId">
          <el-select v-model="form.userId" placeholder="选择短信账号" @change="onSmsAccount">
            <el-option v-for="item in smsAccount" :key="item.userId" :label="item.account" :value="item.userId" />
          </el-select>
        </el-form-item>
        <el-form-item v-show="form.smsUserBalance" label="短信余额">
          <el-input v-model="form.smsUserBalance" :disabled="true" />
        </el-form-item>
        <el-form-item label="操作类型" prop="rechargeType">
          <el-select v-model="form.rechargeType" placeholder="选择操作类型">
            <el-option label="充值" value="1" />
            <el-option label="扣减" value="2" />
            <el-option label="回收" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="充值金额" prop="rechargeNum">
          <el-input v-model="form.rechargeNum" @change="onChangeInput" />
        </el-form-item>
        <el-form-item label="充值后">
          <el-input v-model="smsUserBalanceOld" :disabled="true" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.rechargeRemark" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { custCustApiGetCustApiByParentUserIdAPI, smsSmsChangeList2API, smsSmsChangeSaveAPI, sysUserGetCompanyList4API } from '@/api/Account/TopUp'
import Table from '@/components/Table/index.vue'

export default {
  name: 'TopUp',

  components: {
    Table
  },

  data() {
    return {
      page: {
        limit: 10,
        offset: 0,
        opType: '',
        beginDate: '',
        endDate: '',
        userId: ''
      },
      total: 0,

      edit: {},
      time: [],

      tableData: [],
      tableList: [
        {
          id: 1,
          label: '序号',
          prop: 'uid',
          flag: false
        },
        {
          id: 2,
          label: '单位名称',
          prop: 'name',
          flag: false
        },
        {
          id: 3,
          label: '账户名称',
          prop: 'apiName',
          flag: false
        },
        {
          id: 4,
          label: '账户类型',
          prop: 'accountyype',
          flag: true
        },
        {
          id: 5,
          label: '充值金额',
          prop: 'rechargeNum',
          flag: true
        },
        {
          id: 6,
          label: '充值后金额',
          prop: 'rechargeAfterNum',
          flag: true
        },
        {
          id: 7,
          label: '操作类型',
          prop: 'rechargeType',
          flag: true
        },
        {
          id: 8,
          label: '充值时间',
          prop: 'rechargeTime',
          flag: false
        },
        {
          id: 9,
          label: '充值说明',
          prop: 'rechargeRemark',
          flag: false
        },
        {
          id: 10,
          label: '操作用户',
          prop: 'createUserName',
          flag: false
        }
      ],
      tableItem: [],
      smsAccount: [],
      companyList: [],

      form: {
        userId: '',
        parentUserId: '',
        rechargeUserType: 2,
        unitUser: '',
        unitUserBalance: '',
        smsUser: '',
        smsUserBalance: '',
        rechargeType: '',
        rechargeNum: '',
        rechargeRemark: ''
      },
      rules: {},
      smsUserBalanceOld: '',

      flag: false,
      loading: false,
      dialogVisible: false
    }
  },

  async created() {
    this.smsSmsChangeList2()
    this.flag = true
  },

  methods: {
    async smsSmsChangeList2() {
      this.loading = true
      const res = await smsSmsChangeList2API(this.page)
      this.tableData = res.rows.map((item, index) => {
        return { ...item, uid: index + 1 }
      })
      this.total = res.total
      this.loading = false
    },
    onSearch() {
      this.smsSmsChangeList2(this.page)
    },
    handleClose() {
      this.dialogVisible = false
    },
    onSelectionChange(value) {
      this.tableItem = value
    },
    onAdd() {
      this.dialogVisible = true
    },
    async onOpen() {
      const res = await sysUserGetCompanyList4API()
      this.companyList = res
    },
    onClose() {
      this.form = {
        rechargeUserType: 2
      }
      this.smsUserBalanceOld = ''
    },
    onCurrentChange(value) {
      this.page.offset = value
      this.smsSmsChangeList2()
    },
    onSizeChange(value) {
      this.page.limit = value
      this.smsSmsChangeList2()
    },
    onChange(value) {
      this.page.beginDate = value[0]
      this.page.endDate = value[1]
    },
    async onParent(value) {
      const res = await custCustApiGetCustApiByParentUserIdAPI({ parentId: value })
      const res2 = this.companyList.filter(item => {
        return item.userId === this.form.parentUserId
      })
      this.smsAccount = res
      this.form.unitUserBalance = res2[0].balance / 10000
      this.form.unitUser = value
    },
    onSmsAccount(value) {
      const res = this.smsAccount.filter(item => {
        return item.userId === value
      })
      this.form.smsUserBalance = res[0].balance / 10000
    },
    onChangeInput(value) {
      this.smsUserBalanceOld = Number(this.form.smsUserBalance) + Number(value)
      if (this.form.rechargeType === 2) this.smsUserBalanceOld = Number(this.form.smsUserBalance) - Number(value)
    },
    beforeClose() {
      this.dialogVisible = false
    },
    async onSubmit() {
      this.form.rechargeNum = Number(this.form.rechargeNum) * 10000
      this.form.smsUserBalance = Number(this.form.smsUserBalance) * 10000
      this.form.unitUserBalance = Number(this.form.unitUserBalance) * 10000
      await smsSmsChangeSaveAPI(this.form)
      this.smsSmsChangeList2()
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-select {
  width: 100%;
}
</style>
