<template>
  <div>
    <el-form-item>
      <el-input
        v-model="listQuery.search"
        placeholder="请输入搜索关键词"
        clearable
        size="small"
        style="width: 180px"
        @keyup.enter.native="getList"
      />
    </el-form-item>
    <el-form-item v-if="genre !== 'upcoming'">
      <el-select
        v-model="listQuery.processor"
        filterable
        clearable
        remote
        size="small"
        reserve-keyword
        placeholder="请输入当前处理人"
        :remote-method="remoteUserList"
        :loading="loading"
        style="width: 150px"
        @change="getList"
      >
        <el-option
          v-for="item in UserOptions"
          :key="item.userId"
          :label="item.nickName"
          :value="item.userId"
        />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-select
        v-model="listQuery.processId"
        placeholder="请选择流程"
        size="small"
        clearable
        style="width: 150px"
        @change="getList"
      >
        <el-option-group
          v-for="group in classifyList"
          :key="group.id"
          :label="group.name"
        >
          <el-option
            v-for="item in group.process_list"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-option-group>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-select
        v-model="listQuery.priority"
        placeholder="请选择优先级"
        size="small"
        clearable
        style="width: 150px"
        @change="getList"
      >
        <el-option label="一般" :value="1" />
        <el-option label="紧急" :value="2" />
        <el-option label="非常紧急" :value="3" />
      </el-select>
    </el-form-item>
    <el-form-item v-if="genre !== 'upcoming'">
      <el-select
        v-model="listQuery.isEnd"
        placeholder="是否结束"
        size="small"
        clearable
        style="width: 150px"
        @change="getList"
      >
        <el-option label="是" :value="1" />
        <el-option label="否" :value="0" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <!-- END: 手动结束  PASS:完成  UNPASS: 拒绝 -->
      <el-select
        v-model="listQuery.endType"
        placeholder="请选择工单结束状态"
        size="small"
        clearable
        style="width: 150px"
        @change="getList"
      >
        <el-option label="手动结束" value="END" />
        <el-option label="完成" value="PASS" />
        <el-option label="拒绝" value="UNPASS" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-date-picker
        v-model="timeValue"
        clearable
        size="small"
        type="datetimerange"
        :picker-options="pickerOptions"
        range-separator="至"
        start-placeholder="创建开始日期"
        end-placeholder="创建结束日期"
        align="right"
        @change="getList"
      />
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        icon="el-icon-search"
        size="small"
        @click="getList"
        >搜索</el-button
      >
    </el-form-item>
  </div>
</template>

<script>
import { listUser } from "@/api/system/sysuser";
import { classifyProcessList } from "@/api/process/admin/process";

import { parseTime } from "@/utils";
export default {
  name: "WorkOrderSearch",
  // eslint-disable-next-line vue/require-prop-types
  props: ["genre", "list"],
  data() {
    return {
      loading: false,
      timeValue: "",
      classifyList: [],
      listQuery: {},
      UserOptions: [],
      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]);
            },
          },
        ],
      },
    };
  },
  created() {
    this.getClassesList();
    console.log(this.list, "child component");
    this.listQuery = { ...this.list };
    if (this.listQuery.startTime && this.listQuery.endTime) {
      this.timeValue = [this.listQuery.startTime, this.listQuery.endTime];
    }
    if (this.listQuery.processor && this.listQuery.processorName) {
      this.listQuery.processor = Number(this.listQuery.processor);
      this.remoteUserList(this.listQuery.processorName);
    }
    console.log(this.listQuery);
  },
  methods: {
    getList() {
      if (
        this.timeValue === null ||
        this.timeValue === undefined ||
        this.timeValue === ""
      ) {
        this.listQuery.startTime = "";
        this.listQuery.endTime = "";
      } else {
        this.listQuery.startTime = parseTime(this.timeValue[0]);
        this.listQuery.endTime = parseTime(this.timeValue[1]);
      }
      // 处理显示用户名
      if (this.listQuery.processor) {
        this.UserOptions.forEach((item) => {
          if (this.listQuery.processor == item.userId) {
            this.listQuery.processorName = item.nickName;
          }
        });
      } else {
        this.listQuery.processorName = "";
      }
      console.log(this.listQuery, "listQuery showInfo");
      this.$emit("handleSearch", this.listQuery);
    },
    remoteUserList(query) {
      listUser({
        pageSize: 999999,
        nickName: query,
      }).then((res) => {
        this.UserOptions = res.data.list;
      });
    },
    getClassesList() {
      classifyProcessList().then((res) => {
        this.classifyList = res.data || [];
      });
    },
  },
};
</script>

<style scoped></style>
