<template>
  <div class="customer-css">
    <el-dialog :title="titleCon" :close-on-click-modal="false" :visible.sync="visible" width="960px" style="height: 662px;" class="customer-dialog">
<!--      <el-form :inline="true" label-position="top" :model="pageData" :rules="rules" style="margin-left: 7px;margin-top: -5px;">-->
<!--        <el-form-item label="设备编码" prop="objectID" :rules="rules.objectIdType">-->
<!--          <el-input v-model="pageData.objectID" style="width: 221px"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="设备名称" prop="objectDesc" :rules="rules.objectDescType">-->
<!--          <el-input v-model="pageData.objectDesc" style="width: 221px"></el-input>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <el-form :inline="true" label-position="top" :model="pageData" :rules="rules" style="margin-left: 7px;margin-top: -5px;">-->
<!--        <el-form-item prop="locationID" :rules="rules.locationType">-->
<!--          <span slot="label" @click="getBaseList(26)"><a herf="#">区域编码</a></span>-->
<!--          <el-input v-model="pageData.locationID" style="width: 221px;"></el-input >-->
<!--        </el-form-item>-->
<!--        <el-form-item label="区域名称">-->
<!--          <el-input v-model="pageData.locationName" disabled style="width: 221px"></el-input>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <el-form :inline="true" label-position="top" :model="pageData" :rules="rules" style="margin-left: 7px;margin-top: -5px;">-->
<!--        <el-form-item prop="familyID" :rules="rules.familyType">-->
<!--          <span slot="label" @click="getBaseList(27)"><a herf="#">分类编码</a></span>-->
<!--          <el-input v-model="pageData.familyID" style="width: 221px;" ></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="分类名称">-->
<!--          <el-input v-model="pageData.familyDesc" disabled style="width: 221px"></el-input>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <el-form :inline="true" label-position="top" :model="pageData" :rules="rules" style="margin-left: 7px;margin-top: -5px;">-->
<!--        <el-form-item prop="groupID" :rules="rules.groupType">-->
<!--          <span slot="label" @click="getBaseList(28)"><a herf="#">分组编码</a></span>-->
<!--          <el-input v-model="pageData.groupID" style="width: 221px;" ></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="分组名称">-->
<!--          <el-input v-model="pageData.groupDesc" disabled style="width: 221px"></el-input>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <el-form :inline="true" label-position="top" :model="pageData" style="margin-left: 7px;margin-top: -5px;">-->
<!--        <el-form-item>-->
<!--          <span slot="label" @click="getBaseList(29)"><a herf="#">供应商编码</a></span>-->
<!--          <el-input v-model="pageData.supplierID" style="width: 221px;"></el-input >-->
<!--        </el-form-item>-->
<!--        <el-form-item label="供应商名称">-->
<!--          <el-input v-model="pageData.supplierName" disabled style="width: 221px"></el-input>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <el-form :inline="true" label-position="top" :model="pageData" style="margin-left: 7px;margin-top: -5px;">-->
<!--        <el-form-item>-->
<!--          <span slot="label" @click="getBaseList(30)"><a herf="#">制造商编码</a></span>-->
<!--          <el-input v-model="pageData.manufacturerID" style="width: 221px;" ></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="制造商名称">-->
<!--          <el-input v-model="pageData.manufacturerName" disabled style="width: 221px"></el-input>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <el-form :inline="true" label-position="top" :model="pageData" style="margin-left: 7px;margin-top: -5px;">-->
<!--        <el-form-item label="序列号:">-->
<!--          <el-input v-model="pageData.seqNo" style="width: 221px;"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="购入日期:">-->
<!--          <el-date-picker v-model="pageData.purchessDate" value-format="yyyy-MM-dd HH:mm:ss" style="width: 221px;"></el-date-picker>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <el-form :inline="true" label-position="top" :model="pageData" style="margin-left: 7px;margin-top: -5px;">-->
<!--        <el-form-item>-->
<!--          <span slot="label" style="" @click="getBaseList(203)"><a herf="#">机台编码</a></span>-->
<!--          <el-input v-model="pageData.resourceID" style="width: 221px;" ></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="机台名称">-->
<!--          <el-input v-model="pageData.resourceDesc" disabled style="width: 221px"></el-input>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <el-form :inline="true" label-position="top" :model="pageData" :rules="rules" style="margin-left: 7px;margin-top: -5px;">-->
<!--        <el-form-item prop="deptID" :rules="rules.deptType">-->
<!--          <span slot="label" @click="getBaseList(204)"><a herf="#">所属部门编码</a></span>-->
<!--          <el-input v-model="pageData.deptID" style="width: 221px"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="所属部门名称">-->
<!--          <el-input v-model="pageData.deptName" disabled style="width: 221px"></el-input>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <el-form :inline="true" label-position="top" :model="pageData" :rules="rules" style="margin-left: 7px;margin-top: -5px;">-->
<!--        <el-form-item label="在用" prop="active" :rules="rules.activeType">-->
<!--          <el-select v-model="pageData.active" style="width: 221px;">-->
<!--            <el-option label="是" value="Y"></el-option>-->
<!--            <el-option label="否" value="N"></el-option>-->
<!--          </el-select >-->
<!--        </el-form-item>-->
<!--        <el-form-item label="资产编号" >-->
<!--          <el-input v-model="pageData.assetNo" style="width: 221px;" ></el-input>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <el-form :inline="true" label-position="top" :model="pageData" style="margin-left: 7px;margin-top: -5px;">-->
<!--        <el-form-item label="备注" >-->
<!--          <el-input-->
<!--            type="textarea"-->
<!--            v-model="pageData.assetRemark"-->
<!--            :rows="3"-->
<!--            resize='none'-->
<!--            maxlength="300"-->
<!--            show-word-limit-->
<!--            style="width: 456px;height: 60px">-->
<!--          </el-input>-->
<!--        </el-form-item>-->
<!--      </el-form>-->

      <el-form :inline="true" label-position="top" :model="pageData" :rules="rules" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="BU" prop="bu" :rules="rules.bu">
          <el-select v-model="pageData.bu" placeholder="请选择" :disabled="modalDisableFlag" style="width: 455px">
            <el-option
              v-for = "i in userBuList"
              :key = "i.buNo"
              :label = "i.buNo"
              :value = "i.buNo">
              <span style="float: left;width: 100px">{{ i.sitename }}</span>
              <span style="float: right; color: #8492a6;white-space:nowrap;overflow:hidden;text-overflow:ellipsis; font-size: 11px;width: 60px">
                {{ i.buDesc }}
              </span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备编码" prop="objectID" :rules="rules.objectIdType">
          <el-input v-model="pageData.objectID" style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item label="设备名称" prop="objectDesc" :rules="rules.objectDesc" style="margin-left: -10px">
          <el-input v-model="pageData.objectDesc" style="width: 330px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="pageData" :rules="rules" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item prop="locationID" :rules="rules.locationType">
          <span slot="label" @click="getBaseList(26)"><a herf="#">区域</a></span>
          <el-input v-model="pageData.locationID" style="width: 120px;"></el-input>
          <el-input v-model="pageData.locationName" disabled style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item prop="familyID" :rules="rules.familyType">
          <span slot="label" @click="getBaseList(27)"><a herf="#">分类</a></span>
          <el-input v-model="pageData.familyID" style="width: 120px;" ></el-input>
          <el-input v-model="pageData.familyDesc" disabled style="width: 330px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="pageData" :rules="rules" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item prop="groupID" :rules="rules.groupType">
          <span slot="label" @click="getBaseList(28)"><a herf="#">分组</a></span>
          <el-input v-model="pageData.groupID" style="width: 120px;"></el-input>
          <el-input v-model="pageData.groupDesc" disabled style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item>
          <span slot="label" @click="getBaseList(29)"><a herf="#">供应商</a></span>
          <el-input v-model="pageData.supplierID" style="width: 120px;"></el-input>
          <el-input v-model="pageData.supplierName" disabled style="width: 330px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="pageData" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item>
          <span slot="label" @click="getBaseList(30)"><a herf="#">制造商编码</a></span>
          <el-input v-model="pageData.manufacturerID" style="width: 120px;"></el-input>
          <el-input v-model="pageData.manufacturerName" disabled style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item>
          <span slot="label" style="" @click="getBaseList(503)"><a herf="#">机台编码</a></span>
          <el-input v-model="pageData.resourceID" style="width: 120px;" ></el-input>
          <el-input v-model="pageData.resourceDesc" disabled style="width: 330px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="pageData" :rules="rules" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item prop="deptID" :rules="rules.deptType">
          <span slot="label" @click="getBaseList(204)"><a herf="#">所属部门编码</a></span>
          <el-input v-model="pageData.deptID" style="width: 120px"></el-input>
          <el-input v-model="pageData.deptName" disabled style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="在用" prop="active" :rules="rules.activeType">
          <el-select v-model="pageData.active" style="width: 120px;">
            <el-option label="是" value="Y"></el-option>
            <el-option label="否" value="N"></el-option>
          </el-select >
        </el-form-item>
        <el-form-item label="购入日期:" style="margin-left: -10px">
          <el-date-picker v-model="pageData.purchessDate" value-format="yyyy-MM-dd HH:mm:ss" style="width: 330px;"></el-date-picker>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="pageData" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="序列号:">
          <el-input v-model="pageData.seqNo" style="width: 455px;"></el-input>
        </el-form-item>
        <el-form-item label="资产编号" >
          <el-input v-model="pageData.assetNo" style="width: 454px;" ></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="pageData" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="备注" >
          <el-input
            type="textarea"
            v-model="pageData.assetRemark"
            :rows="3"
            resize='none'
            show-word-limit
            style="width: 921px;height: 60px">
          </el-input>
        </el-form-item>
      </el-form>
      <el-footer style="height:31px;margin-top: 50px;text-align:center">
        <el-button type="primary" @click="saveEamObject()">保存</el-button>
        <el-button type="primary" @click="closeDialog">关闭</el-button>
      </el-footer>
    </el-dialog>
    <!--列表的组件-->
    <Chooselist ref="baseList" @getBaseData="getBaseData"></Chooselist>
  </div>
</template>

<script>
import {
  insertEamObject,
  updateEamObject,
} from '@/api/eam/com_eam_object_add_update.js'
import {getSiteAndBuByUserName} from "@/api/eam/eam.js"
/* 引入组件 */
import Chooselist from '@/views/modules/common/Chooselist_eam';/* 列表组件 */
var functionId = 'C10000001';

export default {
  data() {
    return {
      titleCon: '设备清单维护',
      visible: false,
      userId: this.$store.state.user.name,
      pageData: {
        site: '',
        bu: '',
        buNo: '',
        username: this.$store.state.user.name,
        objectID: '',
        objectDesc: '',
        locationID: '',
        locationName: '',
        familyID: '',
        familyDesc: '',
        groupID: '',
        groupDesc: '',
        supplierID: '',
        supplierName: '',
        manufacturerID: '',
        manufacturerName: '',
        resourceID: '',
        resourceDesc: '',
        seqNo: '',
        purchessDate: '',
        active: 'Y',
        addFlag: 'Y',
        checkFlag: 'Y',
        updateBy:'',
        deptID: '',
        deptName: '',
        assetNo: '',
        assetRemark: '',
      },
      dataListLoading: false,
      rules:{
        objectIdType:[
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        objectDesc: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        locationType:[
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        familyType:[
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        groupType:[
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        deptType:[
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        activeType:[
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        bu: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ]
      },
      userBuList: [],
      modalDisableFlag: false,
    }
  },
  methods: {
    // 获取用户的bu
    getSiteAndBuByUserName () {
      let tempData = {
        username: this.$store.state.user.name,
      }
      getSiteAndBuByUserName(tempData).then(({data}) => {
        if (data.code === 0) {
          this.userBuList = data.rows
        }
      })
    },

    // 初始化组件的参数
    init (currentRow) {
      //初始化参数
      this.pageData = JSON.parse(JSON.stringify(currentRow))
      //打开页面
      this.visible = true
      //判断是否是新增
      if (this.pageData.addFlag === 'Y') {
        this.modalDisableFlag = false
        this.pageData.bu = this.userBuList[0].buNo
      } else {
        this.pageData.bu = this.pageData.site + '_' + this.pageData.buNo
        this.modalDisableFlag = true
      }
      //重置人员信息
      this.pageData.username = this.userId
    },

    // 保存标签自定义的信息
    saveEamObject () {
      if (this.pageData.bu === '' || this.pageData.bu == null) {
        this.$message.warning('请选择BU！')
        return
      }
      if (this.pageData.active == null || this.pageData.active === '') {
        this.$message.warning("请选择是否在用!");
        return false
      }
      if (this.pageData.objectID == null || this.pageData.objectID === '') {
        this.$message.warning("设备编码不能为空!");
        return false
      }
      if (this.pageData.objectDesc == null || this.pageData.objectDesc === '') {
        this.$message.warning("设备名称不能为空!");
        return false
      }
      if (this.pageData.locationID == null || this.pageData.locationID === '') {
        this.$message.warning("区域不能为空!");
        return false
      }
      if (this.pageData.groupID == null || this.pageData.groupID === '') {
        this.$message.warning("分组不能为空!");
        return false
      }
      if (this.pageData.familyID == null || this.pageData.familyID === '') {
        this.$message.warning("分类不能为空!");
        return false
      }
      if (this.pageData.deptID == null || this.pageData.deptID === '') {
        this.$message.warning("所属部门不能为空!");
        return false
      }
      // 判断是否是新增标签自定义
      if (this.pageData.addFlag === 'Y') {
        this.insertEamObjectData()
      } else {
        this.updateEamObjectData()
      }
    },

    // 新增
    insertEamObjectData () {
      // if (this.pageData.site == '软标/RFID') {
      //   this.pageData.site = '2'
      // }
      // if (this.pageData.site == '硬标') {
      //   this.pageData.site = '3'
      // }
      // if (this.pageData.site == '通用设备') {
      //   this.pageData.site = '4'
      // }
      insertEamObject(this.pageData).then(({data}) => {
        if (data.code === 200) {
          this.$message.success(data.msg)
          setTimeout(this.closeDialog(), 2000)
        } else {
          this.$message.error(data.msg)
        }
      })
    },

    // 修改
    updateEamObjectData () {
      // if (this.pageData.site == '软标/RFID'){
      //   this.pageData.site = '2'
      // }
      // if (this.pageData.site == '硬标'){
      //   this.pageData.site = '3'
      // }
      // if (this.pageData.site == '通用设备'){
      //   this.pageData.site = '4'
      // }
      updateEamObject(this.pageData).then(({data}) => {
        if (data.code === 200) {
          this.$message.success(data.msg)
          setTimeout(this.closeDialog(), 2000);
        }else{
          this.$message.error(data.msg);
        }
      })
    },

    // 关闭modal
    closeDialog () {
      // 刷新报工的页面
      this.$emit('refreshPageTables')
      // 关闭当前的页面
      this.visible = false
    },

    // 列表方法的回调
    getBaseData (val) {
      if (this.tagNo === 26) {
        //设置参数
        this.pageData.locationID = val.LocationID;
        this.$set(this.pageData,'locationName',val.LocationName)
      } else if (this.tagNo === 27) {
        //设置参数
        this.pageData.familyID = val.FamilyID;
        this.$set(this.pageData,'familyDesc',val.FamilyDesc)
      } else if (this.tagNo === 28) {
        //设置参数
        this.pageData.groupID = val.GroupID;
        this.$set(this.pageData,'groupDesc',val.GroupDesc)
      } else if (this.tagNo === 29) {
        //设置参数
        this.pageData.supplierID = val.SupplierID;
        this.$set(this.pageData,'supplierName',val.SupplierName)
      } else if (this.tagNo === 30) {
        //设置参数
        this.pageData.manufacturerID = val.ManufacturerID;
        this.$set(this.pageData,'manufacturerName',val.ManufacturerName)
      } else if (this.tagNo === 503) {
        //设置参数
        this.pageData.resourceID = val.resource_id
        this.$set(this.pageData,'resourceDesc',val.resource_desc)
      } else if (this.tagNo === 204) {
        //设置参数
        this.pageData.deptID = val.department_id
        this.$set(this.pageData,'deptName',val.department_name)
      }
    },

    // 获取基础数据列表
    getBaseList (val) {
      this.tagNo = val
      this.$nextTick(() => {
        let strVal = ''
        if (val === 26) {
          strVal = this.pageData.locationID
        }else  if (val === 27) {
          strVal = this.pageData.familyID
        }else  if (val === 28) {
          strVal = this.pageData.groupID
        }else  if (val === 29) {
          strVal = this.pageData.supplierID
        }else  if (val === 30) {
          strVal = this.pageData.manufacturerID
        }else  if (val === 503) {
          strVal = this.pageData.resourceID
        }else  if (val === 204) {
          strVal = this.pageData.deptID
        }
        this.$refs.baseList.init(val, strVal)
      })
    },

  },
  components: {
    Chooselist, /* 选择的组件 */
  },
  watch: {
    pageData: {
      deep: true,
      handler: function (newV, oldV) {
        this.pageData.objectID = this.pageData.objectID.toUpperCase();
        this.pageData.locationID = this.pageData.locationID.toUpperCase();
        this.pageData.familyID = this.pageData.familyID.toUpperCase();
        this.pageData.groupID = this.pageData.groupID.toUpperCase();
        this.pageData.supplierID = this.pageData.supplierID.toUpperCase();
        this.pageData.manufacturerID = this.pageData.manufacturerID.toUpperCase();
      }
    },
  },
  created() {
    // 获取用户的 site 和 bu
    this.getSiteAndBuByUserName()
  }
}

</script>

<style scoped lang="scss">


</style>
