<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="SOP编号" prop="sopCode">
        <el-input
          v-model="queryParams.sopCode"
          placeholder="请输入SOP编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="SOP名称" prop="sopName">
        <el-input
          v-model="queryParams.sopName"
          placeholder="请输入SOP名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="sopStatus">
        <el-select v-model="queryParams.sopStatus" placeholder="状态">
          <el-option
            v-for="dict in dict.type.mes_calendar_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车间名称" prop="workshopName">
        <el-select
          v-model="queryParams.workshopName"
          placeholder="请选择车间名称"
          @change="workShopChange"
        >
          <el-option
            v-for="item in this.workshopList"
            :key="item.key"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="产线" prop="workareaName">
        <el-select v-model="queryParams.workareaName" placeholder="请选择产线">
          <el-option
            v-for="dict in this.workareaList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          style="width: 218px"
          v-model="createTime"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="SOP类型" prop="sopType">
        <el-select v-model="queryParams.sopType" placeholder="请选择SOP类型">
          <el-option
            v-for="dict in dict.type.mes_calendar_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @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="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:sop:sopm:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:sop:sopm:batchremove']"
          >批量删除
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['mes:cal:team:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="sopList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="SOP编号" align="center" prop="sopCode">
        <!-- <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['mes:sop:sopm:query']"
            >{{ scope.row.teamCode }}
          </el-button>
        </template> -->
      </el-table-column>
      <el-table-column label="SOP名称" align="center" prop="sopName" />
      <el-table-column label="状态" align="center" prop="sopStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.mes_calendar_type"
            :value="scope.row.sopStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="车间名称" align="center" prop="workshopName">
      </el-table-column>

      <el-table-column label="SOP类型" align="center" prop="sopType">
      </el-table-column>
      <el-table-column label="产线" align="center" prop="workareaName">
      </el-table-column>
      <el-table-column label="隔离点数量" align="center" prop="pointCount">
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:sop:sopm:edit']"
            >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:sop:sopm:progress']"
            >执行
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:sop:sopm:remove']"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listTeam,
  getTeam,
  delTeam,
  addTeam,
  updateTeam,
} from "@/api/mes/cal/team";
import {
  workshoplistAll,
  getIsWorkareaList,
} from "@/api/mes/spm/segregationPoint";
import { getIsSopPage } from "@/api/mes/sop/sopindex";
import { genCode } from "@/api/system/autocode/rule";

export default {
  name: "Team",

  dicts: ["mes_calendar_type"],
  data() {
    return {
      //自动生成编码
      autoGenFlag: false,
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 班组表格数据
      sopList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 车间数据
      workshopList: [],
      // 工作区域数据
      workareaList: [],
      // 查询参数
      createTime: "",
      queryParams: {
        current: 1,
        size: 10,
        sopCode: null,
        sopName: null,
        sopStatus: null,
        workshopName: null,
        workareaName: null,
        startTime: "",
        endTime: "",
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    this.getworkShop();
  },

  methods: {
    // 格式化日期查询数据
    formatDate(date) {
      if (date && date instanceof Date && !isNaN(date)) {
        // 使用本地时间
        return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(
          2,
          "0"
        )}-${String(date.getDate()).padStart(2, "0")}`;
      }
      return null;
    },
    /** 查询sop列表 */
    getList() {
      this.loading = true;
      // 格式化日期并更新queryParams
      if (Array.isArray(this.createTime) && this.createTime.length === 2) {
        this.queryParams.startTime = this.formatDate(this.createTime[0]);
        this.queryParams.endTime = this.formatDate(this.createTime[1]);
      }
      getIsSopPage(this.queryParams).then((response) => {
        console.log(response, "接口返回结果");
        this.sopList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 获取车间数据
    getworkShop() {
      workshoplistAll().then((response) => {
        // console.log(response, "获取车间数据");
        this.workshopList = response.data.map((item) => {
          return {
            label: item.workshopName,
            value: item.workshopId,
            key: item.workshopCode,
          };
        });
      });
    },
    // 所属车间下拉改变工作区域内容
    workShopChange() {
      this.getworkArea();
    },
    // 获取工作区域数据
    getworkArea() {
      const workshopId = this.form.workshopId;
      if (workshopId) {
        getIsWorkareaList(workshopId).then((response) => {
          // console.log(response, "获取工作区域数据");
          this.workareaList = response.data.map((item) => {
            return {
              label: item.workareaName,
              value: item.workareaId,
              key: item.workareaCode,
            };
          });
        });
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sopCode: null,
        sopName: null,
        sopStatus: null,
        workshopName: null,
        workareaName: null,
        pointCount: null,
        sopType: null,
        createTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.createTime = "";
      (this.queryParams.startTime = ""),
        (this.queryParams.endTime = ""),
        this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.teamId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.$router.push("/mes/sop/sopm/NewSop");
      this.title = "添加隔离点";
      this.optType = "add";
    },
    // 查询明细按钮操作
    // handleView(row) {
    //   this.reset();
    //   const teamId = row.teamId || this.ids;
    //   getTeam(teamId).then((response) => {
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "查看班组";
    //     this.optType = "view";
    //   });
    // },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const teamId = row.teamId || this.ids;
      getTeam(teamId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改隔离点信息";
        this.optType = "edit";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.teamId != null) {
            updateTeam(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeam(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const teamIds = row.teamId || this.ids;
      this.$modal
        .confirm('是否确认删除班组编号为"' + teamIds + '"的数据项？')
        .then(function () {
          return delTeam(teamIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    // handleExport() {
    //   this.download(
    //     "cal/team/export",
    //     {
    //       ...this.queryParams,
    //     },
    //     `team_${new Date().getTime()}.xlsx`
    //   );
    // },
  },
};
</script>
<style lang="scss" src="@/assets/styles/dialog-title.scss" scoped>
.el-input-width {
  width: 380px !important;
}
</style>
