<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="outDate">
        <el-date-picker v-model="queryParams.outDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd"
                        format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="出库类型" prop="assetsName">
        <el-select v-model="queryParams.outType">
          <el-option v-for="dicts in dict.type.out_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"
                   v-hasPermi="['system:assets:add']">新增
        </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="outDate"/>
      <el-table-column label="出库数量" align="center" prop="outNumbers"/>
      <el-table-column label="出库类型" align="center" prop="outType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.out_type" :value="scope.row.outType"/>
        </template>
      </el-table-column>
      <el-table-column label="领用人" align="center" prop="outByName"/>
      <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 ref="form" :model="form" :rules="rules" label-width="80px" :inline="true" :disabled="true">
        <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="outNumbers">
          <el-input v-model="form.outNumbers" placeholder=""/>
        </el-form-item>
        <el-form-item label="出库时间" prop="outDate">
          <el-date-picker v-model="form.outDate" 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="outType">
          <el-select v-model="form.outType" style="width: 206px;">
            <el-option v-for="dicts in dict.type.out_type" :key="dicts.value" :value="dicts.value"
                       :label="dicts.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="领用人" prop="outByName">
          <el-input v-model="form.outByName" @click.native="selectBoss1"/>
        </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 ref="form" :model="form" :rules="rules" label-width="80px" :inline="true" :disabled="true">
        <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="outNumbers">
          <el-input v-model="form.outNumbers" placeholder=""/>
        </el-form-item>
        <el-form-item label="出库时间" prop="outDate">
          <el-date-picker v-model="form.outDate" 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="outType">
          <el-select v-model="form.outType" style="width: 206px;">
            <el-option v-for="dicts in dict.type.out_type" :key="dicts.value" :value="dicts.value"
                       :label="dicts.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="领用人" prop="outByName">
          <el-input v-model="form.outByName" @click.native="selectBoss1"/>
        </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>
    <TeacherSelectLite v-if="teacherOptions.open" :openSelect.sync="teacherOptions.open" :title="teacherOptions.title"
                       @getTeacherList="getTeacherList" @setTeacherInfo="setTeacherInfo"></TeacherSelectLite>
    <!-- 选择领用人 -->
    <el-dialog title="选择领用人" :visible.sync="openSelect" width="650px" append-to-body style="padding: 0 0 0 0">
      <div>
        <el-table style="width:560px;margin:0 25px 0 25px" ref="multipleTable" max-height="250" v-loading="loading"
                  :data="outByList" 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="deptName" label="部门" align="center">
          </el-table-column>
        </el-table>

        <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="close" size="small" style="margin: 0 30px 0 10px">取 消
          </el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import TeacherSelectLite from '@/views/components/moduleComponets/TeacherSelectLite/index.vue'

import {
  AddAssetsOut,
  EditAssetsOut,
  ListAssetsOut,
  delAssetsOut,
  getTeacherList,
} from "@/api/smartSchool/logisticsManage/itemRegistration";
import {
  listAssets,
  getAssets,
  addAssets,
  updateAssets,
  outStorageQueryOne
} from "@/api/smartSchool/logisticsManage/itemRegistration";
import Treeselect from "@riophae/vue-treeselect";

export default {
  dicts: ["asset_attributes", "out_type"],
  name: "OuterStorage",
  components: {TeacherSelectLite},

  data() {
    return {
      // 选择领导人对话框
      teacherOptions: {
        open: false,
        title: '选择教师',
        token: null
      },
      //主表id
      id: "",
      // 遮罩层
      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: {},
      //出库人列表
      outByList: [],
      userId: null,
      // 表单校验
      rules: {
        outDate: [
          {required: true, message: "时间不能为空", trigger: "blur"}
        ],
        outNumbers: [
          {required: true, message: "数量不能为空", trigger: "blur"}
        ],
        outType: [
          {required: true, message: "出库类型不能为空", trigger: "blur"}
        ],
        outByName: [
          {required: true, message: "领用人不能为空", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.id = this.$route.query && this.$route.query.id
    console.log(248, this.id)
    this.getList();
  },
  methods: {
    /** 查询物品登记列表 */
    getList() {
      this.loading = true;
      let query = {
        outDate: this.queryParams.outDate,
        outType: this.queryParams.outType
      }
      ListAssetsOut(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,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // /** 重置按钮操作 */
    // resetQuery() {
    //   this.resetForm("queryForm");
    //   this.handleQuery();
    // },
    // 选择领用人
    selectOutBy() {
      this.openSelect = true
      getTeacherList().then(response => {
        this.outByList = response.rows;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "物品出库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      outStorageQueryOne(id).then(response => {
        this.form = response.data;
        this.editOpen = true;
        this.title = "修改出库记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.assetsId = this.$route.query.id
      console.log(this.form, 123);

      AddAssetsOut(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);

      EditAssetsOut(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.editOpen = false;
        this.getList();
      });

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm(`是否确认删除`).then(() => {
        this.loading = true;
        return delAssetsOut(row.id);
      }).then(() => {
        this.getList();
        this.loading = false;
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
        this.loading = false
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选
    select(selection) {
      if (selection.length > 1) {
        this.$refs.multipleTable.toggleRowSelection(selection[0], false);
        selection.shift();  //删除上一个选项
      }
      this.form.outByName = selection[0].teacherName;
      this.form.outBy = selection[0].userId;
      // this.userId = selection[0].userId;
    },
    selectAll(selection) {
      this.$refs.multipleTable.clearSelection()
    },
    //单选
    handleCurrentChange(row) {
      this.form.outBy = row.userId;
      // this.form.outByName = row.teacherName;
      this.$set(this.form, "outByName", row.teacherName)
      // this.userId = row.userId;
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    close() {
      this.openSelect = false
    },
    selectConfirm() {
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      })
      this.openSelect = false;
      // this.open = true
      // this.$parent.confirmArea(this.areaName, this.areaId);
      // //   this.$parent.handleAdd()
      // this.$refs.multipleTable.clearSelection()
    },
    getTeacherList(queryForm, callback) {
      getTeacherList(queryForm).then(response => {
        callback(response.rows, response.total);
      }).catch(error => {
        callback([], 0);
      })
    },

    setTeacherInfo(item) {
      // // this.form.teacherName = item.teacherName;
      //   this.$set(this.form, 'claimBelongName', item.teacherName);
      //   this.$set(this.form, 'claimBelong', item.id);
      // this.form.outByName = row.teacherName;
      // if (this.teacherOptions.token === 1) {
      this.$set(this.form, "outByName", item.teacherName)
      this.$set(this.form, "outBy", item.userId)
      // } else {
      //   this.$set(this.form1, 'claimBelongName', item.teacherName);
      //   this.$set(this.form1, 'claimBelong', item.id);
      // }

      // this.userId = row.userId;
      // this.$refs.multipleTable.toggleRowSelection(row)

    },
    selectBoss1(val) {
      this.teacherOptions.open = true;
      this.teacherOptions.token = val
    },


    /** 导出按钮操作 */
    // handleExport() {
    //   this.download('system/assets/export', {
    //     ...this.queryParams
    //   }, `assets_${new Date().getTime()}.xlsx`)
    // }
  }
}
</script>

<style scoped>

</style>
