<template>
<!--  <el-card style="margin: 5px;margin-top: -20px">-->
    <el-form :model="formData" ref="dataForm" :rules="rules" label-width="100px" style="margin-bottom: -15px">
      <el-button-group style="display: flex;margin: 0px 20px 30px 85%; height: 35px;">
        <el-button
          type="primary"
          @click.native="submit()"
        >保存
        </el-button>
      </el-button-group>

      <el-row>
        <el-col :span="12">
          <el-form-item class="query-form-item" label="资产品名:" style="width: 91%">
            <el-select v-model="formData.assetName" style="width: 100%" placeholder="请选择资产品名" >
              <el-option
                v-for="item in dict.type.device_source_type"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                <span style="float: left">{{ item.label }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="query-form-item" label="资产备用类型:" style="width: 91%">
            <el-select v-model="formData.assetReserveType" style="width: 100%" placeholder="请选择资产备用类型" >
              <el-option
                v-for="item in dict.type.device_source_type"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                <span style="float: left">{{ item.label }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <el-form-item class="query-form-item" label="是否强检:" >
            <el-checkbox v-model="formData.isImperative" ></el-checkbox>
          </el-form-item>
        </el-col>
        <el-col :span="1"><el-form-item></el-form-item></el-col>
        <el-col :span="7">
          <el-form-item  class="query-form-item"  label="检定周期(月):">
            <el-input v-model="formData.imperativeCycle" style="width: 80%"
                      clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item  class="query-form-item"  label="开始检定日期:">
            <el-date-picker
              style="width: 90%"
              v-model="formData.startImperativeDate"
              type="date"
              placeholder="请输入开始检定日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="4">
          <el-form-item class="query-form-item" label="是否保养:" >
            <el-checkbox v-model="formData.isMaintain" ></el-checkbox>
          </el-form-item>
        </el-col>
        <el-col :span="1"><el-form-item></el-form-item></el-col>
        <el-col :span="7">
          <el-form-item  class="query-form-item"  label="保养周期(月):">
            <el-input v-model="formData.maintainCycle" style="width: 80%"
                      clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item  class="query-form-item"  label="开始保养日期:">
            <el-date-picker
              style="width: 90%"
              v-model="formData.startMaintainDate"
              type="date"
              placeholder="请输入开始保养日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item class="query-form-item" label="维修组:" style="width: 91%">
            <el-select v-model="formData.serviceType" style="width: 100%" placeholder="请选择维修组" >
              <el-option
                v-for="item in dict.type.device_source_type"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                <span style="float: left">{{ item.label }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="query-form-item" label="保养组:" style="width: 91%">
            <el-select v-model="formData.maintainType" style="width: 100%" placeholder="请选择保养组" >
              <el-option
                v-for="item in dict.type.device_source_type"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                <span style="float: left">{{ item.label }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item class="query-form-item" label="管理人:" style="width: 91%">
            <el-select v-model="formData.managerUser" style="width: 100%" placeholder="请选择管理人" >
              <el-option v-for="item in userList"
                         :key="item.employeeName"
                         :label="item.employeeName"
                         :value="item.userId">
                <span>{{ item.employeeName }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="query-form-item" label="审核人:" style="width: 91%">
            <el-select v-model="formData.approveUser" style="width: 100%" placeholder="请选择审核人" >
                  <el-option v-for="item in userList"
                             :key="item.employeeName"
                             :label="item.employeeName"
                             :value="item.userId">
                    <span>{{ item.employeeName }}</span>
                  </el-option>
                </el-select>
          </el-form-item>
        </el-col>
      </el-row>


      <el-row>
        <el-col :span="12">
          <el-form-item  class="query-form-item"  label="对应会计科目:">
            <el-input v-model="formData.ledgerAccount" style="width: 90%"
                      clearable></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item  class="query-form-item"  label="减值准备:">
            <el-input v-model="formData.impairmentProvision" style="width: 90%"
                      clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item  class="query-form-item"  label="预计工作量:">
            <el-input v-model="formData.estimatedWorkload" style="width: 90%"
                      clearable></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item  class="query-form-item"  label="已完成工作量:">
            <el-input v-model="formData.completedWorkload" style="width: 90%"
                      clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item class="query-form-item" label="维保类型:" style="width: 91%">
            <el-select v-model="formData.maintenanceType" style="width: 100%" placeholder="请选择维保类型" >
              <el-option
                v-for="item in dict.type.device_source_type"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                <span style="float: left">{{ item.label }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item  class="query-form-item"  label="维保周期(月):">
            <el-input-number :min="1"  v-model="formData.maintenanceCycle"  style="width: 90%" controls-position="right"
                             clearable></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>


      <el-row>
        <el-col :span="12">
          <el-form-item  class="query-form-item"  label="维保开始日期:">
            <el-date-picker
              style="width: 90%"
              v-model="formData.startMaintenancDate"
              type="date"
              placeholder="请输入维保开始日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item  class="query-form-item"  label="维保结束日期:">
            <el-date-picker
              style="width: 90%"
              v-model="formData.endMaintenancDate"
              type="date"
              placeholder="请输入维保结束日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item class="query-form-item" label="内外网:" style="width: 91%">
              <input type="radio" name="networkType" value="1" v-model="formData.networkType"/>内网
              <input type="radio" name="networkType" value="2" v-model="formData.networkType"/>外网
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="query-form-item" label="使用角色:" style="width: 91%">
            <input type="radio" name="userRole" value="1" v-model="formData.userRole"/>医生站
            <input type="radio" name="userRole" value="2" v-model="formData.userRole"/>护士站
          </el-form-item>
        </el-col>

      </el-row>

      <el-row>
        <el-col :span="5">
          <el-form-item class="query-form-item" label="是否加域:" >
            <el-checkbox v-model="formData.isAddDomain" style="width: 90%"></el-checkbox>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item class="query-form-item" label="u盘是否禁用:" >
            <el-checkbox v-model="formData.isUDisc" style="width: 90%"></el-checkbox>
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="query-form-item" label="设备归类:" style="width: 91%">
            <input type="radio" name="ascriptionType" value="1" v-model="formData.ascriptionType"/>专用设备
            <input type="radio" name="ascriptionType" value="2" v-model="formData.ascriptionType"/>通用办公设备
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>

</template>

<script>
import {addDeptDevice, saveDeptDevice, updateDeptDevice} from "@/api/dev/deptDevice";
import {isBlank} from "@/utils/strUtil";
import {getDeptListByUser} from "@/api/auth/authDept";
import {getInvListByUser} from "@/api/system/invWarehouse";
import {listDeptUser} from "@/api/system/deptUser";
import {genDeviceCode} from "@/api/dev/deviceInfoApi";

export default {
  props: {
    rowData: {
      type: Object,
      required: true
    },
    editSaveDev: {
      type: Function,
      required: true
    },
    maintainsList: {
      type: Array,
      required: true
    }
  },
  name: "deptDeviceModifyDialog",
  dicts: ['device_source_type'],
  data() {
    return {
      deptList: [],
      invList: [],
      userList: [],
      formData: {},
      rules: {
        // deptCode: [
        //   {required: true, message: '请选择部门', trigger: 'change'}
        // ],
        // invCode: [
        //   {required: true, message: '请选择仓库', trigger: 'change'}
        // ],
        // deviceName: [
        //   {required: true, message: '请输入设备名称', trigger: 'blur'}
        // ],
        // originCode: [
        //   {required: true, message: '请输入原始编码', trigger: 'blur'}
        // ]
      }
    };
  },
  methods: {
    supChange(val) {
      this.formData.supName = this.maintainsList.find(item => item.id == val).name
      this.formData.supId = val
    },
    deptChange() {
      this.formData.invCode = null;
      this.formData.manager = null;
      this.invList = [];
      this.getInvList();
      this.getDeptUserList();
    },
    getDeptList() {
      getDeptListByUser().then((res) => {
        this.deptList = res.data || [];
        this.getInvList();
        this.getDeptUserList();
      });
    },
    getInvList() {
      let params = {deptCode: this.formData.deptCode};
      getInvListByUser(params)
        .then((response) => {
          this.invList = response.data || [];
          this.getList();
        })
        .catch(() => {
        });
    },
    getDeptUserList() {
      this.userList = [];
      let params = {deptId: null};
      this.deptList.forEach((item) => {
        if (item.code === this.formData.deptCode) {
          params.deptId = item.id;
        }
      });
      listDeptUser(params).then((res) => {
        this.userList = res.data.list || [];
      })
    },
    submit() {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          this.$message.error("请完善必填信息");
        } else {
          this.rowData.assetName = this.formData.assetName;
          this.rowData.isImperative = this.formData.isImperative;
          this.rowData.isMaintain = this.formData.isMaintain;
          this.rowData.imperativeCycle = this.formData.imperativeCycle;
          this.rowData.maintainCycle = this.formData.maintainCycle;
          this.rowData.startImperativeDate = this.formData.startImperativeDate;
          this.rowData.startMaintainDate = this.formData.startMaintainDate;
          this.rowData.serviceType = this.formData.serviceType;
          this.rowData.maintainType = this.formData.maintainType;
          this.rowData.managerUser = this.formData.managerUser;
          this.rowData.approveUser = this.formData.approveUser;
          this.rowData.ledgerAccount = this.formData.ledgerAccount;
          this.rowData.impairmentProvision = this.formData.impairmentProvision;
          this.rowData.estimatedWorkload = this.formData.estimatedWorkload;
          this.rowData.completedWorkload = this.formData.completedWorkload;
          this.rowData.maintenanceType = this.formData.maintenanceType;
          this.rowData.maintenanceCycle = this.formData.maintenanceCycle;

          this.rowData.startMaintenancDate = this.formData.startMaintenancDate;
          this.rowData.endMaintenancDate = this.formData.endMaintenancDate;
          this.rowData.networkType = this.formData.networkType;
          this.rowData.userRole = this.formData.userRole;
          this.rowData.isAddDomain = this.formData.isAddDomain;
          this.rowData.isUDisc = this.formData.isUDisc;
          this.rowData.ascriptionType = this.formData.ascriptionType;
          this.rowData.assetReserveType = this.formData.assetReserveType;

          this.editSaveDev();

        }
      })
    },
    genCode(row) {
      let loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      genDeviceCode().then(res => {
        loading.close()
        if (res.code != 20000) {
          this.$message.error(res.message)
          return
        }
        this.formData.deviceCode = res.data
      }).catch(e => {
        loading.close()
      })
    },
  },
  created() {
    console.log(this.rowData);
    console.log(this.maintainsList.length);
    this.formData = JSON.parse(JSON.stringify(this.rowData));
    console.log(this.formData);
  },
}
</script>

<style scoped>

</style>
