<template>
  <!-- 公司资产 -->
  <div style="margin-left:1%">
    <div>
      <div style="margin:20px 0px">
        <el-form :inline="true" :model="queryDTO">
          <el-form-item label="资产名称">
            <el-input v-model="queryDTO.fixedName"></el-input>
          </el-form-item>
          <el-form-item label="资产编号">
            <el-input v-model="queryDTO.fixedCode"></el-input>
          </el-form-item>
          <el-form-item label="资产状态">
            <el-select v-model="queryDTO.fixedState" placeholder="请选择">
              <el-option
                v-for="item in optionsget"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="资产地址">
            <el-input v-model="queryDTO.fixedAddress"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="selectgetlist()">查询</el-button>
            <el-button type="info" icon="el-icon-refresh" @click="backget()">重置</el-button>
            <el-button type="success" icon="el-icon-plus" @click="Upedatassets()">添加资产</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table
        :data="assetslist"
        style="width: 99%"
        :cell-style="{'text-align':'center'}"
        :header-cell-style="{'text-align':'center'}"
        border
      >
        <el-table-column label="序号" width="50px" align="center">
          <template slot-scope="scope">
            {{
            (queryDTO.pageNum - 1) * queryDTO.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="fixedName" label="资产名称"></el-table-column>
        <el-table-column prop="fixedCode" label="资产编号"></el-table-column>
        <el-table-column prop="fixedMoney" label="资产金额"></el-table-column>
        <el-table-column prop="fixedController" label="资产管理员">
          <template slot-scope="scope">
            <el-tag>{{scope.row.fixedControllerName}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="fixedAddress" label="资产地址"></el-table-column>
        <el-table-column prop="registerTime" label="登记时间" width="200px"></el-table-column>
        <el-table-column prop="prop" label="登记人">
          <template slot-scope="scope">
            <el-tag>{{scope.row.userName}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="fixedState" label="资产状态">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.fixedState== '0' ? 'success' : scope.row.fixedState == '1' ? 'warning':'danger' "
            >{{scope.row.fixedState == '0' ? '未使用' : scope.row.fixedState == '1' ? '已使用':'已丢失'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="prop" label="操作">
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="Updateget(scope.row)">修改管理者</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="queryDTO.pageNum"
        :page-size="queryDTO.pageSize"
        layout="total, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>
    <!-- 修改管理张的抽屉 -->
    <div>
      <el-dialog
        title="修改管理人员"
        :visible.sync="drawer"
        :direction="direction"
        :before-close="handleClose"
        width="30%"
      >
        <div style="margin:30px 10%;height:130px;">
          <el-select
            v-model="user.userName"
            :multiple="false"
            filterable
            remote
            reserve-keyword
            placeholder="请输入资产负责人姓名"
            :remote-method="remoteMethod"
            :loading="loading"
            :disabled=" falesId"
          >
            <el-option
              v-for="item in options"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId"
            ></el-option>
          </el-select>
          <el-button
            type="primary"
            style="width:110px;margin:20px 10px"
            @click="grtselect()"
          >{{fzname}}</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { queryBypageAll, update } from "@/api/assets";
import { secletAllStaff } from "@/api/user";
export default {
  data() {
    return {
      //查询资产
      queryDTO: {
        fixedCode: null, //资产编号
        fixedAddress: null, //资产地址
        fixedState: null, //资产状态
        fixedName: null, //资产名称
        companyId: null,
        pageNum: 1,
        pageSize: 10
      },
      //状态
      optionsget: [
        {
          value: 0,
          label: "未使用"
        },
        {
          value: 1,
          label: "已使用"
        },
        {
          value: 2,
          label: "已丢失"
        }
      ],
      assetslist: [], //表格资产
      total: null,
      drawer: false, //修改管理值抽屉
      ///抽屉的修改
      user: {
        companyId: null,
        userId: null,
        userName: null
      },
      options: [],
      list: [],
      loading: false,
      direction: "rtl",
      falesId: true,
      fzname: "修改管理者",
      updateDTO: {
        fixedId: null,
        fixedController: null
      } ///抽屉
    };
  },
  created() {
    this.queryDTO.companyId = this.$store.getters.companyId; //查询资产获取Id
    this.selectAssets();
    this.selectupdates();
  },
  methods: {
    //分页
    handleCurrentChange(val) {
      console.log(val);
      this.queryDTO.pageNum = val;
      this.selectAssets();
    },
    //重置
    backget() {
      this.queryDTO = {
        fixedCode: null, //资产编号
        fixedAddress: null, //资产地址
        fixedState: null, //资产状态
        fixedName: null, //资产名称
        companyId: this.$store.getters.companyId,
        pageNum: 1,
        pageSize: 10
      };
      this.selectAssets();
    },
    //搜索
    selectgetlist() {
      this.selectAssets();
    },
    //查询公司资产（分页）
    selectAssets() {
      queryBypageAll(this.queryDTO)
        .then(res => {
          if (res.success) {
            this.assetslist = res.data.list;
            this.total = res.data.total;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(error => {
          this.$message.error("查询公司资产失败");
        });
    },
    //添加公司资产
    Upedatassets() {
      this.$router.push("/assetsUpedata");
    },
    //下拉框的搜索
    selectupdates() {
      let user = {
        companyId: this.$store.getters.companyId,
      };
      secletAllStaff(user)
        .then(res => {
          if (res.code == 20000) {
            this.list = res.data;
          }
        })
        .catch(error => {
          this.$message.error("没有当前负责人");
        });
    },
    //修改管理者
    Updateget(row) {
      this.drawer = true;
      this.updateDTO.fixedId = row.fixedId;
      this.updateDTO.fixedController = row.fixedController;
      this.user.userName = row.fixedControllerName;
      console.log(row);
    },
    //输入关键字出名字
    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.falesId != false) {
        this.falesId = false;
        this.fzname = "确定管理者";
      } else {
        if (this.options.length > 0) {
          this.updateDTO.fixedController = this.user.userName;
          console.log(this.updateDTO);
          update(this.updateDTO)
            .then(res => {
              if (res.success) {
                this.$message({
                  showClose: true,
                  message: "管理员修改成功",
                  type: "success"
                });
                this.drawer = false;
                this.falesId = true;
                this.selectAssets();
              } else {
                this.$message({
                  showClose: true,
                  message: "修改失败",
                  type: "warning"
                });
              }
            })
            .catch(e => {
              this.$message.error(e.msg);
            });
        } else {
          this.$message({
            showClose: true,
            message: "还没有修改负责人哦亲",
            type: "warning"
          });
          this.drawer = false;
        }
      }
    },
    //关闭抽屉
    handleClose(done) {
      this.falesId = true;
      this.drawer = true;
      this.fzname = "修改管理者";
      this.options = [];
      done();
    }
  }
};
</script>

<style>
</style>