<template>
  <div style="width: 100%; height: 100%">
    <template-page
      v-if="showRecord"
      ref="pageRef"
      :operation="operation()"
      :optionsEvensGroup="optionsEvensGroup"
      :getList="getList"
      :exportList="exportList"
      :tableAttributes="tableAttributes"
      :tableEvents="tableEvents"
      :columnParsing="columnParsing"
    >
    </template-page>
    <div class="app-container" v-else>
      <CreditListDetail :recordsListId="recordsListId" />
    </div>
    <!-- 新增弹窗 -->
    <el-dialog
      title="经销商信用额度管理"
      :visible.sync="dialogForm"
      width="30%"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-form ref="addForm" :rules="rules" :model="addForm" label-width="120px">
        <el-form-item label="信用变更额度" prop="amount">
          <el-input v-model.number="addForm.amount"></el-input>
        </el-form-item>

        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            class="selectStyle"
            v-model="addForm.startTime"
            type="date"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            class="selectStyle"
            v-model="addForm.endTime"
            type="date"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelFn">取 消</el-button>
        <el-button type="primary" @click="addDataFn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import { getCreditListEdit, getWalletCustomerList, getCustomerList } from '@/api/finance/credit_list'
import { getCreditList, getCreditListExport } from '@/api/finance/credit_list_v2'
import CreditListDetail from './components/credit_list-detail'
import { downloadFiles } from '@/utils/util'
export default {
  components: {
    CreditListDetail,
    TemplatePage
  },
  data() {
    return {
      showRecord: true,
      dialogForm: false, //弹窗表单
      addForm: {
        startTime: '',
        endTime: '',
        amount: null
      },
      data: null,
      recordsListId: {}, //记录
      customerList: null,
      rules: {
        startTime: [{ required: true, message: '请选择开始时间', trigger: 'blur' }],
        endTime: [{ required: true, message: '请选择结束时间', trigger: 'blur' }],
        amount: [
          {
            required: true,
            message: '请输入数字',
            type: 'number',
            trigger: 'blur'
          }
        ],
        customerId: [{ required: true, message: '请选择经销商', trigger: 'blur' }],
        customerWalletId: [{ required: true, message: '请选择钱包', trigger: 'blur' }]
      },
      optionsEvensGroup: [],
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: false
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: []
    }
  },
  created() {
    this.getCustomerData({ pageSize: -1, pageNum: 1 })
  },
  methods: {
    // 列表请求函数
    getList: getCreditList,
    // 列表导出函数
    exportList: getCreditListExport,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    operation() {
      return (h, { row, index, column }) => {
        return (
          <div class="operation-btns">
            {this.$checkBtnRole('edit', this.$route.meta.roles) ? (
              <el-button
                type="text"
                class="textColor"
                onClick={() => {
                  this.editFn(row)
                }}
              >
                设置额度
              </el-button>
            ) : null}
            <el-button
              type="text"
              class="textColor"
              onClick={() => {
                this.recordFn(row.id)
              }}
            >
              记录
            </el-button>
          </div>
        )
      }
    },
    //获取经销商列表
    async getCustomerData(data) {
      const res = await getCustomerList(data)
      console.log(res)
      this.customerList = res.data.records
    },
    //设置额度
    editFn(value) {
      this.data = {
        customerId: value.customerId,
        customerWalletId: value.customerWalletId
      }
      this.dialogForm = true
    },
    //确定
    async addDataFn() {
      await this.$refs.addForm.validate()
      await getCreditListEdit({ ...this.addForm, ...this.data })
      // this.getDataList({
      //   type: 'COMMONLY',
      //   pageSize: this.pageSize,
      //   pageNum: this.currentPage
      // })
      this.$refs.pageRef.refreshList()

      this.$message.success('设置成功')
      this.dialogForm = false
    },
    //取消
    async cancelFn() {
      await this.$refs.addForm.resetFields()
      this.dialogForm = false
    },
    //记录
    recordFn(id) {
      this.recordsListId = id
      this.showRecord = false
    }
  }
}
</script>

<style lang="scss" scoped>
.selectStyle {
  width: 100%;
}
</style>
