<template>
  <div>
    <el-card style="margin: 5px;margin-top: -20px">
      <el-form :model="inspectOrder" ref="dataForm" label-width="100px" style="margin-bottom: -15px">
        <el-button-group style="display: flex;margin: 0px 0 10px 80%; height: 35px">
          <el-button
            type="primary"
            v-if="inspectOrder.id === null"
            @click.native="selectDevice()"
          >选择设备
          </el-button>
          <el-button
            type="primary"
            @click.native="submit()"
          >保存
          </el-button>
        </el-button-group>

        <el-row>
          <el-col :span="10">
            <el-form-item class="query-form-item" label="部门:">
              <el-select v-model="inspectOrder.deptCode"
                         placeholder="请选择部门"
                         clearable="true"
                         @change="deptChange"
                         :disabled="ischeck"
                         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="仓库:">
              <el-select v-model="inspectOrder.invCode"
                         placeholder="请选择仓库"
                         clearable="true"
                         :disabled="isDeviceNameEmpty||ischeck"
                         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="设备名称:">
              <el-input v-model="inspectOrder.deviceName" placeholder="请输入设备名称" style="width: 90%"
                        disabled
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="物资编码:">
              <el-input v-model="inspectOrder.code" placeholder="请输入编码" style="width: 90%" clearable
                        disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="维保类型:">
              <el-select v-model="inspectOrder.type"
                         placeholder="请选择部门"
                         clearable="true"
                         disabled
                         style="width: 90%"
              >
                <el-option :value="1" label="报修"></el-option>
                <el-option :value="2" label="计划"></el-option>
                <el-option :value="3" label="手动"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="设备状态:">
              <el-select v-model="inspectOrder.deviceStatus"
                         placeholder="请选择设备状态"
                         style="width: 90%"
              >
                <el-option :value="0" label="异常"></el-option>
                <el-option :value="1" label="正常"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="18">
            <el-form-item class="query-form-item" label="备注:">
              <el-input v-model="inspectOrder.remark" type="textarea" style="width: 100%" auto-complete="true" autosize></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>


    <el-dialog
      title="选择维保设备"
      :visible.sync="deviceDialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="65%"
      v-if="deviceDialogVisible"
      append-to-body
    >
      <el-form class="query-form" :model="filterQuery" :inline="true" label-width="100px">
        <el-form-item class="query-form-item" label="部门:">
          <el-input v-model="deptName" disabled></el-input>
        </el-form-item>
        <el-form-item class="query-form-item" label="仓库:">
          <el-input v-model="invName" disabled></el-input>
        </el-form-item>
        <el-form-item class="query-form-item" label="资产编码:">
          <el-input v-model="filterQuery.code" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button-group>
            <el-button type="primary" icon="el-icon-refresh" @click="onReset"></el-button>
            <el-button type="primary" icon="search" @click="queryDevice"
            >查询
            </el-button
            >
            <el-button type="primary" icon="search" @click="combine">确定</el-button>
          </el-button-group>
        </el-form-item>
      </el-form>

      <el-table v-loading="deviceLoading" :data="deviceList" style="width: 100%"
                highlight-current-row
                @current-change="handleCurrentChange"
      >
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="设备名称" prop="deviceName" width="200"></el-table-column>
        <el-table-column label="资产编码" prop="code" width="200"></el-table-column>
        <el-table-column label="规格型号" prop="ggxh" width="120"></el-table-column>
        <el-table-column label="批次号" prop="batchNo" width="120"></el-table-column>
        <el-table-column label="生产日期" prop="productionDate" width="120"></el-table-column>
        <el-table-column label="失效日期" prop="expireDate" width="120"></el-table-column>
        <el-table-column label="生产厂家" prop="manufactory" show-overflow-tooltip></el-table-column>
        <el-table-column label="供应商" prop="supName" show-overflow-tooltip></el-table-column>
        <el-table-column label="注册证号" prop="zczbhhzbapzbh" width="150" show-overflow-tooltip></el-table-column>
      </el-table>

      <pagination
        v-show="deviceTotal>0"
        :total="deviceTotal"
        :limit.sync="filterQuery.limit"
        :page.sync="filterQuery.page"
        @pagination="getDeviceList"
      ></pagination>
    </el-dialog>
  </div>

</template>

<script>
import {isBlank} from "@/utils/strUtil";
import {getDeptListByUser} from "@/api/auth/authDept";
import {getInvListByUser} from "@/api/system/invWarehouse";
import {getDeptDeviceList} from "@/api/dev/deptDevice";
import {saveDeviceInspectOrder} from "@/api/dev/deviceInspectOrder";


export default {
  props: {
    inspectOrder: {
      type: Object,
      required: true
    },
    closeDialog: {
      type: Function,
      required: true
    }
  },
  name: "DeviceInspectOrderModify",
  data() {
    return {
      deptList: [],
      invList: [],
      isDeviceNameEmpty: false,
      ischeck: true,
      deviceDialogVisible: false,
      deviceLoading: false,
      filterQuery: {
        code: null,
        deptCode: null,
        invCode: null,
        page: 1,
        limit: 10
      },
      deviceList: [],
      deviceTotal: 0,
      deptName: null,
      invName: null,
      selectRow: null,
      check: false,
    };
  },

  methods: {

    deptChange() {
      if(this.inspectOrder.id == null){
        this.inspectOrder.deviceName=null;
        this.inspectOrder.code=null;
      }
      this.inspectOrder.invCode = null;
      this.invList = [];
      this.isDeviceNameEmpty=false;
      this.getInvList();
    },
    getDeptList() {
      getDeptListByUser().then((res) => {
        this.deptList = res.data || [];
        this.getInvList();
      });
    },
    getInvList() {
      let params = {deptCode: this.inspectOrder.deptCode};
      getInvListByUser(params)
        .then((response) => {
          this.invList = response.data || [];
          this.getList();
        })
        .catch(() => {
        });
    },
    submit() {
      if (isBlank(this.inspectOrder.deptCode) || isBlank(this.inspectOrder.invCode) || isBlank(this.inspectOrder.code)) {
        this.$message.error("请选择设备相关信息")
        return;
      }
      let formName = isBlank(this.inspectOrder.id) ? "add" : "edit";
      saveDeviceInspectOrder(this.inspectOrder, formName).then((res) => {
        if (res.code === 20000) {
          this.$message.success("添加成功");
          this.closeDialog();
        } else {
          this.$message.error(res.message);
        }
      }).catch((error) => {
        this.$message.error(error.message);
      });
    },
    selectDevice() {
      if (isBlank(this.inspectOrder.deptCode) || isBlank(this.inspectOrder.invCode)) {
        this.$message.warning("请先选择维保设备所属部门和仓库");
        return;
      }
      this.setDeptAnInvName();
      this.check = false
      this.deviceDialogVisible = true;
      this.filterQuery = {
        code: null,
        deptCode: this.inspectOrder.deptCode,
        invCode: this.inspectOrder.invCode,
        page: 1,
        limit: 10
      }
      this.getDeviceList();
    },
    getDeviceList() {
      this.deviceLoading = true;
      this.deviceList = [];
      getDeptDeviceList(this.filterQuery).then((res) => {
        this.deviceLoading = false;
        if (res.code === 20000) {
          this.deviceList = res.data.list || [];
          this.deviceTotal = res.data.total || 0;
        } else {
          this.$message.error(res.message);
          this.deviceList = [];
          this.deviceTotal = 0;
        }
      }).catch((error) => {
        this.deviceLoading = false;
        this.deviceList = [];
        this.deviceTotal = 0;
      });
    },
    setDeptAnInvName() {
      this.deptList.forEach((item) => {
        if (item.code === this.inspectOrder.deptCode) {
          this.deptName = item.name;
        }
      });

      this.invList.forEach((item) => {
        if (item.code === this.inspectOrder.invCode) {
          this.invName = item.name;
        }
      })
    },
    onReset() {
      this.filterQuery.code = null;
      this.filterQuery.page = 1;
      this.filterQuery.limit = 10;
      this.getDeviceList();
    },
    queryDevice() {
      this.filterQuery.page = 1;
      this.getDeviceList();
    },
    handleCurrentChange(row) {
      this.check = !this.check
      this.selectRow = row;
    },
    combine() {
      if (this.check == false) {
        this.$message.error("请选择设备!")
        return;
      }
      this.deviceDialogVisible = false;
      this.inspectOrder.code = this.selectRow.code;
      this.inspectOrder.deviceName = this.selectRow.deviceName;
      this.isDeviceNameEmpty=true;
    },
  },
  created() {
    this.getDeptList();
  },
}
</script>

<style scoped>

</style>
