<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="入库时间" prop="enterDate">
        <el-date-picker v-model="queryParams.enterDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd"
                        format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="入库类型" prop="enterType">
        <el-select v-model="queryParams.enterType">
          <el-option v-for="dicts in dict.type.enter_type" :key="dicts.value" :value="dicts.value"
                     :label="dicts.label"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-form-item>

    </el-form>

    <el-table v-loading="loading" :data="assetsList" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="55" align="center" label="序号"/>
      <el-table-column label="资产名称" align="center" prop="assetsName"/>
      <el-table-column label="入库时间" align="center" prop="enterDate"/>
      <el-table-column label="入库数量" align="center" prop="enterNumbers"/>
      <el-table-column label="入库类型" align="center" prop="enterType">

        <template slot-scope="scope">
          <dict-tag :options="dict.type.enter_type" :value="scope.row.enterType"/>
        </template>
      </el-table-column>
      <el-table-column label="入库人" align="center" prop="enterByName"/>
      <el-table-column label="备注" align="center" prop="remark"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="350px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:assets:edit']">修改
          </el-button>

          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:assets:remove']">删除
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 添加物品登记对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px">
      <el-form :model="form" :rules="rules" label-width="80px" :inline="true" disabled>
        <el-form-item label="资产名称" prop="assetsName">
          <el-input v-model="this.$route.query.assetsName" placeholder="请输入资产名称"/>
        </el-form-item>
        <el-form-item label="规格型号" prop="assetsSpecs">
          <el-input v-model="this.$route.query.assetsSpecs" placeholder="请输入规格型号"/>
        </el-form-item>
        <el-form-item label="资产属性" prop="assetsAttribute">
          <!--          <el-input v-model="this.$route.query.assetsAttribute">-->
          <el-select v-model="this.$route.query.assetsAttribute" style="width:206px">
            <el-option v-for="dicts in dict.type.asset_attributes" :key="dicts.value" :value="dicts.value"
                       :label="dicts.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库存数量" prop="assetsNumbers">
          <el-input v-model="this.$route.query.assetsNumbers" placeholder="请输入库存数量"/>
        </el-form-item>
      </el-form>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true">

        <el-form-item label="入库数量" prop="enterNumbers">
          <el-input v-model="form.enterNumbers" placeholder=""/>
        </el-form-item>
        <el-form-item label="入库时间" prop="enterDate">
          <el-date-picker v-model="form.enterDate" type="datetime" placeholder="选择日期时间"
                          value-format="yyyy-MM-dd HH:mm"
                          format="yyyy-MM-dd HH:mm" style="width: 206px;">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入库类型" prop="enterType">
          <el-select v-model="form.enterType" style="width:206px">
            <el-option v-for="dicts in dict.type.enter_type" :key="dicts.value" :value="dicts.value"
                       :label="dicts.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入库人" prop="enterBy">
          <el-input v-model="form.enterByName" @click.native="selectEnterBy"/>
          <!-- <el-select v-model="form.enterBy " style="width:206px" placeholder="请选择入库人">
            <el-option v-for="(enterBy,index) in enterByList"
                       :key="index"
                       :value="enterBy.id"
                       :label="enterBy.teacherName"></el-option>
          </el-select> -->
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input style="width:500px" v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 修改 -->
    <el-dialog :title="title" :visible.sync="editOpen" width="650px">
      <el-form :model="form" :rules="rules" label-width="80px" :inline="true" disabled>
        <el-form-item label="资产名称" prop="assetsName">
          <el-input v-model="this.$route.query.assetsName" placeholder="请输入资产名称"/>
        </el-form-item>
        <el-form-item label="规格型号" prop="assetsSpecs">
          <el-input v-model="this.$route.query.assetsSpecs" placeholder="请输入规格型号"/>
        </el-form-item>
        <el-form-item label="资产属性" prop="assetsAttribute">
          <el-select v-model="this.$route.query.assetsAttribute" style="width:206px">
            <el-option v-for="dicts in dict.type.asset_attributes" :key="dicts.value" :value="dicts.value"
                       :label="dicts.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库存数量" prop="assetsNumbers">
          <el-input v-model="this.$route.query.assetsNumbers" placeholder="请输入库存数量"/>
        </el-form-item>
      </el-form>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true">

        <el-form-item label="入库数量" prop="enterNumbers">
          <el-input v-model="form.enterNumbers" placeholder=""/>
        </el-form-item>
        <el-form-item label="入库时间" prop="enterDate">
          <el-date-picker v-model="form.enterDate" type="datetime" placeholder="选择日期时间"
                          value-format="yyyy-MM-dd HH:mm"
                          format="yyyy-MM-dd HH:mm" style="width: 206px;">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入库类型" prop="enterType">
          <el-select v-model="form.enterType" style="width:206px">
            <el-option v-for="dicts in dict.type.enter_type" :key="dicts.value" :value="dicts.value"
                       :label="dicts.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入库人" prop="enterBy">
          <el-input v-model="form.enterByName" @click.native="selectEnterBy"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input style="width:500px" v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择领用人 -->
    <el-dialog v-if="isCreate" title="选择入库人" :visible.sync="openSelect" width="780px" append-to-body
               style="padding: 0 0 0 0" :close="selectClose">
      <div>
        <el-form :model="selectQuery" size="small" :inline="true" label-width="68px">
          <el-form-item label="教师名称" prop="teacherName">
            <el-input v-model="selectQuery.teacherName" placeholder="请输入教师名称" clearable/>
          </el-form-item>
          <el-form-item label="教师电话" prop="teacherTel">
            <el-input v-model="selectQuery.teacherTel" placeholder="请选择老师电话" clearable/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="searchEnterBy">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetEnterByUserName">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table style="width:700px;margin:0 25px 0 25px" ref="multipleTable" max-height="250"
                  v-loading="selectLoading" :data="enterByList" size="small" highlight-current-row
                  @row-click="handleCurrentChange" @selection-change="select" @select-all="selectAll">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="教师名称" align="center" prop="teacherName"/>
          <el-table-column prop="sex" label="性别" align="center">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"></dict-tag>
            </template>
          </el-table-column>
          <el-table-column prop="teacherTel" label="教师电话" align="center">
          </el-table-column>
        </el-table>
        <pagination v-show="selectTotal > 0" :total="selectTotal" :page.sync="selectQuery.pageNum"
                    :limit.sync="selectQuery.pageSize" @pagination="searchEnterBy"/>
        <div style="text-align: center; margin: 35px 0 0 0">
          <el-button type="primary" @click="selectConfirm" size="small" style="margin: 0 10px 0 0">确 定
          </el-button>
          <el-button @click="selectClose" size="small" style="margin: 0 30px 0 10px">取 消
          </el-button>
        </div>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {
  AddAssetsEnter,
  EditAssetsEnter,
  ListAssetsEnter,
  delAssetsEnter,
  getAssetsEnter,
  getTeacherList,
  listEnterBy
} from "@/api/smartSchool/logisticsManage/itemRegistration";
import {listAssets, getAssets, addAssets, updateAssets} from "@/api/smartSchool/logisticsManage/itemRegistration";

export default {
  dicts: ["asset_attributes", "enter_type", 'sys_user_sex'],
  name: "EnterStorage",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物品登记表格数据
      assetsList: [],
      // 弹出层标题
      title: "",
      title1: "入库",
      title2: "出库",
      // 是否显示弹出层
      open: false,
      editOpen: false,
      openSelect: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      //出库人列表
      enterByList: [],
      selectTotal: 0,
      //
      enterBy: undefined,
      enterByName: undefined,
      // 出库人查询参数
      selectQuery: {
        pageNum: 1,
        pageSize: 10,
      },
      selectLoading: false,
      // 表单校验
      rules: {
        enterDate: [{required: true, message: "时间不能为空", trigger: "blur"}],
        enterNumbers: [{required: true, message: "数量不能为空", trigger: "blur"}],
        enterType: [{required: true, message: "出库类型不能为空", trigger: "blur"}],
        enterBy: [{required: true, message: "入库人不能为空", trigger: "input"}],
      },
      isCreate: false,
    };
  },
  created() {
    this.id = this.$route.query && this.$route.query.id
    this.getList();
    this.getEnterByList();
  },
  methods: {
    //获取入库人列表
    getEnterByList() {
      listEnterBy().then(res => {
        this.enterByList = res.rows
      })
    },
    /** 查询物品登记列表 */
    getList() {
      this.loading = true;
      // console.log(this.queryParams);
      let query = {
        enterDate: this.queryParams.enterDate,
        enterType: this.queryParams.enterType
      }
      // console.log(query);

      ListAssetsEnter(this.id, query).then(response => {
        this.assetsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.editOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        assetsName: null,
        assetsSpecs: null,
        assetsNumbers: null,
        assetsUnit: null,
        assetsAttribute: null,
        depositPlace: null,
        assetsType: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 选择入库人
    selectEnterBy() {
      this.isCreate = true
      this.openSelect = true
      this.searchEnterBy()
    },
    // 搜索入库人
    searchEnterBy() {
      this.selectLoading = true
      getTeacherList(this.selectQuery).then(res => {
        this.enterByList = res.rows
        this.selectTotal = res.total
        this.selectLoading = false
      })
    },
    resetEnterByUserName() {
      this.selectQuery = {
        pageNum: 1,
        pageSize: 10,
      }
      this.searchEnterBy()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物品登记";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id
      getAssetsEnter(id).then(response => {
        this.form = response.data;
        // console.log(123, response);

        this.editOpen = true;
        this.title = "修改物品登记";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.assetsId = this.$route.query.id
      this.$refs['form'].validate(valid => {
        if (valid) {
          AddAssetsEnter(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      })


    },
    /** 修改 */
    editSubmit() {
      this.form.assetsId = this.$route.query.id
      // console.log(this.form, 123);
      // this.form.enterBy = this.userId
      EditAssetsEnter(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.editOpen = false;
        this.getList();
      });

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm(`是否确认删除`).then(() => {
        this.loading = true;
        return delAssetsEnter(row.id);
      }).then(() => {
        this.getList();
        this.loading = false;
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
        this.loading = false
      });
    },
    // 多选
    select(selection) {
      if (selection.length > 1) {
        this.$refs.multipleTable.toggleRowSelection(selection[0], false);
        selection.shift();  //删除上一个选项
      }
      if (selection.length = 1) {
        this.enterByName = selection[0].teacherName;
        this.enterBy = selection[0].id;
      }
    },
    selectAll(selection) {
      this.$refs.multipleTable.clearSelection()
    },
    //单选
    handleCurrentChange(row) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    // 确认入库人
    selectConfirm() {
      this.form.enterByName = this.enterByName
      this.form.enterBy = this.enterBy
      this.$nextTick(() => {
        console.log(123456);

        this.$refs.form.clearValidate();
      })
      this.openSelect = false;
      this.isCreate = false
    },
    selectClose() {
      this.openSelect = false
      this.isCreate = false
    },
  },
  /** 导出按钮操作 */
  // handleExport() {
  //   this.download('system/assets/export', {
  //     ...this.queryParams
  //   }, `assets_${new Date().getTime()}.xlsx`)
  // }
}
</script>

<style scoped>

</style>
