<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="供应商" prop="supplierId">
        <el-select v-model="queryParams.supplierId" placeholder="请选择供应商" style="width: 240px">
          <el-option
              v-for="item in supplierList"
              :key="item.supplierId"
              :label="item.supplierName"
              :value="item.supplierId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['supplier:rating:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ratingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商" align="center" prop="score" />
      <el-table-column label="评分" align="center" prop="score" />
      <el-table-column label="评价内容" align="center" prop="comment" />
      <el-table-column label="评价时间" align="center" prop="rateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.rateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Rating">
import { listRating, getRating, delRating, addRating, updateRating } from "@/api/supplier/rating";

const { proxy } = getCurrentInstance();

const ratingList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(false);
const ids = ref([]);
const supplierList = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderId: null,
    menuId: null,
    score: null,
    comment: null,
    rateTime: null,
    status: null,
    createrCode: null,
    createdate: null,
    updaterCode: null,
    updatedate: null,
    deptId: null,
    remarks: null
  },
  rules: {
    orderId: [
      { required: true, message: "订单id不能为空", trigger: "blur" }
    ],
    menuId: [
      { required: true, message: "菜单id不能为空", trigger: "blur" }
    ],
    score: [
      { required: true, message: "评分不能为空", trigger: "blur" }
    ],
    comment: [
      { required: true, message: "评价内容不能为空", trigger: "blur" }
    ],
    rateTime: [
      { required: true, message: "评价时间不能为空", trigger: "blur" }
    ],
  }
});

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

/** 查询评价列表 */
function getList() {
  loading.value = true;
  listRating(queryParams.value).then(response => {
    ratingList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

// 表单重置
function reset() {
  form.value = {
    rateId: null,
    orderId: null,
    menuId: null,
    score: null,
    comment: null,
    rateTime: null,
    status: null,
    delFlag: null,
    createrCode: null,
    createdate: null,
    updaterCode: null,
    updatedate: null,
    deptId: null,
    remarks: null
  };
  proxy.resetForm("ratingRef");
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加评价";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _rateId = row.rateId || ids.value
  getRating(_rateId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改评价";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["ratingRef"].validate(valid => {
    if (valid) {
      if (form.value.rateId != null) {
        updateRating(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRating(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _rateIds = row.rateId || ids.value;
  proxy.$modal.confirm('是否确认删除评价编号为"' + _rateIds + '"的数据项？').then(function() {
    return delRating(_rateIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

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

getList();
</script>
