<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="empName">
        <el-input
            v-model="queryParams.empName"
            placeholder="请输入姓名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间" prop="empDate">
        <el-date-picker clearable
                        v-model="queryParams.empDate"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="班组" prop="empTeam">
        <el-select clearable v-model="queryParams.empTeam" placeholder="请选择" style="width: 200px">
          <el-option value="" label=""></el-option>
          <el-option value="A" label="A班"></el-option>
          <el-option value="B" label="B班"></el-option>
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="机台号" prop="deviceId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.deviceId"-->
      <!--          placeholder="请输入机台号"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="稼动率" prop="efficiency">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.efficiency"-->
      <!--          placeholder="请输入稼动率"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="规格" prop="spec">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.spec"-->
      <!--          placeholder="请输入规格"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="米克重" prop="mick">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.mick"-->
      <!--          placeholder="请输入米克重"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="密度" prop="density">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.density"-->
      <!--          placeholder="请输入密度"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="产量" prop="length">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.length"-->
      <!--          placeholder="请输入产量"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="毛高" prop="height">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.height"-->
      <!--          placeholder="请输入毛高"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="单价" prop="price">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.price"-->
      <!--          placeholder="请输入单价"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="总价" prop="totalPrice">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.totalPrice"-->
      <!--          placeholder="请输入总价"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </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="primary"-->
      <!--          plain-->
      <!--          icon="Plus"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['emp:empCalc:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="Edit"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['emp:empCalc:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="Delete"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['emp:empCalc:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExportD"
        >日报导出
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Download"
            @click="handleExportM"
        >月报导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="empCalcList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="统计ID" align="center" prop="calcId"/>
      <el-table-column label="姓名" align="center" prop="empName"/>
      <el-table-column label="时间" align="center" prop="empDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.empDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班组" align="center" prop="empTeam"/>
      <el-table-column label="机台号" align="center" prop="deviceId"/>
      <el-table-column label="稼动率" align="center" prop="efficiency">
        <template #default="scope">
          <span>{{ (scope.row.efficiency * 100).toFixed(2) }}%</span>
        </template>
      </el-table-column>
      <el-table-column label="规格" align="center" prop="spec"/>
      <el-table-column label="米克重" align="center" prop="mick"/>
      <el-table-column label="密度" align="center" prop="density"/>
      <el-table-column label="产量" align="center" prop="length"/>
      <el-table-column label="毛高" align="center" prop="height"/>
      <el-table-column label="单价" align="center" prop="price"/>
      <el-table-column label="总价" align="center" prop="totalPrice">
        <template #default="scope">
          <span>{{ (scope.row.totalPrice).toFixed(2) }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template #default="scope">-->
      <!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['emp:empCalc:edit']">修改</el-button>-->
      <!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['emp:empCalc:remove']">删除</el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改员工产量统计对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="empCalcRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="empName">
          <el-input v-model="form.empName" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="时间" prop="empDate">
          <el-date-picker clearable
                          v-model="form.empDate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="班组" prop="empTeam">
          <el-input v-model="form.empTeam" placeholder="请输入班组"/>
        </el-form-item>
        <el-form-item label="机台号" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入机台号"/>
        </el-form-item>
        <el-form-item label="稼动率" prop="efficiency">
          <el-input v-model="form.efficiency" placeholder="请输入稼动率"/>
        </el-form-item>
        <el-form-item label="规格" prop="spec">
          <el-input v-model="form.spec" placeholder="请输入规格"/>
        </el-form-item>
        <el-form-item label="米克重" prop="mick">
          <el-input v-model="form.mick" placeholder="请输入米克重"/>
        </el-form-item>
        <el-form-item label="密度" prop="density">
          <el-input v-model="form.density" placeholder="请输入密度"/>
        </el-form-item>
        <el-form-item label="产量" prop="length">
          <el-input v-model="form.length" placeholder="请输入产量"/>
        </el-form-item>
        <el-form-item label="毛高" prop="height">
          <el-input v-model="form.height" placeholder="请输入毛高"/>
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价"/>
        </el-form-item>
        <el-form-item label="总价" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入总价"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 导出日报 -->
    <el-dialog title="日报导出" v-model="exportD" width="250px" append-to-body>

      <el-date-picker clearable
                      v-model="dayTime"
                      type="date"
                      value-format="YYYY-MM-DD"
                      placeholder="请选择时间">
      </el-date-picker>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="report('D')">确 定</el-button>
          <el-button @click="cancel('D')">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 导出日报 -->
    <el-dialog title="月报导出" v-model="exportM" width="250px" append-to-body>
      <el-date-picker clearable
                      v-model="monthTime"
                      type="month"
                      value-format="YYYY-MM"
                      placeholder="请选择月份">
      </el-date-picker>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="report('M')">确 定</el-button>
          <el-button @click="cancel('M')">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="EmpCalc">
import {addEmpCalc, delEmpCalc, getEmpCalc, listEmpCalc, updateEmpCalc} from "@/api/emp/empCalc";

const {proxy} = getCurrentInstance();

const empCalcList = ref([]);
const open = ref(false);
const exportD = ref(false);
const exportM = 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 dayTime = ref("")
const monthTime = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    empName: null,
    empDate: null,
    empTeam: null,
    deviceId: null,
    efficiency: null,
    spec: null,
    mick: null,
    density: null,
    length: null,
    height: null,
    price: null,
    totalPrice: null,
    orderByColumn: 'calcId',
    isAsc: 'descending'
  },
  rules: {}
});

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

/** 查询员工产量统计列表 */
function getList() {
  loading.value = true;
  listEmpCalc(queryParams.value).then(response => {
    empCalcList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel(flag) {
  if (flag == 'D') {
    exportD.value = false;
  } else if (flag == 'M') {
    exportM.value = false;
  } else {
    open.value = false;
    reset();
  }
}

// 表单重置
function reset() {
  form.value = {
    calcId: null,
    empName: null,
    empDate: null,
    empTeam: null,
    deviceId: null,
    efficiency: null,
    spec: null,
    mick: null,
    density: null,
    length: null,
    height: null,
    price: null,
    totalPrice: null
  };
  proxy.resetForm("empCalcRef");
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加员工产量统计";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _calcId = row.calcId || ids.value
  getEmpCalc(_calcId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改员工产量统计";
  });
}

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

/** 删除按钮操作 */
function handleDelete(row) {
  const _calcIds = row.calcId || ids.value;
  proxy.$modal.confirm('是否确认删除员工产量统计编号为"' + _calcIds + '"的数据项？').then(function () {
    return delEmpCalc(_calcIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExportD() {
  exportD.value = true;
}

/** 导出日报 */
function handleExportM() {
  exportM.value = true;
  // proxy.download('emp/empCalc/export', {
  //   ...queryParams.value
  // }, `empCalc_${new Date().getTime()}.xlsx`)
}

function report(flag) {
  let date;
  let name;
  let url;
  if (flag === 'D') {
    date = dayTime.value;
    url = '/api/export/productivity-day?date=' + dayTime.value;
    name = `经编生产绩效日报${dayTime.value}.xlsx`;
  } else {
    date = monthTime.value;
    url = '/api/export/productivity-month?date=' + dayTime.value;
    name = `经编生产绩效日报${dayTime.value}.xlsx`;
  }

  if (!date) {
    proxy.$modal.msgError("请选择导出时间");
  } else {
    proxy.download(url, {}, name)
    cancel(flag);
  }
}

getList();
</script>
