<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" size="mini" label-width="100px" v-if="showSearch">
        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="部门:">
              <el-select v-model="filterQuery.deptCode" placeholder="请选择部门" clearable="true"
                         @change="deptChange"
                         style="width: 90%"
              >
                <el-option
                  v-for="item in deptList"
                  :key="item.name"
                  :label="item.name"
                  :value="item.code">
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item class="query-form-item" label="仓库:">
              <el-select v-model="filterQuery.invCode" placeholder="请选择仓库" clearable="true"
                         style="width: 90%"
              >
                <el-option
                  v-for="item in invList"
                  :key="item.name"
                  :label="item.name"
                  :value="item.code">
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item class="query-form-item" label="计划单号:">
              <el-input v-model="filterQuery.orderId" placeholder="请输入计划单号" style="width: 90%"
                        clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="资产编码:">
              <el-input v-model="filterQuery.code" placeholder="请输入资产编码" style="width: 90%"
                        clearable></el-input>
            </el-form-item>
          </el-col>
<!--          <el-col :span="8">-->
<!--            <el-form-item label="计划级别:" class="query-form-item" >-->
<!--              <el-select v-model="filterQuery.level" style="width: 90%" placeholder="请选择计划级别" clearable>-->
<!--                <el-option label="低级" :value=1></el-option>-->
<!--                <el-option label="中级" :value=2></el-option>-->
<!--                <el-option label="高级" :value=3></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-col :span="8">
            <el-form-item class="query-form-item" label="结束时间:">
              <el-date-picker
                  :picker-options="pickerOptions"
                  v-model="endDateRange"
                  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>
          <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-group>
      </div>

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

      <el-table v-loading="loading" :data="list" style="width: 100%" @current-change="handleDetail"   highlight-current-row border>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="计划单号" prop="orderId"></el-table-column>
        <!--        <el-table-column label="部门" prop="deptName"></el-table-column>-->
        <!--        <el-table-column label="仓库" prop="invName"></el-table-column>-->
        <el-table-column label="设备名称" prop="deviceName"></el-table-column>
        <el-table-column label="资产编码" prop="code"></el-table-column>
        <el-table-column label="规格型号" prop="ggxh"></el-table-column>
        <el-table-column label="批次号" prop="batchNo"></el-table-column>
        <el-table-column label="开始时间" prop="startTime"></el-table-column>
        <el-table-column label="结束时间" prop="endTime"></el-table-column>
        <el-table-column label="计划周期">
          <template slot-scope="scope">
            {{ scope.row.frequency }}   {{ frequencyUnitMap[scope.row.frequencyUnit] }}
          </template>
        </el-table-column>
        <el-table-column label="创建人" prop="createUserName"></el-table-column>
<!--        <el-table-column label="计划状态" prop="planStatus">-->
<!--          <template slot-scope="scope">-->
<!--            <el-tag :type="statusFilterType(scope.row.planStatus)">{{ planStatusMap[scope.row.planStatus] }}</el-tag>-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column label="计划级别" prop="level" show-overflow-tooltip width="120">-->
<!--          <template slot-scope="scope">-->
<!--            <el-tag :type="statusFilterType(scope.row.level)">{{ levelMap[scope.row.level] }}</el-tag>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="状态" prop="status" show-overflow-tooltip width="120">
          <template slot-scope="scope">
            <el-tag>{{ statusMap[scope.row.status] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="auditOrder(scope.row)"
            >审核
            </el-button
            >
          </template>
        </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>

    <el-dialog
        title="审核巡检计划"
        :visible.sync="formVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="65%"
        v-if="formVisible"
    >
      <deviceInspectPlanAuditModify
          :inspectPlan="deviceInspectPlan"
          :closeDialog="closeDialog"
      >
      </deviceInspectPlanAuditModify>
    </el-dialog>

    <el-card class="el-card">
      <el-table v-loading="loading" :data="delectList" style="width: 100%" border highlight-current-row>
        <el-table-column label="序号" type="index" width="60"></el-table-column>
        <el-table-column label="项目内容" prop="content" width="300"></el-table-column>
        <el-table-column label="备注" prop="remark" show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="datailSelect(scope.row)"
            >详情
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      title="巡检配置详情"
      :visible.sync="deviceProjectSelectVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="40%"
      v-if="deviceProjectSelectVisible"
    >
      <DeviceInspectPlanProjectSet
        :detailQuery="detailQuery"
        :closeDialog="closeDialog"
      >
      </DeviceInspectPlanProjectSet>
    </el-dialog>


  </div>
</template>

<script>
import {getDeptListByUser} from "@/api/auth/authDept";
import {findInvByUser} from "@/api/system/invSubWarehouse";
import {getDeviceInspectPlanList, deleteInspectPlan, updateStatus} from "@/api/dev/deviceInspectPlan";
import deviceInspectPlanAuditModify from "@/views/dev/DeviceInspectPlanAuditModify"
import {selectDeviceInspectPlanDelect} from "@/api/dev/deviceInspectPlanDelect";
import DeviceInspectPlanProjectSet from "@/views/dev/DeviceInspectProjectSelect";



export default {
  name: "DeviceInspectPlanNew",
  data() {
    return {
      filterQuery: {
        orderId: null,
        deptCode: this.$store.getters.locDeptCode,
        invCode: null,
        code: null,
        status: 1,
        page: 1,
        limit: 20,
      },
      delectList:[],
      list: [],
      total: 0,
      deptList: [],
      invList: [],
      loading: false,
      formVisible: false,
      statusMap: {
        0: "草稿",
        1: "未审核",
        2: "已审核"
      },
      levelMap: {
        1: "低级",
        2: "中级",
        3: "高级"
      },
      planStatusMap: {
        1: "未启用",
        2: "运行中",
        3: "已结束",
        4: "已过期"
      },
      frequencyUnitMap: {
        1: "年",
        2: "月",
        3: "日",
        4: "小时",
        5: "分钟",
        6: "秒",
      },
      formName: null,
      formMap: {
        add: "新增设备巡检计划",
        edit: "编辑设备巡检计划"
      },
      deviceInspectPlan: {
        id: null,
        code: null,
        deptCode: null,
        invCode: null,
        deviceName: null,
        orderId: null,
        endTime: '',
        startTime: '',
        expectedTime:null
      },
      deviceProjectSelectVisible: false,
      detailQuery: null,
      showSearch: true,
      actDateRange: [],
      endDateRange: [],
      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]);
            },
          },
        ],
      },

      pickerAuditOptions: {
        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: {
    statusFilterType(status) {
      const statusMap = {
        1: "success",
        2: "warning",
        3: "danger",
      };
      return statusMap[status];
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        orderId: null,
        deptCode: this.$store.getters.locDeptCode,
        invCode: null,
        code: null,
        status: 1,
        page: 1,
        limit: 20,
      };
      this.endDateRange = [];
      this.actDateRange = [];
      this.spaceList = [];
      this.getList();
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      if (this.actDateRange != null) {
        this.filterQuery.ksStartDate = this.actDateRange[0];
        this.filterQuery.ksEndDate = this.actDateRange[1];
      } else {
        this.filterQuery.ksStartDate = null;
        this.filterQuery.ksEndDate = null;
      }
      if (this.endDateRange != null) {
        this.filterQuery.jsStartDate = this.endDateRange[0];
        this.filterQuery.jsEndDate = this.endDateRange[1];
      } else {
        this.filterQuery.jsStartDate = null;
        this.filterQuery.jsEndDate = null;
      }
      getDeviceInspectPlanList(this.filterQuery).then((res) => {
        this.loading = false;
        if (res.code === 20000) {
          this.list = res.data.list || [];
          this.total = res.data.total || 0;
          this.delectList = []
        } 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;
      })
    },
    deptChange() {
      this.invList = [];
      this.filterQuery.invCode = null;
      this.getInvList();
    },
    getDeptList() {
      getDeptListByUser().then((res) => {
        this.deptList = res.data || [];
        this.getInvList();
        this.getList();
      });
    },
    invChange() {
      this.spaceList = [];
      this.filterQuery.fromInvSpaceCode = null;
      this.getSpaceList();
    },
    getInvList() {
      let params = {deptCode: this.filterQuery.deptCode};
      findInvByUser(params)
        .then((response) => {
          this.invList = response.data || [];
          this.getList();
        })
        .catch(() => {
        });
    },

    datailSelect(row){
      this.detailQuery = row
      this.deviceProjectSelectVisible = true
    },

    auditOrder(row) {
      this.formVisible = true;
      this.deviceInspectPlan = row;
    },
    deleteDialog(rowId) {
      this.$confirm('此操作将永久删除该巡检计划, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {id: rowId};
        this.loading = true;
        deleteInspectPlan(params).then((res) => {
          this.loading = false;
          if (res.code === 20000) {
            this.$message.success("删除成功！");
            this.getList();
          } else {
            this.$message.error(res.message);
          }
        }).catch(() => {
          this.loading = false;
        });
      });
    },
    handleDetail(row){
      this.selectDeviceInspectPlanDelect({orderIdFk:row.orderId})
    },
    selectDeviceInspectPlanDelect(obj) {
      selectDeviceInspectPlanDelect(obj).then((res) => {
        this.loading = false;
        if (res.code === 20000) {
          this.delectList = res.data || [];
        } else {
          this.$message.error(res.message);
          this.delectList = [];
        }
      }).catch((error) => {
        this.loading = false;
        this.$message.error(error.message);
        this.delectList = [];
      })
    },
    closeDialog() {
      this.formVisible = false;
      this.getList();
    },
  },
  components: {deviceInspectPlanAuditModify,DeviceInspectPlanProjectSet},
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    this.getDeptList();
  },
};
</script>

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

