<!--
 * @Author: zwq
 * @Date: 2022-09-15 10:44:29
 * @LastEditors: zwq
 * @LastEditTime: 2023-06-27 16:07:58
 * @Description: 
-->
<template>
  <el-card shadow="never" class="aui-card--fill">
    <el-row :gutter="6" style="background-color:#e6f7ff;padding-top:10px;margin-bottom:0">
      <el-form ref="dataForm" :model="dataForm" :rules="rules" size="small" label-width="120px">
        <el-col :span="6">
          <el-form-item :label="$t('module.basicData.inspectionTime')" prop="time">
            <el-date-picker
              type="daterange"
              v-model="time"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              :style="{ width: '100%' }"
              :start-placeholder="$t('module.basicData.startTime')"
              :end-placeholder="$t('module.basicData.endTime')"
              range-separator="-"
              clearable
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item :label="$t('module.basicData.inspectionSite')" prop="inspectionSite">
            <el-input
              size="small"
              v-model="dataForm.inspectionSite"
              clearable
              :placeholder="$t('placeholder.first')+$t('module.basicData.inspectionSite')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item :label="$t('module.basicData.product')" prop="productId">
            <el-select
              v-model="dataForm.productId"
              @change="getWorkingprocedureByProductId"
              :placeholder="$t('placeholder.second')+$t('module.basicData.product')"
              filterable
              clearable
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="(item, index) in optionArr.arr0"
                :key="index"
                :label="item.name"
                :value="item.id"
                :disabled="item.disabled"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item :label="$t('module.basicData.procedure')" prop="workingProcedureId">
            <el-select
              v-model="dataForm.workingProcedureId"
              :placeholder="$t('placeholder.second')+$t('module.basicData.procedure')"
              filterable
              clearable
              @change="$forceUpdate()"
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="(item, index) in optionArr.arr4"
                :key="index"
                :label="item.workingProcedureName"
                :value="item.workingProcedureId"
                :disabled="item.disabled"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item :label="$t('module.basicData.machine')" prop="machineId">
            <el-select
              v-model="dataForm.machineId"
              :placeholder="$t('placeholder.second')+$t('module.basicData.machine')"
              filterable
              clearable
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="(item, index) in optionArr.arr1"
                :key="index"
                :label="item.name"
                :value="item.id"
                :disabled="item.disabled"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item :label="$t('module.basicData.batch')" prop="batchNumber">
            <el-input
              size="small"
              v-model="dataForm.batchNumber"
              clearable
              :placeholder="$t('placeholder.first')+$t('module.basicData.batch')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item :label="$t('module.basicData.inspectionNum')" prop="id">
            <el-input 
            size="small" 
            v-model="dataForm.id" 
            clearable 
            :placeholder="$t('placeholder.first')+$t('module.basicData.inspectionNum')" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item :label="$t('module.basicData.orderNumber')" prop="orderNumber">
            <el-input
              size="small"
              v-model="dataForm.orderNumber"
              clearable
              :placeholder="$t('placeholder.first')+$t('module.basicData.orderNumber')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="12">
          <el-form-item style="float:right;padding-right:20px">
            <el-button size="small" @click="getDataList()">
              <svg class="icon-svg"><use xlink:href="#icon-sousuo"></use></svg>
              {{ $t('query')}}
            </el-button>
            <el-button size="small" type="primary" @click="addOrUpdateHandle()">
              <svg class="icon-svg"><use xlink:href="#icon-新建"></use></svg>
              {{ $t('register')}}
            </el-button>
            <el-button size="small" type="primary" @click="exportHandle('通用作业')">
              <svg class="icon-svg">
                <use xlink:href="#导出"></use>
              </svg>
              {{ $t('export')}}
            </el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <base-table
        :table-props="tableProps"
        :page="listQuery.page"
        :limit="listQuery.limit"
        :table-data="tableData"
      >
        <method-btn
          v-if="tableBtn.length"
          slot="handleBtn"
          :width="150"
          :label="$t('handle')"
          :method-list="tableBtn"
          @clickBtn="handleClick"
        />
      </base-table>
      <pagination
        :limit.sync="listQuery.limit"
        :page.sync="listQuery.page"
        :total="listQuery.total"
        @pagination="getDataList"
      />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      :optionArr="optionArr"
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @successSubmit="getDataList"
    ></add-or-update>
  </el-card>
</template>
<script>
import basicPage from "@/mixins/basic-page";
import AddOrUpdate from './components/generalOperation-add'
import basicFilter from "@/filters/basic-filter";
import { timeFormatter } from "@/filters/basic-filter"
import i18n from "@/i18n";

const tableProps = [
  {
    prop: "id",
    label: i18n.t("module.basicData.inspectionNum"),
  },
  {
    prop: "batchNumber",
    label: i18n.t("module.basicData.batch"),
  },
  {
    prop: "inspectionStage",
    label: i18n.t("module.basicData.inspectionStage"),
    filter: basicFilter("inspectionStage"),
  },
  {
    prop: "productName",
    label: i18n.t("module.basicData.productName"),
  },
  {
    prop: "machineName",
    label: i18n.t("module.basicData.machine"),
  },
  {
    prop: "workingProcedureName",
    label: i18n.t("module.basicData.procedure"),
  },
  {
    prop: "createTime",
    label: i18n.t("module.basicData.inspectionTime"),
    filter: timeFormatter,
  },
  {
    prop: "shiftName",
    label: i18n.t("module.basicData.classes"),
  },
  {
    prop: "numberOfSamples",
    label: i18n.t("module.basicData.sampleQuantity"),
  },
  {
    prop: "numberOfDefects",
    label: i18n.t("module.basicData.defectValue"),
  },
  {
    prop: "defectiveQuantity",
    label: i18n.t("module.basicData.poorValue"),
  },
  {
    prop: "remark",
    label: i18n.t("schedule.remark"),
  },
];
const tableBtn = [
  {
    type: "edit",
    btnName: i18n.t("edit"),
  },
  {
    type: "delete",
    btnName: i18n.t("delete"),
  },
];
export default {
  mixins: [basicPage],
  data() {
    return {
      urlOptions: {
        getDataListURL: "/processInspection/inspectionSheet/page",
        deleteURL: "/processInspection/inspectionSheet",
        exportUrl: "/processInspection/inspectionSheet/export",
      },
        optionArrUrl: [
          "/basic/product/page",
          "/basic/machine/page",
          "/basic/shift/page",
          "/sys/user/page",
        ],
      tableProps,
      tableBtn,
      tableData: [],
      optionArr: {},
      time: [],
      rules: {},
    };
  },
  components: {
    AddOrUpdate,
  },
  created() {},
  activated() {
    this.getArr();
  },
  methods: {
    getArr() {
      this.optionArrUrl.forEach((item, index) => {
        this.$http
          .get(item, {
            params: {
              page: 1,
              limit: 500,
            },
          })
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.$set(this.optionArr, `arr${index}`, res.data.list);
          })
          .catch(() => {});
      });
    },
    getWorkingprocedureByProductId(val) {
      this.dataForm.workingProcedureId = ''
      if (val) {
        this.$http
          .get(`/basic/productWorkingprocedureRelation/getWorkingprocedureByProductId/${val}`)
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.$set(this.optionArr, "arr4", res.data);
          })
          .catch(() => {});
      }else{
            this.$set(this.optionArr, "arr4", []);
      }
    },
    getDataList() {
      this.dataListLoading = true;
      this.$http
        .get(this.urlOptions.getDataListURL, {
          params: {
            page: this.listQuery.page,
            limit: this.listQuery.limit,
            startTime: this.time && this.time.length > 0 ? this.time[0] : "",
            endTime: this.time && this.time.length > 0 ? this.time[1] : "",
            ...this.dataForm,
          },
        })
        .then(({ data: res }) => {
          this.dataListLoading = false;
          if (res.code !== 0) {
            this.tableData = [];
            this.listQuery.total = 0;
            return this.$message.error(res.msg);
          }
          this.tableData = res.data.list;
          this.listQuery.total = res.data.total;
        })
        .catch(() => {
          this.dataListLoading = false;
        });
    },
  },
};
</script>

<style></style>
