<template>
  <div class="app-container">
    <div class="search-con">
      <div class="title">查询条件</div>
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="TS合约代码" prop="tsCode">
          <el-input v-model="queryParams.tsCode" placeholder="请输入TS合约代码" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="交易日期" prop="tradeDate">
          <el-input v-model="queryParams.tradeDate" placeholder="请输入交易日期" clearable @keyup.enter="handleQuery" />
        </el-form-item>
      </el-form>
      <div class="search-btn-con">
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button type="info" icon="Refresh" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <div class="main-con">
      <div class="title-con">
        <div class="title">基本信息</div>
        <div class="operate-btn-con">
          <el-button @click="handleAdd" icon="Plus" v-hasPermi="['invest:futuresDailyHistory:add']">新增</el-button>
          <el-button :disabled="multiple" icon="Delete" @click="handleDelete" v-hasPermi="['invest:futuresDailyHistory:remove']">删除</el-button>
          <el-button @click="handleExport" icon="Download" v-hasPermi="['invest:futuresDailyHistory:export']">导出</el-button>
        </div>
      </div>
      <div class="content-con" v-loading="loading">
        <el-table v-loading="loading" :data="futuresDailyHistoryList" @selection-change="handleSelectionChange">
          <!-- <el-table-column type="selection" width="55" align="center" /> -->
          <el-table-column label="TS合约代码" align="center" prop="tsCode" />
          <el-table-column label="交易日期" align="center" prop="tradeDate" />
          <el-table-column label="昨收盘价" align="center" prop="preClose" />
          <el-table-column label="昨结算价" align="center" prop="preSettle" />
          <el-table-column label="开盘价" align="center" prop="open" />
          <el-table-column label="最高价" align="center" prop="high" />
          <el-table-column label="最低价" align="center" prop="low" />
          <el-table-column label="收盘价" align="center" prop="close" />
          <el-table-column label="结算价" align="center" prop="settle" />
          <el-table-column label="收盘价涨跌" align="center" prop="change1" />
          <!-- <el-table-column label="成交量(手)" align="center" prop="vol" />
                  <el-table-column label="成交金额(万元)" align="center" prop="amount" /> -->
          <el-table-column label="结算价涨跌" align="center" prop="change2" />
          <el-table-column label="持仓量(手)" align="center" prop="oi" />
          <el-table-column label="持仓量变化" align="center" prop="oiChg" />
          <!-- <el-table-column label="交割结算价" align="center" prop="delvSettle" /> -->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template v-slot="scope">
              <div class="ctrl-btn d-flex">
                <el-tooltip v-for="item in operateList" :key="item.id" class="item" effect="dark" :content="item.title" placement="top">
                  <el-button :icon="item.icon" :v-hasPermi="item.hasPermi" circle @click="handleOperate(item.id, scope.row)"></el-button>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination small background layout="total, prev, pager, next" :total="total" @current-change="handleCurrentChange" />
      </div>
    </div>
    <!-- 添加或修改期货日行情历史对话框 -->
    <el-dialog :title="title" v-model="open" width="880px" append-to-body>
      <el-form ref="futuresDailyHistoryRef" :model="form" :inline="true" :rules="rules" label-width="120px">
        <el-form-item label="TS合约代码" prop="tsCode">
          <el-input v-model="form.tsCode" placeholder="请输入TS合约代码" />
        </el-form-item>
        <el-form-item label="交易日期" prop="tradeDate">
          <el-input v-model="form.tradeDate" placeholder="请输入交易日期" />
        </el-form-item>
        <el-form-item label="昨收盘价" prop="preClose">
          <el-input v-model="form.preClose" placeholder="请输入昨收盘价" />
        </el-form-item>
        <el-form-item label="昨结算价" prop="preSettle">
          <el-input v-model="form.preSettle" placeholder="请输入昨结算价" />
        </el-form-item>
        <el-form-item label="开盘价" prop="open">
          <el-input v-model="form.open" placeholder="请输入开盘价" />
        </el-form-item>
        <el-form-item label="最高价" prop="high">
          <el-input v-model="form.high" placeholder="请输入最高价" />
        </el-form-item>
        <el-form-item label="最低价" prop="low">
          <el-input v-model="form.low" placeholder="请输入最低价" />
        </el-form-item>
        <el-form-item label="收盘价" prop="close">
          <el-input v-model="form.close" placeholder="请输入收盘价" />
        </el-form-item>
        <el-form-item label="结算价" prop="settle">
          <el-input v-model="form.settle" placeholder="请输入结算价" />
        </el-form-item>
        <el-form-item label="收盘价涨跌" prop="change1">
          <el-input v-model="form.change1" placeholder="请输入收盘价涨跌" />
        </el-form-item>
        <el-form-item label="结算价涨跌" prop="change2">
          <el-input v-model="form.change2" placeholder="请输入结算价涨跌" />
        </el-form-item>
        <el-form-item label="成交量(手)" prop="vol">
          <el-input v-model="form.vol" placeholder="请输入成交量" />
        </el-form-item>
        <el-form-item label="成交金额(万元)" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入成交金额" />
        </el-form-item>
        <el-form-item label="持仓量(手)" prop="oi">
          <el-input v-model="form.oi" placeholder="请输入持仓量" />
        </el-form-item>
        <el-form-item label="持仓量变化" prop="oiChg">
          <el-input v-model="form.oiChg" placeholder="请输入持仓量变化" />
        </el-form-item>
        <el-form-item label="交割结算价" prop="delvSettle">
          <el-input v-model="form.delvSettle" placeholder="请输入交割结算价" />
        </el-form-item>
      </el-form>
      <template v-if="title !== '查看期货日行情历史'" #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="FuturesDailyHistory">
import {
  listFuturesDailyHistory,
  getFuturesDailyHistory,
  delFuturesDailyHistory,
  addFuturesDailyHistory,
  updateFuturesDailyHistory
} from '@/api/invest/futuresDailyHistory'
// eslint-disable-next-line no-unused-vars
import { require } from '@/utils/require'
const { proxy } = getCurrentInstance()

const futuresDailyHistoryList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')
const operateList = ref([
  { id: 'view', icon: 'View', title: '查看', hasPermi: ['invest:futuresDailyHistory:query'] }
  // { id: 'edit', icon: 'Edit', title: '修改', hasPermi: ['invest:futuresDailyHistory:edit'] },
  // { id: 'delete', icon: 'Delete', title: '删除', hasPermi: ['invest:futuresDailyHistory:remove'] }
])
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    tsCode: null,
    tradeDate: null,
    preClose: null,
    preSettle: null,
    open: null,
    high: null,
    low: null,
    close: null,
    settle: null,
    change1: null,
    vol: null,
    amount: null,
    change2: null,
    oi: null,
    oiChg: null,
    delvSettle: null
  },
  rules: {}
})

const handleOperate = (operate, row) => {
  switch (operate) {
    case 'view':
      handleView(row)
      break
    case 'edit':
      handleUpdate(row)
      break
    case 'delete':
      handleDelete(row)
      break
    default:
      break
  }
}

const { queryParams, form, rules } = toRefs(data)

/** 查询期货日行情历史列表 */
function getList() {
  loading.value = true
  listFuturesDailyHistory(queryParams.value).then((response) => {
    futuresDailyHistoryList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    tsCode: null,
    tradeDate: null,
    preClose: null,
    preSettle: null,
    open: null,
    high: null,
    low: null,
    close: null,
    settle: null,
    change1: null,
    vol: null,
    amount: null,
    change2: null,
    oi: null,
    oiChg: null,
    delvSettle: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null
  }
  proxy.resetForm('futuresDailyHistoryRef')
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 分页
const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  getList()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

// 查看
const handleView = (row) => {
  title.value = '查看期货日行情历史'
  form.value = row
  open.value = true
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加期货日行情历史'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getFuturesDailyHistory(_id).then((response) => {
    form.value = response.data
    open.value = true
    title.value = '修改期货日行情历史'
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs.futuresDailyHistoryRef.validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateFuturesDailyHistory(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addFuturesDailyHistory(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal
    .confirm('是否确认删除选中的数据项？')
    .then(function () {
      return delFuturesDailyHistory(_ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'invest/futuresDailyHistory/export',
    {
      ...queryParams.value
    },
    `futuresDailyHistory_${new Date().getTime()}.xlsx`
  )
}

getList()
</script>
