<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="资产编号" prop="aNumber">
        <el-input
          v-model="queryParams.aNumber"
          placeholder="请输入资产编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产名称" prop="aName">
        <el-input
          v-model="queryParams.aName"
          placeholder="请输入资产名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库类型" prop="storageType">
        <el-select v-model="queryParams.storageType" placeholder="请选择入库类型" clearable size="small">
          <el-option
            v-for="dict in storageTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="storageStatus">
        <el-select v-model="queryParams.storageStatus" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in storageStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </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-form-item>
    </el-form>


    <el-table height="500" v-loading="loading" :data="stockList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center"/>-->
<!--      <el-table-column label="id" align="center" prop="aId"/>-->
      <el-table-column label="资产编号" align="center" prop="aNumber"/>
      <el-table-column label="资产名称" align="center" prop="aName"/>
      <el-table-column label="入库类型" align="center" prop="storageType" :formatter="storageTypeFormat"/>
      <el-table-column label="入库时间" align="center" prop="wareTime" width="180">
        <template slot-scope="scope">
          <span>{{ formDateTime(scope.row.wareTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="dbMaterielName"/>
      <el-table-column label="供应商" align="center" prop="dbSupplierId" :formatter="selectVo"/>
      <el-table-column label="规格型号" align="center" prop="specType"/>
      <el-table-column label="状态" align="center" prop="storageStatus" :formatter="storageStatusFormat"/>
      <el-table-column label="制单人" align="center" prop="sysUser"/>
      <el-table-column label="领用人" align="center" prop="leadUser"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="small"
            @click="showForm(scope.row)"
            v-hasPermi="['assets:stock:edit']"
          >查看
          </el-button>

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

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

    <!-- 添加或修改库存管理对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="800px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :disabled="singleForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="资产编号" prop="aNumber">
              <el-input v-model="form.aNumber" placeholder="请输入资产编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产名称" prop="aName">
              <el-input v-model="form.aName" placeholder="请输入资产名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="入库类型">
              <el-select v-model="form.storageType" placeholder="请选择入库类型">
                <el-option
                  v-for="dict in storageTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库时间" prop="wareTime">
              <el-date-picker clearable size="small" style="width: 200px"
                              v-model="form.wareTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择入库时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="类型" prop="dbMaterielId">
          <treeselect
            v-model="form.dbMaterielId"
            :options="treeOptions"
            :show-count="true" :disabled="singleForm"
            placeholder="选择上级菜单"
          />
        </el-form-item>
        <el-form-item label="供应商" prop="dbSupplierId">
          <el-select v-model="form.dbSupplierId" placeholder="请输入供应商">
            <el-option
              v-for="dict in options"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格型号" prop="specType">
          <el-input v-model="form.specType" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.storageStatus" placeholder="请选择状态">
            <el-option
              v-for="dict in storageStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="制单人" prop="sysUserId">
          <el-input v-model="form.sysUserId" placeholder="请输入制单人"/>
        </el-form-item>
        <el-form-item label="保质期开始" prop="shelfLifeStart">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.shelfLifeStart"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择保质期开始">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="保质期结束" prop="shelfLifeEnd">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.shelfLifeEnd"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择保质期结束">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="领用人" prop="leadUserId">
          <el-input v-model="form.leadUser" placeholder="请输入领用人"/>
        </el-form-item>
        <el-form-item label="领用时间" prop="leadTime">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.leadTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择领用时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listStock, getStock, delStock, addStock, updateStock, exportStock } from '@/api/assets/stock'
  import { select } from '@/api/system/supplier'
  import { treelist } from '@/api/system/materiel'
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  export default {

    name: 'Stock',
    components: {
      Treeselect
    },
    props: ['t'],
    data() {
      return {
        disabled: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 库存管理表格数据
        stockList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 入库类型字典
        storageTypeOptions: [],
        // 状态字典
        storageStatusOptions: [],

        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          aNumber: undefined,
          aName: undefined,
          storageType: undefined,
          storageStatus: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          aNumber: [
            { required: true, message: '资产编号不能为空', trigger: 'blur' }
          ],
          aName: [
            { required: true, message: '资产名称不能为空', trigger: 'blur' }
          ],
          storageType: [
            { required: true, message: '入库类型不能为空', trigger: 'blur' }
          ],
          dbMaterielId: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          dbSupplierId: [
            { required: true, message: '供应商不能为空', trigger: 'blur' }
          ],
          sysUserId: [
            { required: true, message: '制单人不能为空', trigger: 'blur' }
          ]
        },
        // 非单个禁用
        singleForm: true,
        treeOptions: [],
        options: []
      }
    },
    created() {
      this.treelist()
      this.getList()
      this.getDicts('storage_type').then(response => {
        this.storageTypeOptions = response.data
      })
      this.getDicts('storage_status').then(response => {
        this.storageStatusOptions = response.data
      })
      this.select()
    },
    methods: {
      treelist() {
        treelist().then(response => {
          this.treeOptions = response.data
        })
      }
      ,
      /** 查询库存管理列表 */
      getList() {
        this.loading = true
        listStock(this.queryParams).then(response => {
          this.stockList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      select() {
        select().then(response => {
          this.options = response.data
        })
      },
      // 入库类型字典翻译
      storageTypeFormat(row, column) {
        return this.selectDictLabel(this.storageTypeOptions, row.storageType)
      },
      // 状态字典翻译
      storageStatusFormat(row, column) {
        return this.selectDictLabel(this.storageStatusOptions, row.storageStatus)
      },
      selectVo(row, column) {
        return this.selectFormat(this.options, row.dbSupplierId + '')
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          aId: undefined,
          aNumber: undefined,
          aName: undefined,
          storageType: undefined,
          wareTime: undefined,
          dbMaterielId: undefined,
          dbSupplierId: undefined,
          specType: undefined,
          storageStatus: undefined,
          sysUserId: undefined,
          remark: undefined,
          shelf: undefined,
          shelfLifeStart: undefined,
          shelfLifeEnd: undefined,
          leadUserId: undefined,
          leadTime: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.aId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加库存管理'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const aId = row.aId || this.ids
        getStock(aId).then(response => {
          this.form = response.data
          this.form.dbSupplierId = this.form.dbSupplierId + ''
          this.form.storageType = this.form.storageType + ''
          this.form.storageStatus = this.form.storageStatus + ''
          this.open = true
          this.title = '修改库存管理'
        })
      },
      /** 修改按钮操作 */
      showForm(row) {
        this.reset()
        const aId = row.aId || this.ids
        getStock(aId).then(response => {
          this.form = response.data
          this.form.dbSupplierId = this.form.dbSupplierId + ''
          this.form.storageType = this.form.storageType + ''
          this.form.storageStatus = this.form.storageStatus + ''
          this.open = true
          this.disabled = true
          this.title = '查看'
        })
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.aId != undefined) {
              updateStock(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('修改成功')
                  this.open = false
                  this.getList()
                } else {
                  this.msgError(response.msg)
                }
              })
            } else {
              addStock(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('新增成功')
                  this.open = false
                  this.getList()
                } else {
                  this.msgError(response.msg)
                }
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const aIds = row.aId || this.ids
        this.$confirm('是否确认删除库存管理编号为"' + aIds + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delStock(aIds)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        }).catch(function() {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有库存管理数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return exportStock(queryParams)
        }).then(response => {
          this.download(response.msg)
        }).catch(function() {
        })
      }
    },
    //刷新列表
    watch: {
      't': function(newVal) {
        this.handleQuery()
      }
    }
  }
</script>
