<template>
  <div>
    <el-card style="margin-top: -10px">
      <el-form :model="inspectPlan" ref="formRef" :rules="formRules" label-width="120px" style="margin-bottom: -15px">
        <el-button-group style="display: flex;margin: 0px 0 10px 85%; height: 35px">
          <el-button type="primary" @click.native="submit(2)">审核通过</el-button>
          <el-button type="primary" @click.native="submit(3)">审核拒绝</el-button>
        </el-button-group>

        <el-row>
          <el-col :span="10">
            <el-form-item class="query-form-item" label="部门:" prop="deptCode">
              <el-select v-model="inspectPlan.deptCode" placeholder="请选择部门" clearable="true"
                         :disabled="inspectPlan.id !== null" @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="10">
            <el-form-item class="query-form-item" label="仓库:" prop="invCode">
              <el-select v-model="inspectPlan.invCode" :disabled="inspectPlan.id !== null" 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-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="设备名称:" prop="deviceName">
              <el-input v-model="inspectPlan.deviceName" disabled placeholder="请输入设备名称" style="width: 90%"
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="物资编码:" prop="code">
              <el-input v-model="inspectPlan.code" disabled placeholder="请输入编码" style="width: 90%"
                        clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="10">
            <el-form-item label="计划开始时间:" prop="startTime">
              <el-date-picker
                  disabled
                  v-model="inspectPlan.startTime"
                  type="datetime"
                  placeholder="选择计划开始时间"
                  style="width: 90%"
                  :picker-options="pickerOptionsStart"
                  :clearable="false"
                  value-format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="计划结束时间:" prop="endTime">
              <el-date-picker
                  disabled
                  v-model="inspectPlan.endTime"
                  type="datetime"
                  :picker-options="pickerOptionsEnd"
                  placeholder="选择计划结束时间"
                  style="width: 90%"
                  :clearable="false"
                  value-format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <el-form-item label="时间频率:" prop="frequency">
              <el-input v-model="inspectPlan.frequency" disabled type="number" placeholder="请选择时间频率"
                        style="width: 100%"
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="单位:" label-width="73px" prop="frequencyUnit">
              <el-select v-model="inspectPlan.frequencyUnit" disabled style="width: 64%" placeholder="单位" clearable>
                <el-option label="年" :value=1></el-option>
                <el-option label="月" :value=2></el-option>
                <el-option label="日" :value=3></el-option>
                <el-option label="小时" :value=4></el-option>
                <el-option label="分钟" :value=5></el-option>
                <el-option label="秒" :value=6></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="计划状态:" prop="planStatus">
              <el-select v-model="inspectPlan.planStatus" style="width: 90%" placeholder="请选择计划级别" clearable>
                <el-option label="不启用" :value=1></el-option>
                <el-option label="启用" :value=2></el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--          <el-col :span="10">-->
<!--            <el-form-item label="计划级别:" prop="level">-->
<!--              <el-select v-model="inspectPlan.level" style="width: 90%" disabled 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-row>
      </el-form>
    </el-card>

    <el-card>

      <el-table :data="delectList" style="width: 100%;" border highlight-current-row max-height="300" height="500"
                ref="multipleTable">
        <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>
    </el-card>

  </div>

</template>

<script>
import {isBlank} from "@/utils/strUtil";
import {getDeptListByUser} from "@/api/auth/authDept";
import {getInvListByUser, warehouseUserList} from "@/api/system/invWarehouse";
import {updateStatus} from "@/api/dev/deviceInspectPlan";
import {getDeptDeviceList} from "@/api/dev/deptDevice";
import deviceInspectPlanProjectSet from "@/views/dev/DeviceInspectPlanProjectSet"
import {
  selectDeviceInspectPlanDelect,
  uploadDeviceInspectPlanDelect,
  delectDeviceInspectPlanDelect
} from "@/api/dev/deviceInspectPlanDelect";


export default {
  props: {
    inspectPlan: {
      type: Object,
      required: true
    },
    closeDialog: {
      type: Function,
      required: true
    }
  },
  name: "DeviceInspectPlanAuditModify",
  data() {
    return {
      deptList: [],
      invList: [],
      userList: [],
      deviceDialogVisible: false,
      deviceLoading: false,
      filterQuery: {
        code: null,
        deptCode: null,
        invCode: null,
        page: 1,
        limit: 10
      },
      loading: false,
      inputQuery: {
        id: null,
        orderIdFk: null
      },
      deviceList: [],
      delectList: [],
      deviceTotal: 0,
      deptName: null,
      invName: null,
      selectRow: null,
      inspectPlanSetVisible: false,
      // 开始时间限制
      pickerOptionsStart: {
        disabledDate: (time) => {
          if (this.inspectPlan.endTime) {
            return time.getTime() > new Date(this.inspectPlan.endTime).getTime() || time.getTime() <= new Date(this.inspectPlan.createDate).getTime() - 86400000;
          }
          return time.getTime() <= new Date(this.inspectPlan.createDate).getTime() - 86400000;
        },
      },
      // 结束时间限制
      pickerOptionsEnd: {
        disabledDate: (time) => {
          if (this.inspectPlan.startTime) {
            return time.getTime() < new Date(this.inspectPlan.startTime).getTime();
          }
          return time.getTime() <= new Date(this.inspectPlan.createDate).getTime() - 86400000;
        },
      },
      // 开始时间限制
      pickerOptionsExpected: {
        disabledDate: (time) => {
          return time.getTime() <= new Date(this.inspectPlan.createDate).getTime() - 86400000;
        },
      },
    };
  },
  methods: {
    deptChange() {
      this.inspectPlan.invCode = null;
      this.invList = [];
      this.getInvList();
    },
    getDeptList() {
      getDeptListByUser().then((res) => {
        this.deptList = res.data || [];
        this.getInvList();
      });
    },
    getInvList() {
      let params = {deptCode: this.inspectPlan.deptCode};
      getInvListByUser(params)
          .then((response) => {
            this.invList = response.data || [];
            this.getList();
          })
          .catch(() => {
          });
    },
    submit(status) {
      //判断巡检人要填
      if(status == 2 && this.inspectPlan.inspectUser == null){
        return   this.$message.error("负责人不能为空");
      }
      this.inspectPlan.status=status
      updateStatus(this.inspectPlan).then((res) => {
        if (res.code === 20000) {
          this.$message.success("审核成功");
          this.closeDialog();
        } else {
          this.$message.error(res.message);
        }
      }).catch((error) => {
        this.$message.error(error.message);
      });
    },
    combine() {
      this.deviceDialogVisible = false;
      this.inspectPlan.code = this.selectRow.code;
      this.inspectPlan.deviceName = this.selectRow.deviceName;
    },

    //!--------------------------------------------------------------   新加方法           --------------------------------------------------------- //
    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 = [];
      })
    },
    getUserBus() {
      let query = {
        code: this.inspectPlan.invCode,
      }
      //加载用户数据
      warehouseUserList(query).then((res) => {
        this.userList = [];
        this.userList = res.data.list;
      }).catch((error) => {
        this.$message.error("用户数据加载失败")
      });
    },
  }
  ,
  components: {
    deviceInspectPlanProjectSet
  }
  ,
  created() {
    this.inspectPlan.createDate = new Date().getTime();
    this.inspectPlan.inspectUser = ""
    this.getDeptList();
    this.getUserBus();
    if (!isBlank(this.inspectPlan.id)) {
      this.selectDeviceInspectPlanDelect({orderIdFk: this.inspectPlan.orderId});
    }
  }
  ,
}
</script>

<style scoped>

</style>
