<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" label-width="100px" v-if="showSearch">

        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="项目编码:">
              <el-input v-model="filterQuery.code" style="width: 90%" placeholder="请输入项目编码"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="项目内容:">
              <el-input v-model="filterQuery.content" style="width: 90%" placeholder="请输入项目内容"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
<!--          <el-col :span="8">-->
<!--            <el-form-item class="query-form-item" label="项目类型:">-->
<!--              <el-select v-model="filterQuery.type" style="width: 90%" placeholder="请选择项目类型" clearable="true"-->
<!--              >-->
<!--                <el-option label="巡检" :value="1"></el-option>-->
<!--                <el-option label="养护" :value="2"></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="8">
            <el-form-item class="query-form-item" label="创建时间:">
              <el-date-picker
                  :picker-options="pickerOptions"
                  v-model="actDateRange"
                  type="daterange"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  style="width: 90%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

        </el-row>


      </el-form>
      <div class="top-right-btn">
        <el-button-group style="margin-left: 10px;display:flex;">
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="selectRepairOrder">选入</el-button>
        </el-button-group>
      </div>

      <el-divider style="margin: 15px"></el-divider>

      <el-table v-loading="loading" :data="list" style="width: 100%" highlight-current-row
                @current-change="selectDetail" border>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="项目编码" prop="code" show-overflow-tooltip width="140"></el-table-column>
        <el-table-column label="项目内容" prop="content" show-overflow-tooltip width="180"></el-table-column>
        <el-table-column label="项目类型" prop="type" show-overflow-tooltip width="120">
          <template slot-scope="scope">
            <el-tag>
              {{ projectSetType[scope.row.type] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建人" prop="createUserName" show-overflow-tooltip width="140"></el-table-column>
        <el-table-column label="更新人" prop="updateUserName" show-overflow-tooltip width="140"></el-table-column>
        <el-table-column label="创建时间" prop="createTime" show-overflow-tooltip width="140"></el-table-column>
        <el-table-column label="更新时间" prop="udpateTime" show-overflow-tooltip width="140"></el-table-column>
        <el-table-column label="备注" prop="remak" show-overflow-tooltip width="140"></el-table-column>
      </el-table>
      <pagination
          v-show="total>0"
          :total="total"
          :limit.sync="filterQuery.limit"
          :page.sync="filterQuery.page"
          @pagination="getList"
      ></pagination>
    </el-card>


  </div>
</template>

<script>
import {findInvByUser} from "@/api/system/invSubWarehouse";
import {getprojectSetList, deleteDevprojectSet,} from "@/api/dev/deviceProjectSet";
import deviceRepairOrderModify from "@/views/dev/DeviceRepairOrderModify.vue";
import DeviceProjectSetDialog from "@/views/dev/DeviceProjectSetDialog"
import {isBlank} from "@/utils/strUtil";
import {addDeviceInspectPlanDelect} from "@/api/dev/deviceInspectPlanDelect";
import {addDeviceInspecTakeDetail} from "@/api/dev/deviceInspectTake"


export default {
  name: "DeviceProjectSelectTake",
  props: {
    inputQuery: {
      type: Object,
      required: true
    },
    type: {
      type: Object,
      required: true
    },
    closeDialog: {
      type: Function,
      required: true
    }
  },
  data() {
    return {
      filterQuery: {
        id: null,
        code: "",
        content: null,
        type: null,
        startTime: null,
        endTime: null,
        page: 1,
        limit: 20
      },
      Query:{
        id:null,
        projectCode:"",
        projectContent:"",
        result:"",
        taskOrderIdFk:""
      },
      selectQuery: {},
      list: [],
      total: 0,
      loading: false,
      formVisible: false,
      codeTableLoading: false,

      projectSetType: {
        1: "巡检",
        2: "养护",
        3: "报修"
      },
      formName: null,
      deviceRepairOrder: {},
      showSearch: true,
      actDateRange: [],
      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]);
            },
          },
        ],
      },
    };
  },
  methods: {
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        id: null,
        code: "",
        content: null,
        type: null,
        startTime: null,
        endTime: null,
        page: 1,
        limit: 20
      };
      this.actDateRange = [];
      this.getList();
    },
    onSubmit() {
      if (this.actDateRange !== null) {
        this.filterQuery.startTime = this.actDateRange[0];
        this.filterQuery.endTime = this.actDateRange[1];
      } else {
        this.filterQuery.startTime = null;
        this.filterQuery.endTime = null;
      }
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      this.filterQuery.type = this.type
      getprojectSetList(this.filterQuery).then((res) => {
        this.loading = false;
        if (res.code === 20000) {
          this.list = res.data.list || [];
          this.total = res.data.total || 0;
        } else {
          this.$message.error(res.message);
          this.list = [];
          this.total = 0;
        }
      }).catch((error) => {
        this.loading = false;
        this.$message.error(error.message);
        this.list = [];
        this.total = 0;
      })
    },
    invChange() {
      this.getList();
    },
    getInvList() {
      let params = {deptCode: this.filterQuery.deptCode};
      findInvByUser(params)
          .then((response) => {
            this.invList = response.data || [];
            this.getList();
          })
          .catch(() => {
          });
    },
    selectRepairOrder() {
      if (isBlank(this.selectQuery.id)) {
        return this.$message.error("请先选择巡检内容");
      }
      this.selectQuery.id = ""
      this.Query =  this.inputQuery
      this.Query.result =  this.selectQuery.remak
      this.Query.projectContent =  this.selectQuery.content
      this.Query.projectCode =  this.selectQuery.code
      this.Query.status =  1

      addDeviceInspecTakeDetail(this.Query).then((res) => {
        if (res.code == 20000) {
          this.closeDialog();
        } else {
          this.$message.error(res.message);
        }
      }).catch((error) => {
        this.$message.error(error.message);
      })
    },
    selectDetail(row) {
      this.selectQuery = row
    }
  },
  components: {
    deviceRepairOrderModify, DeviceProjectSetDialog
  },
  filters: {
    statusFilterType(status) {
      const statusMap = {
        2: "warning",
        3: "success",
        4: "danger",
      };
      return statusMap[status];
    },
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    this.getList();
  },
};
</script>

<style type="text/scss" lang="scss">
</style>

