<template>
  <div class="Billing">
    <div class="header">
      <el-form :inline="true">
        <el-button type="primary" @click="onEdit()">修改</el-button>
        <el-button type="primary" @click="onSearch()">查询</el-button>
        <el-form-item>
          <el-select v-model="page.parentId">
            <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>
    </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 === 'parentId'">
          <span v-show="data.scope.parentId === 66">短信服务中心</span>
          <span v-show="data.scope.parentId === 96">市场部</span>
          <span v-show="data.scope.parentId === 99">博物馆</span>
        </div>
        <div v-show="data.item === 'unitCashType'">
          {{ data.scope.unitCashType === 1 ? '预付费' : '后付费' }}
        </div>
        <div v-show="data.item === 'balance'">0</div>
        <div v-show="data.item === 'cashtype'">
          {{ data.scope.cashtype === 1 ? '预付费' : '后付费' }}
        </div>
        <div v-show="data.item === 'chargingMethod'">
          {{ data.scope.chargingMethod === 1 ? '成功计费' : '提交计费' }}
        </div>
      </template>
    </Table>
    <el-dialog
      title="修改"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form>
        <el-form-item>
          <el-select v-model="tableItem.parentId" :disabled="true">
            <el-option label="短信服务中心" :value="66" />
            <el-option label="市场部" :value="96" />
            <el-option label="博物馆" :value="99" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="tableItem.unitBalance" :disabled="true" />
        </el-form-item>
        <el-form-item>
          <el-select v-model="tableItem.unitCashType">
            <el-option label="预付费" :value="1" />
            <el-option label="后付费" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="tableItem.apiName" :disabled="true" />
        </el-form-item>
        <el-form-item>
          <el-input value="0" :disabled="true" />
        </el-form-item>
        <el-form-item>
          <el-select v-model="tableItem.cashtype">
            <el-option label="预付费" :value="1" />
            <el-option label="后付费" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="tableItem.chargingMethod">
            <el-option label="成功计费" :value="1" />
            <el-option label="提交计费" :value="2" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { custCustApiListAPI, custCustApiUpdateAPI } from '@/api/Account/Billing'
import Table from '@/components/Table/index.vue'

export default {
  name: 'Billing',

  components: {
    Table
  },

  data() {
    return {
      page: {
        limit: 10,
        offset: 0
      },
      total: 0,

      tableData: [],
      tableList: [
        {
          id: 1,
          label: '序号',
          prop: 'uid',
          flag: false
        },
        {
          id: 2,
          label: '单位',
          prop: 'parentId',
          flag: true
        },
        {
          id: 3,
          label: '余额',
          prop: 'unitBalance',
          flag: false
        },
        {
          id: 4,
          label: '付费模式',
          prop: 'unitCashType',
          flag: true
        },
        {
          id: 5,
          label: '短信账号',
          prop: 'apiName',
          flag: false
        },
        {
          id: 6,
          label: '余额',
          prop: 'balance',
          flag: true
        },
        {
          id: 7,
          label: '付费模式',
          prop: 'cashtype',
          flag: true
        },
        {
          id: 8,
          label: '计费方式',
          prop: 'chargingMethod',
          flag: true
        },
        {
          id: 9,
          label: '更新时间',
          prop: 'createtime',
          flag: false
        }
      ],
      tableItemList: [],
      tableItem: {},

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

  created() {
    this.getCustCustApiList()
    this.flag = true
  },

  methods: {
    async getCustCustApiList() {
      this.loading = true
      const res = await custCustApiListAPI(this.page)
      this.tableData = res.rows.map((item, index) => {
        return { ...item, uid: index + 1 }
      })
      this.total = res.total
      this.loading = false
    },
    onSearch() {
      this.getCustCustApiList()
    },
    onSelectionChange(value) {
      this.tableItemList = value
    },
    onEdit() {
      if (this.tableItemList.length < 1 || this.tableItemList.length > 1) {
        return this.$message({
          message: '只能选中一项哦',
          type: 'warning'
        })
      }
      this.tableItem = {
        id: this.tableItemList[0].id,
        unitName: this.tableItemList[0].unitName,
        apiName: this.tableItemList[0].apiName,
        chargingMethod: this.tableItemList[0].chargingMethod,
        cashtype: this.tableItemList[0].cashtype,
        unitCashType: this.tableItemList[0].unitCashType,
        unitBalance: this.tableItemList[0].unitBalance,
        parentId: this.tableItemList[0].parentId,
        userId: this.tableItemList[0].userId
      }
      this.dialogVisible = true
    },
    handleClose() {
      this.dialogVisible = false
    },
    async onSubmit() {
      await custCustApiUpdateAPI(this.tableItem)
      this.getCustCustApiList()
      this.dialogVisible = false
    },
    onCurrentChange(value) {
      this.page.offset = value
      this.getCustCustApiList()
    },
    onSizeChange(value) {
      this.page.limit = value
      this.getCustCustApiList()
    }
  }
}
</script>
