<template>
  <div>
    <div>
      <el-row style="margin:20px 0px">
        <el-col :xl="12" :lg="12" :md="12" :sm="12" :xs="24">
          <el-form
            :model="FixedAssetsAddDTO"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="资产编号" prop="fixedCode">
              <el-input v-model="FixedAssetsAddDTO.fixedCode" laceholder="请输入资产编号"></el-input>
            </el-form-item>
            <el-form-item label="资产名称" prop="fixedName">
              <el-input v-model="FixedAssetsAddDTO.fixedName" laceholder="请输入资产名称"></el-input>
            </el-form-item>
            <el-form-item label="资产金额" prop="fixedMoney">
              <el-input v-model="FixedAssetsAddDTO.fixedMoney" laceholder="请输入资产金额"></el-input>
            </el-form-item>
            <el-form-item label="资产地址" prop="fixedAddress">
              <el-input v-model="FixedAssetsAddDTO.fixedAddress" laceholder="请输入资产地址"></el-input>
            </el-form-item>
            <el-form-item label="提交时间">
              <el-input v-model="FixedAssetsAddDTO.registerTime" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="登记详情">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入登记信息"
                v-model="FixedAssetsAddDTO.fixedDetails"
              ></el-input>
            </el-form-item>
            <el-form-item label="登记备注">
              <el-input v-model="FixedAssetsAddDTO.registerRemarks" laceholder="请输入登记备注"></el-input>
            </el-form-item>
            <el-form-item label="资产负责人" style="width:500px;">
              <div>
                <el-select
                  v-model="user.userId"
                  :multiple="false"
                  filterable
                  remote
                  reserve-keyword
                  placeholder="请输入关键词"
                  :remote-method="remoteMethod"
                  :loading="loading"
                   :disabled="attrId!=0?true:false"
                  @change="grtselect()"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.userId"
                    :label="item.userName"
                    :value="item.userId"
                  ></el-option>
                </el-select>
              </div>
            </el-form-item>
            <el-form-item style="margin-top:60px">
              <el-button type="success" @click="submitForm()">提交</el-button>
              <el-button @click="back()">返回</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import { selectByFixedId, updates } from "@/api/assets";
import { money, account_password_valid } from "@/utils/myValidate";
import { secletAllStaff } from "@/api/user";
export default {
  data() {
    return {
      FixedAssetsAddDTO: {
        companyId: null,
        fixedAddress: null, //资产地址
        fixedCode: null, //资产编号（不重复）
        fixedController: null, //选择资产负责人
        fixedDetails: null, //登记详情
        fixedMoney: null, //资产金额
        fixedName: null, //资产名称
        fixedState: 0, //默认为0
        registerRemarks: null, //登记备注
        registerTime: null, //登记时间
        userId: null
      }, //添加资产

      rules: {
        fixedName: [
          { required: true, message: "请输入活动名称", trigger: "change" }
        ],
        fixedCode: [{ validator: account_password_valid, trigger: "blur" }],
        fixedAddress: [
          {
            required: true,
            message: "请输入资产地址",
            trigger: "change"
          }
        ],
        fixedMoney: [
          {
            validator: money,
            trigger: "change"
          }
        ]
      },
      user: {
        companyId: null,
        userName: null,
        userId: null
      },
      loading: false,
      options: [],
      list: [],
      attrId: null
    };
  },
  computed: {
    fixedId() {
      return this.$route.query.id;
    }
  },
  created() {
    this.attrId = this.$route.query.attrId;
    this.selectget();
    this.ggb();
  },

  methods: {
    //查询要修改的信息
    selectget() {
      selectByFixedId({ fixedId: this.fixedId })
        .then(res => {
          if (res.success) {
            this.FixedAssetsAddDTO = res.data;
            this.user.userId = this.FixedAssetsAddDTO.fixedControllerName;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(e => {
          this.$message.error("查询该条资产失败");
        });
    },
    //提交
    submitForm() {
      updates(this.FixedAssetsAddDTO)
        .then(res => {
          if (res.success) {
            this.$message({
              message: "修改资产信息成功",
              type: "success"
            });
            if (this.attrId == 0) {
              this.$router.push("/assetslist");
            } else {
              this.$router.push("/administrationlsit");
            }
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(e => {
          this.$message.error("错了哦，这是一条错误消息");
        });
    },
    //返回
    back() {
      if (this.attrId == 0) {
        this.$router.push("/assetslist");
      } else {
        this.$router.push("/administrationlsit");
      }
    },
    //下拉框的搜索
    ggb() {
      this.user.companyId = this.$store.getters.companyId;
      secletAllStaff(this.user)
        .then(res => {
          if (res.code == 20000) {
            this.list = res.data;
          }
        })
        .catch(error => {
          this.$message.error("没有当前负责人");
        });
    },
    //输入关键字出名字
    remoteMethod(query) {
      if (query !== "") {
        this.loading = true;
        setTimeout(() => {
          this.loading = false;
          this.options = this.list.filter(item => {
            return (
              item.userName.toLowerCase().indexOf(query.toLowerCase()) > -1
            );
          });
        }, 200);
      } else {
        this.options = [];
      }
    },
    //负责人添加
    grtselect() {
      if (this.options.length > 0) {
        console.log(this.user.userId);
        this.FixedAssetsAddDTO.fixedController = this.user.userId;
      } else {
        this.$message.error("没有负责人哦亲");
      }
    }
  }
};
</script>

<style>
</style>