<template>
  <div>
    <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item label="所属库房：">
        <el-input disabled v-model="form.fatherName" style="width:80%" size="mini" clearable></el-input>
      </el-form-item>
      <el-form-item label="采集点代码：">
        <el-input :disabled='!btnType' v-model="form.gathercode" style="width:80%" size="mini" clearable></el-input>
      </el-form-item>
      <el-form-item label="采集点名称：">
        <el-input :disabled='!btnType' v-model="form.gathername" style="width:80%" size="mini" clearable></el-input>
      </el-form-item>
      <el-form-item label="序号：">
        <el-input :disabled='!btnType' v-model="form.storeseq" style="width:80%" size="mini" clearable></el-input>
      </el-form-item>
      <el-form-item label="  ">
        <el-button @click="add" :disabled="JSON.stringify(treeRow)==='{}' || treeRow.storecode?true:false "
          v-if="!btnType&&showButtons.add" size="mini" type="success">
          新增</el-button>
        <el-button @click="edit" :disabled="JSON.stringify(treeRow)==='{}'" v-if="!btnType&&showButtons.edit" size="mini"
          type="primary">修改
        </el-button>
        <el-button @click="del" :disabled="JSON.stringify(treeRow)==='{}' "
          v-if="!btnType && (treeRow.storecode?true:false) && showButtons.del" size="mini" type="danger">
          删除</el-button>
        <el-button @click="submit('ruleForm')" v-show="btnType" size="mini" type="primary">保存</el-button>
        <el-button @click="cancel" v-show="btnType" size="mini" type="info">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import {
    clearObjValue,
    message
  } from '@/utils/common'
  export default {
    props: {
      treeRow: {
        type: Object,
        default: () => {},
      },
      treeNode: {
        type: Object,
        default: () => {},
      },
      showButtons: {
        type: Object,
        default: () => {},
      },
    },
    data() {
      return {
        btnType: false,
        form: {
          id: "",
          fatherName: "",
          gathercode: "",
          gathername: "",
          storeseq: "",
        },
        pName: "",
        rules: {

        }
      }
    },
    methods: {
      add() {
        this.title = '新增'
        this.btnType = !this.btnType
        this.form.fatherName = this.treeRow.gathername
        this.form.gathercode = ""
        this.form.gathername = ""
        this.form.storeseq = ""
      },
      edit() {
        this.btnType = !this.btnType
        this.title = '修改'
      },
      del() {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let data = {
            id: this.treeRow.id
          }
          this.$store.dispatch('collectionPoint/deletePoint', data).then(res => {
            this.$emit('getTree')
            clearObjValue(this.form)
            this.$emit('cancels')
          })
        }).catch(() => {});
      },
      submit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let data = {
              fatherName: this.form.fatherName,
              gathercode: this.form.gathercode,
              gathername: this.form.gathername,
              storecode: this.form.storecode,
              storeseq: this.form.storeseq,
            }
            if (this.title === '新增') {
              data.storecode = this.form.id
              this.$store.dispatch('collectionPoint/addPoint', data).then(res => {
                clearObjValue(this.form)
                this.btnType = false
                this.$emit('getTree')
              })
            } else {
              data.id = this.form.id
              this.$store.dispatch('collectionPoint/updatePoint', data).then(res => {
                clearObjValue(this.form)
                this.btnType = false
                this.$emit('getTree')
              })
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      cancel() {
        this.btnType = !this.btnType
        this.$emit('handleNodeClick', this.treeRow, this.treeNode)
      },
    }
  }

</script>
