<template>
  <div>
    <el-card style="margin: 5px;margin-top: -20px">
      <el-form :model="repairOrder" 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"
            @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="repairOrder.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="10">
            <el-form-item class="query-form-item" label="仓库:">
              <el-select v-model="repairOrder.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-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="设备名称:">
              <el-input v-model="repairOrder.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="repairOrder.code" placeholder="请输入编码" style="width: 90%" clearable
                        disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="18">
            <el-form-item label="报修说明:">
              <el-input v-model="repairOrder.msg" placeholder="请输入报修信息" style="width: 90%"
                        type="textarea"
                        clearable></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 {saveDeviceRepairOrder} from "@/api/inventory/deviceRepairOrder";
import {getDeptDeviceList} from "@/api/inventory/deptDevice";


export default {
  props: {
    repairOrder: {
      type: Object,
      required: true
    },
    closeDialog: {
      type: Function,
      required: true
    }
  },
  name: "DeviceRepairOrderModify",
  data() {
    return {
      deptList: [],
      invList: [],
      deviceDialogVisible: false,
      deviceLoading: false,
      filterQuery: {
        code: null,
        deptCode: null,
        invCode: null,
        page: 1,
        limit: 10
      },
      deviceList: [],
      deviceTotal: 0,
      deptName: null,
      invName: null,
      selectRow: null
    };
  },
  methods: {
    deptChange() {
      this.repairOrder.invCode = null;
      this.invList = [];
      this.getInvList();
    },
    getDeptList() {
      getDeptListByUser().then((res) => {
        this.deptList = res.data || [];
        this.getInvList();
      });
    },
    getInvList() {
      let params = {deptCode: this.repairOrder.deptCode};
      getInvListByUser(params)
        .then((response) => {
          this.invList = response.data || [];
          this.getList();
        })
        .catch(() => {
        });
    },
    submit() {
      if (isBlank(this.repairOrder.deptCode) || isBlank(this.repairOrder.invCode) || isBlank(this.repairOrder.code)) {
        this.$message.error("请选择设备相关信息")
        return;
      }
      let formName = isBlank(this.repairOrder.id) ? "add" : "edit";
      saveDeviceRepairOrder(this.repairOrder, 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.repairOrder.deptCode) || isBlank(this.repairOrder.invCode)) {
        this.$message.warning("请先选择报修设备所属部门和仓库");
        return;
      }
      this.setDeptAnInvName();
      this.deviceDialogVisible = true;
      this.filterQuery = {
        code: null,
        deptCode: this.repairOrder.deptCode,
        invCode: this.repairOrder.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.repairOrder.deptCode) {
          this.deptName = item.name;
        }
      });

      this.invList.forEach((item) => {
        if (item.code === this.repairOrder.invCode) {
          this.invName = item.name;
        }
      })
    },
    onReset() {
      this.filterQuery.code = null;
      this.filterQuery.page = 1;
      this.getDeviceList();
    },
    queryDevice() {
      this.filterQuery.page = 1;
      this.getDeviceList();
    },
    handleCurrentChange(row) {
      this.selectRow = row;
    },
    combine() {
      this.deviceDialogVisible = false;
      this.repairOrder.code = this.selectRow.code;
      this.repairOrder.deviceName = this.selectRow.deviceName;
    },
  },
  created() {
    this.getDeptList();
  },
}
</script>

<style scoped>

</style>
