<template>
  <div class="app-container">
    <el-row :gutter="20">

      <el-col :span="6" :xs="24">

        <el-card class="box-card">

          <el-form>
            <el-form-item>
              <el-input
                placeholder="输入关键字进行过滤"
                v-model="filterText">
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleAdd">
                增加
              </el-button>
              <el-button type="primary"  :disabled="disabled" @click="handleNextAdd">
                下级增加
              </el-button>
            </el-form-item>
          </el-form>
          <el-tree
            class="filter-tree"
            :data="treeOptions"
            default-expand-all
            :filter-node-method="filterNode"
            @current-change="selection"
            ref="tree">
          </el-tree>
        </el-card>
      </el-col>
      <el-col :span="18" :xs="24">
        <el-card class="box-card">
          <template v-if="form.parentId===undefined">
            请选择
          </template>
          <template v-else>
            <!-- 添加或修改物料对话框 -->
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
              <el-form-item label="物料名称" prop="mName">
                <el-input v-model="form.mName" maxlength="50" placeholder="请输入物料名称"/>
              </el-form-item>
              <el-form-item label="父ID" prop="parentId">
                <treeselect
                  v-model="form.parentId"
                  :options="treeOptions"
                  :show-count="true"
                  placeholder="选择上级菜单"
                />
              </el-form-item>
              <el-form-item label="显示顺序" prop="orderNum">
                <el-input-number v-model="form.orderNum" placeholder="请输入显示顺序"/>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button type="danger" @click="handleDelete">删除</el-button>
              </el-form-item>
            </el-form>
          </template>

        </el-card>
      </el-col>
      <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="物料名称" prop="mName">
            <el-input v-model="form.mName" maxlength="50" placeholder="请输入物料名称"/>
          </el-form-item>
          <el-form-item label="父ID" prop="parentId">
            <treeselect
              v-model="form.parentId"
              :options="treeOptions"
              :show-count="true"
              placeholder="选择上级菜单"
            />
          </el-form-item>
          <el-form-item label="显示顺序" prop="orderNum">
            <el-input-number v-model="form.orderNum" 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-row>
  </div>
</template>

<script>
  import {
    listMateriel,
    treelist,
    getMateriel,
    delMateriel,
    addMateriel,
    updateMateriel,
    exportMateriel
  } from '@/api/system/materiel'
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'

  let id = 1000

  export default {
    name: 'Materiel',
    components: { Treeselect },
    data() {
      return {
        //按钮屏蔽
        disabled:true,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 物料表格数据
        materielList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          mName: undefined,
          parentId: undefined,
          orderNum: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          mName: [
            { required: true, message: '物料名称不能为空', trigger: 'blur' }
          ]
        },
        // 菜单树选项
        menuOptions: [],
        filterText: '',
        treeOptions: []

      }
    },
    created() {
      this.reset()
      this.treelist()
    },
    watch: {
      filterText(val) {
        this.$refs.tree.filter(val)
      }
    },
    methods: {

      /** 查询物料列表 */
      getList() {
        this.loading = true
        listMateriel(this.queryParams).then(response => {
          this.materielList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      treelist() {
        treelist().then(response => {
          this.treeOptions = response.data
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          mId: undefined,
          mName: undefined,
          parentId: undefined,
          orderNum: undefined,
          remark: 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.mId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.open = true
        this.title = '添加物料'
        this.reset()
      },
      /** 新增按钮操作 */
      handleNextAdd() {
        this.form.parentId=this.form.mId
        this.form.mId=undefined;
        this.form.mName=undefined;
        this.form.orderNum=undefined;
        this.open = true
        this.title = '添加物料'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const mId = row.mId || this.ids
        getMateriel(mId).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改物料'
        })
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.mId != undefined) {
              updateMateriel(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('修改成功')
                  this.open = false
                  this.treelist()
                } else {
                  this.msgError(response.msg)
                }
              })
            } else {
              addMateriel(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('新增成功')
                  this.open = false
                  this.treelist()
                } else {
                  this.msgError(response.msg)
                }
              })
            }
          }

        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const mIds = row.mId || this.ids
        this.$confirm('是否确认删除物料编号为"' + mIds + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delMateriel(mIds)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        }).catch(function() {
        })
      },
      /** 删除按钮操作 */
      handleDelete() {
        const mIds = this.form.mId
        this.$confirm('是否确认删除物料编号为"' + mIds + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delMateriel(mIds)
        }).then(() => {
          this.treelist()
          this.msgSuccess('删除成功')
          this.reset()
        }).catch(function() {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有物料数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return exportMateriel(queryParams)
        }).then(response => {
          this.download(response.msg)
        }).catch(function() {
        })
      },
      filterNode(value, data) {
        if (!value) return true
        return data.label.indexOf(value) !== -1
      },
      selection(value, data) {
        this.reset()
        this.disabled=false
        let obj = data.data
        this.form.mId = obj.id
        this.form.parentId = obj.parentId
        this.form.mName = obj.label
        this.form.orderNum = obj.orderNum
      }
    }
  }
</script>
