<template>
  <el-dialog :title="!id ? '新增' : '编辑设备信息'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :inline="true">
      <el-form-item label="类型">
        <el-select v-model="type" :disabled="departmentReaderOnly" @change="changeType"></el-select>
      </el-form-item>

      <el-form-item label="品牌">
        <el-select
          v-model="brandName"
          :disabled="departmentReaderOnly"
          placeholder="请选择"
          @change="changeBrand"
        ></el-select>
      </el-form-item>
    </el-form>
    <el-form>
      <el-form-item label="规格">
        <el-select style="width:500px"
          v-model="specType"
          :disabled="departmentReaderOnly"
          @change="changeType"
          
        ></el-select>
      </el-form-item>
    </el-form>
    <el-form :inline="true">
      <el-form-item label="使用方">
        <el-input v-model="user" placeholder="使用方" clearable :disabled="departmentReaderOnly"></el-input>
      </el-form-item>
      <el-form-item label="现有标识">
        <el-input v-model="code" placeholder="现有编码" clearable :disabled="departmentReaderOnly"></el-input>
      </el-form-item>
    </el-form>
    <el-form :inline="true">
      <el-form-item label="状态">
        <el-select v-model="equipmentState" :disabled="departmentReaderOnly" @change="changeType">
          <el-option
            v-for="item in stateList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="自带标识">
        <el-input v-model="originalCode" placeholder="设备自带标识" clearable></el-input>
      </el-form-item>
    </el-form>
    <el-form :inline="true">
      <el-form-item label="出库时间">
        <el-date-picker
          v-model="outTime"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetime"
          default-time="outTime"
          placeholder="选择日期时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="入库时间">
        <el-date-picker v-model="inTime" type="datetime" default-time="inTime" placeholder="选择日期时间" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
    </el-form>
    <el-form :inline="true">
      <el-form-item label="备注">
        <el-input v-model="mark" placeholder="备注信息" clearable></el-input>
      </el-form-item>
      <el-form-item label="买入金额">
        <el-input v-model="buyAmount" placeholder="采购金额" clearable></el-input>
      </el-form-item>
    </el-form>
    <el-form :inline="true">
      <el-form-item label="出售金额">
        <el-input v-model="saleAmount" placeholder="出售时金额" clearable></el-input>
      </el-form-item>
      <el-form-item label="审批编号">
        <el-input v-model="no" placeholder="购买审批编号" clearable></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="warning" @click="save(0)">取消</el-button>
      <el-button type="primary" @click="save(1)">保存</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { apiEditUseDevice } from "@/api/api_equipment";
export default {
  data() {
    return {
      visible: false,
      types: [],
      brands: [],
      specs: [],
      id: null,
      type: null,
      brandName: null,
      inTime: null, //入库时间
      outTime: null, //出库时间
      specType: null,
      num: 1, //
      user: null, //
      mark: null, //备注
      code: null,
      equipmentState: null, //状态
      buyAmount: null, //买入金额
      saleAmount: null, //出售金额
      departmentReaderOnly: true,
      originalCode: null,
      no: null, //审批流水
      isAdd: false,
      stateList: [
        { value: 0, label: "入库" },
        { value: 1, label: "售出" },
        { value: 2, label: "不可用" },
        { value: 3, label: "调拨" },
        { value: 4, label: "申领" },
      ],
      specReq: {
        page: 1,
        rows: 10,
        sort: null,
        order: null,
        id: null,
        isDelete: 0,
        gmtCreate: null,
        gmtModified: null,
        specType: null,
        type: null,
        num: null,
        unit: null, //单位 件/台
        user: null, //使用者
        owner: null, //所有者
        qr: null, //设备本身标识
        originalCode: null, //设备原有标识，老标识
        code: null, //设备现有标识
        mark: null, //备注
        gmtOutbound: null, // 最后一次出库时间
        gmtInbound: null, //第一次入库时间
        equipmentState: null, //状态
        buyAmount: null, //买入金额
        saleAmount: null, //出售金额
        no: null, //审批流水
        brandId: null,
        typeId: null,
        gmtInStartTime: null,
        gmtInEndTime: null,
        gmtOutStartTime: null,
        gmtOutEndTime: null,
        count: null,
        other: null,
      },
    };
  },
  mounted() {},
  computed: {},
  methods: {
    async init(obj, types, brands) {
      console.log("device-list-detail init");
      console.log(obj);
      this.id = obj.id;
      this.brandName = obj.brandName;
      this.code = obj.code;
      this.equipmentState = obj.equipmentState;
      this.mark = obj.mark;
      this.no = obj.no;
      this.num = obj.num;
      this.originalCode = obj.originalCode;
      this.saleAmount = obj.saleAmount;
      this.buyAmount = obj.buyAmount;
      this.specType = obj.specType;
      this.type = obj.type;
      this.user = obj.user;
      this.inTime = obj.gmtInbound;
      this.outTime = obj.gmtOutbound;
      this.visible = true;
    },

    save(val) {
      if(val == 0){
        this.visible = false
        return
      }

      
      this.specReq.id = this.id
      this.specReq.buyAmount = this.buyAmount;
      this.specReq.saleAmount = this.saleAmount;
      this.specReq.gmtInbound = this.inTime;
      this.specReq.gmtOutbound = this.outTime;
      this.specReq.mark = this.mark;
      this.specReq.originalCode = this.originalCode;
      this.specReq.no = this.no;
      this.editSpecs();
      
    },
    editSpecs() {
      console.log("editSpecs");
      apiEditUseDevice(this.specReq).then((data) => {
        console.log(data);
        if (data && data.code === 0) {
          this.visible = false;
          this.$message("更新成功");
          this.$emit("refreshDataList");
        } else {
          this.$message.error(data.msg);
        }
      });
    },
  },
};
</script>