<template>
  <el-dialog
    :title="modalTitle"
    :visible.sync="modalVisible"
    :width="formLabelWidth"
    top="2vh"
    @close="handleCancel">
    <div class="mobile">
      <div class="hc-box add">
        <div class="hc-box-header">
            产品类别信息
        </div>
        <div class="hc-box-wrap">
            <el-form label-width="90px" :model="form" :rules="rules" ref="from" class="from">
                <el-form-item label="类别名称:" prop="typeName">
                    <el-input type="text" v-model="form.typeName" :disabled="limit.isRead"></el-input>
                </el-form-item>
                <el-form-item label="类别值:">
                    <el-input type="text" v-model="form.typeVal" :disabled="true" placeholder="类别值后台自动生成"></el-input>
                </el-form-item>
                <el-form-item label="方法学名称:">
                    <el-input type="text" v-model="form.methodology" :disabled="limit.isRead"></el-input>
                </el-form-item>
                <el-form-item label="备注:">
                    <el-input type="textarea"
                              :rows="3"
                              v-model="form.typeDesc"
                              :disabled="limit.isRead"></el-input>
                </el-form-item>

            </el-form>
        </div>
      </div>
      <div class="hc-box taskList">
        <div class="hc-box-header">
           类别结构信息
        </div>
        <div class="hc-box-wrap">
            <el-tag
              :key="index"
              v-for="(tag,index) in form.productTypeExpansions"
              :closable="!limit.isRead"
              :disable-transitions="false"
              @close="handleClose(tag)">
              {{tag.expansionItem}}
            </el-tag>
            <el-input
              class="input-new-tag"
              v-if="inputVisible"
              v-model="inputValue"
              ref="saveTagInput"
              size="small"
              @keyup.enter.native="handleInputConfirm"
              @blur="handleInputConfirm"
            >
            </el-input>
            <el-button v-else class="button-new-tag" size="small" @click="showInput" v-show="!limit.isRead">+ 添加类型属性</el-button>
        </div>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="submit" v-if="!limit.isRead">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addProductType , updateProductType } from "@/api/baseManage/productType";
export default {
  name: 'addMobiel',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: 'title'
    },
    action: {
      type: String,
      default: 'add'
    },
    selectRow: {
      type: Object,
      default:null
    }
  },
  watch: {
    visible(newVal, oldVal) {
      this.modalVisible = newVal;
    },
    title(newVal, oldVal) {
      this.modalTitle = newVal;
    },
    action(newVal, oldVal) {
      this.modalAction = newVal;
    },
    selectRow(newVal, oldVal) {
      this.modalSelectRow = newVal;
    }
  },
  data() {
    return {
      modalTitle: this.title,
      modalVisible: this.visible,
      modalAction: this.action,
      modalSelectRow: this.selectRow,
      formLabelWidth: '1024px',
      //窗口权限
      limit: {
        isRead: false, //是否只读
        isAdd: false //是否可写
      },
      //验证
      rules: {
        typeName: [{ required: true, message: '类别名称不能为空!', trigger: 'blur' }]
      },
      //属性
      inputVisible: false,
      inputValue:'',
      //表单
      form: {
        productTypeExpansions:[]
      }
    };
  },
  created() {
    // console.log(this.selectRow)
    console.log(this.modalSelectRow)
    switch (this.modalAction) {
      case 'add':
        this.limit.isAdd = true;
        break;
      case 'edit':
        this.limit.isAdd = true;
        this.form = Object.assign({},this.modalSelectRow)
        break;
      case 'view':
        this.limit.isRead = true;
        this.form = Object.assign({},this.modalSelectRow)
        break;
      default:
        break;
    }
  },
  methods: {
    //删除类型属性
    handleClose(tag) {
      this.form.productTypeExpansions.splice(this.form.productTypeExpansions.indexOf(tag), 1);
    },
    //打开属性填写框
    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    //输入属性确定
    handleInputConfirm() {
      let inputValue = this.inputValue;
      console.log(inputValue)
      if (inputValue) {
        this.form.productTypeExpansions.push({
          "expansionItem":inputValue
        });
      }
      this.inputVisible = false;
      this.inputValue = '';
    },
    //表单提交
    submit() {
      this.$refs.from.validate(valid => {
        if (valid) {
          let params = Object.assign({},this.form)
          console.log(params)
          if(this.modalAction === 'add'){
            addProductType(params).then(res => {
                if (res.retHead.errCode === 0) {
                  this.msgSuccess("操作成功");
                  this.modalVisible = false
                 	this.$emit('closeModal', true);
                }
            });
          }else{
            updateProductType(params).then(res => {
                if (res.retHead.errCode === 0) {
                  this.msgSuccess("操作成功");
                  this.modalVisible = false
                 	this.$emit('closeModal', true);
                }
            });
          }
        } else {
          return false;
        }
      });
    },
    //窗口关闭
    handleCancel() {
      this.$emit('closeModal', false);
    }
  }
};
</script>

<style lang="scss" scoped>

  .el-tag + .el-tag {
      margin-left: 10px;
    }
    .button-new-tag {
      margin-left: 10px;
      height: 32px;
      line-height: 30px;
      padding-top: 0;
      padding-bottom: 0;
    }
    .input-new-tag {
      width: 90px;
      margin-left: 10px;
      vertical-align: bottom;
    }

  .hc-box{
    border:1px solid #e9e9e9;
    margin-bottom:20px;
    .hc-box-header{
        height:40px;
        line-height:40px;
        background:#fafafa;
        font-weight:bold;
        color:#333;
        padding:0 15px;
        .action{
          margin-left:10px;
          color:#304FFE
        }
    }
  }
  .add{
    .hc-box-wrap{
      padding:15px 30px 15px 0;
    }
  }
  .taskList{
    .hc-box-wrap{
      padding:15px;
    }
  }
</style>
