<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">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dailyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商" align="center" prop="supplierId" />
      <el-table-column label="日常照片" align="center" >
        <template #default="scope">
          <el-button :icon="Folder" circle @click=""></el-button>
        </template>
      </el-table-column>
      <el-table-column label="上传时间" align="center" prop="uploadDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.uploadDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" />
      <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="Daily">
import { listDaily, getDaily, delDaily, addDaily, updateDaily } from "@/api/supplier/daily";

const { proxy } = getCurrentInstance();

const dailyList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
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,
    supplierId: null,
    uploadDate: null,
    status: null,
    createrCode: null,
    createdate: null,
    updaterCode: null,
    updatedate: null,
    deptId: null,
    remarks: null
  },
  rules: {
    supplierId: [
      { required: true, message: "供应商id不能为空", trigger: "blur" }
    ],
    uploadDate: [
      { required: true, message: "上传时间不能为空", trigger: "blur" }
    ],
  }
});

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

/** 查询供应商日常工作记录列表 */
function getList() {
  loading.value = true;
  listDaily(queryParams.value).then(response => {
    dailyList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

// 表单重置
function reset() {
  form.value = {
    dailyId: null,
    supplierId: null,
    uploadDate: null,
    status: null,
    delFlag: null,
    createrCode: null,
    createdate: null,
    updaterCode: null,
    updatedate: null,
    deptId: null,
    remarks: null
  };
  proxy.resetForm("dailyRef");
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加供应商日常工作记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _dailyId = row.dailyId || ids.value
  getDaily(_dailyId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改供应商日常工作记录";
  });
}

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

/** 删除按钮操作 */
function handleDelete(row) {
  const _dailyIds = row.dailyId || ids.value;
  proxy.$modal.confirm('是否确认删除供应商日常工作记录编号为"' + _dailyIds + '"的数据项？').then(function() {
    return delDaily(_dailyIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

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

getList();
</script>
