<template>
  <div class="commissionManagerDiv">
    <div class="commissionManagerSearchDiv">
      <el-button style="margin-left: 10px" type="warning" @click="handleAddCommissionButtonClick">添加佣金</el-button>
      <!--      <el-input
        type="number"
        style="margin-left: 10px"
        v-model="searchContent"
        placeholder="请输入要搜索的价格"
        class="searchInput"
      >
        <template #prepend>
          <el-select v-model="searchStoreId" filterable>
            <el-option v-for="store of stores" :label="store.name" :value="store.id" :key="store.id" />
          </el-select>
        </template>
        <template #append>
          <el-button :icon="Search" @click="search" />
        </template>
      </el-input>-->
    </div>
    <div class="commissionManagerTableDiv">
      <el-table :data="tableShowData" style="width: 100%" table-layout="auto">
        <el-table-column fixed prop="id" label="ID" width="50px" />
        <el-table-column prop="priceMin" label="最低价格" width="200px" />
        <el-table-column prop="priceMax" label="最高价格" width="200px" />
        <el-table-column prop="commission" label="佣金" width="200px" />
        <el-table-column prop="storeId" label="店铺ID" width="100px" />
        <el-table-column prop="createTime" label="创建时间" width="400px" />
        <el-table-column prop="updateTime" label="更新时间" width="400px" />
        <el-table-column prop="status" label="状态" width="100px">
          <template #default="scope">
            <el-switch :model-value="scope.row.status" @click="handleChangeCommissionStatus(scope.$index)" />
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="210px">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="handleExchangeButtonClick(scope.row)">修改</el-button>
            <!--            <el-button link type="primary" size="small" @click="handleChangeStatusButtonClick(scope.row)"-->
            <!--              >修改状态</el-button-->
            <!--            >-->
            <!--            <el-button link type="primary" size="small" @click="handleFlagButtonClick(scope.row)">插旗</el-button>-->
            <!--            <el-button link type="primary" size="small" @click="handleRemoveButtonClick(scope.row)">删除</el-button>-->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="commissionManagerPaginationDiv">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="totalCommissionCount"
        :current-page="nowPage"
        @update:current-page="handleChangePage"
      />
    </div>
    <el-dialog
      v-model="showExchangeDialog"
      title="修改佣金"
      width="30%"
      :before-close="
        () => {
          exchangeCommission = undefined
        }
      "
    >
      <el-form :model="exchangeCommission" label-width="150px">
        <el-form-item label="最低价格" prop="priceMin">
          <el-input v-model.number="exchangeCommission.priceMin" type="number" />
        </el-form-item>
        <el-form-item label="最高价格" prop="priceMax">
          <el-input v-model.number="exchangeCommission.priceMax" type="number" />
        </el-form-item>
        <el-form-item label="佣金" prop="commission">
          <el-input v-model.number="exchangeCommission.commission" type="number" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showExchangeDialog = false">取消</el-button>
          <el-button type="primary" @click="handleExchange">确认</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog
      v-model="showAddDialog"
      title="添加佣金"
      width="30%"
      :before-close="
        () => {
          addCommission = { priceMin: 0, priceMax: 0, commission: 0, storeId: 1 }
        }
      "
    >
      <el-form :model="addCommission" label-width="150px">
        <el-form-item label="最低价格" prop="priceMin">
          <el-input v-model.number="addCommission.priceMin" type="number" />
        </el-form-item>
        <el-form-item label="最高价格" prop="priceMax">
          <el-input v-model.number="addCommission.priceMax" type="number" />
        </el-form-item>
        <el-form-item label="佣金" prop="commission">
          <el-input v-model.number="addCommission.commission" type="number" />
        </el-form-item>
        <el-form-item label="店铺" prop="storeId">
          <el-select v-model="addCommission.storeId" filterable>
            <el-option v-for="store of stores" :label="store.name" :value="store.id" :key="store.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showAddDialog = false">取消</el-button>
          <el-button type="primary" @click="handleAdd">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from "vue"
import { ICommission } from "@/api/commission/types/commission"
import { ElMessage } from "element-plus"
import { addCommissionApi, changeCommissionApi, changeCommissionStatusApi, getCommissionsApi } from "@/api/commission"
import { IStore } from "@/api/store/types/store"
import { getAllStoreApi } from "@/api/store"

const nowPage = ref<number>(1)
const totalCommissionCount = ref<number>(1)
const tableShowData = ref<ICommission[]>([])
const showExchangeDialog = ref<boolean>()
const exchangeCommission = ref<ICommission>()
const showAddDialog = ref<boolean>()
const addCommission = ref<ICommission>({ priceMin: 0, priceMax: 0, commission: 0, storeId: 1 })
const stores = ref<IStore[]>([])
const handleAdd = () => {
  addCommissionApi(addCommission.value)
    .then(() => {
      ElMessage.success("添加成功")
      handleChangePage()
    })
    .catch((err) => {
      console.log(err)
      ElMessage.error("添加失败，请重试！")
    })
  showAddDialog.value = false
}
const handleAddCommissionButtonClick = () => {
  showAddDialog.value = true
}
const handleChangePage = () => {
  getCommissionsApi(nowPage.value)
    .then((response) => {
      tableShowData.value = response.data.data.info
      totalCommissionCount.value = response.data.data.total
    })
    .catch(() => {
      ElMessage.error("获取数据失败！")
    })
}
const handleChangeCommissionStatus = (index: number) => {
  changeCommissionStatusApi(tableShowData.value[index])
    .then(() => {
      ElMessage.success("修改成功！")
      tableShowData.value[index].status = !tableShowData.value[index].status
    })
    .catch((err) => {
      console.log(err)
      ElMessage.error("修改失败！")
    })
}
const handleExchangeButtonClick = (row: ICommission) => {
  exchangeCommission.value = row
  showExchangeDialog.value = true
}
const handleExchange = () => {
  if (exchangeCommission.value) {
    changeCommissionApi(exchangeCommission.value)
      .then(() => {
        ElMessage.success("修改成功！")
      })
      .catch((err) => {
        console.log(err)
        ElMessage.error("修改失败，请重试！")
      })
    showExchangeDialog.value = false
  }
}
onMounted(() => {
  getAllStoreApi()
    .then((response) => {
      stores.value = response.data.data
    })
    .catch((err) => {
      console.log(err)
      ElMessage.error("获取店铺失败！")
    })
  handleChangePage()
})
</script>

<style scoped>
.commissionManagerDiv {
  width: calc(100% - 20px);
  height: calc(100% - 20px);
  margin: 10px 10px 10px 10px;
}
.commissionManagerTableDiv {
  width: 100%;
  height: calc(90% - 20px);
  margin-top: 10px;
}
.commissionManagerPaginationDiv {
  height: 5%;
  margin-top: 10px;
  min-height: 32px;
}
.commissionManagerSearchDiv {
  height: 5%;
  width: 100%;
}
.searchInput {
  width: calc(30% + 100px);
}
</style>
