<template>
  <div class='app-container'>
    <el-form inline>
      <el-form-item label="当前状态：">
        <el-select v-model="data.form.pushStatus" placeholder="请选择当前状态" style="width: 200px" clearable>
          <el-option v-for="item in repairStatusOptions" :key="item.value" :value="item.value" :label="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="时间：">
        <el-date-picker v-model="data.time" type="datetimerange" start-placeholder="开始时间" end-placeholder="结束时间"
          value-format="YYYY-MM-DD HH:mm:ss" @change="handleChangeTime" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="getSearch">搜索</el-button>
        <el-button icon="Refresh" @click="getRefresh">重置</el-button>
        <el-button type="primary" icon="download" plain @click="getDownload">批量导出</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="data.tableData" v-loading="loading">
      <el-table-column label="收货人" prop="receiverName" align="center" />
      <el-table-column label="手机号" prop="mobile" align="center" />
      <el-table-column label="详细地址" align="center">
        <template #default="scope">
          {{ scope.row.province }}{{ scope.row.city }}{{ scope.row.district }}{{ scope.row.address }}
        </template>
      </el-table-column>
      <el-table-column label="产品型号" prop="goodsName" align="center" />
      <el-table-column label="维修类型" prop="typeOfRepair" align="center" :formatter="typeFormatter" />
      <el-table-column label="维修数量" prop="repairQuantity" align="center" />
      <el-table-column label="购买凭证" align="center" width="100px">
        <template #default="scope">
          <el-image :src="scope.row.proofPurchase" />
        </template>
      </el-table-column>
      <el-table-column label="故障描述" prop="faultDescription" align="center" />
      <el-table-column label="物流公司" prop="logisticsName" align="center" />
      <el-table-column label="快递单号" prop="logisticsCode" align="center" />
      <el-table-column label="当前状态" align="center" :formatter="statusFormatter" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-dropdown v-if="scope.row.status === 3">
            <el-button type="primary" link>
              审核<el-icon><arrow-down /></el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="getChange(scope.row.id, 1)">通过</el-dropdown-item>
                <el-dropdown-item @click="getChange(scope.row.id, 2)">不通过</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script setup>
import { list, change } from '@/api/service/repair'
import { useCommon } from '@/hooks/index'

const { repairStatusOptions, repairTypeOptions } = useCommon()

const { proxy } = getCurrentInstance()

const data = reactive({
  form: {
    pageNum: 1,
    pageSize: 10
  },
  // 列表
  tableData: [],
  idList: []
})

// 遮罩层
const loading = ref(false)
// 总数据
const total = ref(0)

// 列表
const getList = () => {
  loading.value = true
  list(data.form).then(res => {
    if (res.code === 0) {
      data.tableData = res.data.records
      total.value = res.data.total
      loading.value = false
    }
  })
}

// 时间
const handleChangeTime = (e) => {
  data.form.startTime = e[0]
  data.form.endTime = e[1]
}

// 搜索
const getSearch = () => {
  data.form.pageNum = 1
  getList()
}

// 重置
const getRefresh = () => {
  data.form = {
    pageNum: 1,
    pageSize: 10
  }
  getList()
}

// 审核
const getChange = (id, status) => {
  change(id, status).then(res => {
    if (res.code === 0) {
      proxy.$modal.msgSuccess('审核成功！')
      getList()
    }
  })
}

// 导出
const getDownload = () => {
  proxy.download("oms/repairrecordOrder/export", data.form, `config_${new Date().getTime()}.xlsx`)
}

getList()

// 字典翻译
const statusFormatter = (row) => {
  return proxy.selectDictLabel(repairStatusOptions.value, row.status)
}

const typeFormatter = (row) => {
  return proxy.selectDictLabel(repairTypeOptions.value, row.typeOfRepair)
}

</script>