<!-- 小程序用户管理新增、编辑、详情 -->
<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="form" label-well-code="right" label-width="120px">
      <el-form-item label="手机号" prop="phone">
        <el-input v-model.trim="form.phone" :disabled="dialogStatus === 'detail'" type="text" placeholder="必填" clearable/>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model.trim="form.name" :disabled="dialogStatus === 'detail'" type="text" placeholder="必填" clearable/>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model.trim="form.keyword" :disabled="dialogStatus === 'detail'" type="text" placeholder="必填" clearable/>
      </el-form-item>
      <el-form-item label="所在项目" prop="startDate">
        <el-select v-model="form.projectIds" multiple collapse-tags placeholder="请选择" @change="changeSelectProject">
          <el-option
            v-for="item in projectList"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveData">提交</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'AddUserDialog',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      form: {
        phone: '', // 手机号
        name: '', // 名称
        keyword: '111111', // 密码
        projectIds: [], // 所在项目
        id: '' // 主键
      }, // 表单
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情'
      }, // 表头显示标题
      rules: {
        name: [{ required: true, message: '项目名称不能为空', trigger: ['blur', 'change'] }],
        phone: [{ required: true, message: '手机号不能为空', trigger: ['blur', 'change'] }]
      }, // 前端校验规则
      projectList: [
        {
          value: '1',
          label: '项目1'
        },
        {
          value: '2',
          label: '项目2'
        },
        {
          value: '3',
          label: '项目3'
        }
      ]
    }
  },
  created() {
    this.fetchPrincipalList()
    this.fetchDeptList()
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus // 类型 新增create 编辑update 详情detail
      this.dialogFormVisible = dialogFormVisible // 对话框显隐
      if (dialogStatus === 'create') { // 如果是新增，清除验证
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update') { // 如果是修改，将row中数据填写到输入框中
        this.form = {
          name: row.name, // 名称
          phone: row.phone, // 手机号
          keyword: row.keyword || '111111', // 密码
          projectIds: row.projectIds, // 所在项目
          id: row.id
        }
      }
    },
    // 重置表单
    resetForm() {
      this.form = {
        phone: '', // 手机号
        name: '', // 名称
        keyword: '111111', // 密码
        projectIds: [], // 所在项目
        id: '' // 主键
      }
    },
    // 点击提交
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          // 在这里注意如果密码是空的话，就默认111111
          // addDeviceType(this.form.productName, this.form.productModel, this.form.machineVersion,
          //   this.form.hardwareVersion, this.form.softwareVersion, this.form.designArchive,
          //   this.form.batchFiling, this.form.status, this.form.principal,
          //   this.form.kpi, this.form.quotaSummary, this.quotaSummaryFile, this.form.communicationVersion,
          //   this.form.productType).then(response => {
          //   if (response.code === 200) {
          //     this.$confirm('新增成功，是否继续新增?', '提示', {
          //       confirmButtonText: '是',
          //       cancelButtonText: '否',
          //       type: 'info'
          //     }).then(() => {
          //       this.resetForm()
          //       this.$nextTick(() => {
          //         this.$refs['dataForm'].clearValidate()
          //       })
          //     }).catch(() => {
          //       this.$emit('watchChild')
          //       this.dialogFormVisible = false
          //     })
          //   }
          // })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        // 在这里注意如果密码是空的话，就默认111111
        if (valid) {
          // updateDeviceType(this.form.id, this.form.productName, this.form.productModel, this.form.machineVersion,
          //   this.form.hardwareVersion, this.form.softwareVersion, this.form.designArchive,
          //   this.form.batchFiling, this.form.status, this.form.principal,
          //   this.form.kpi, this.form.quotaSummary, this.quotaSummaryFile, this.form.communicationVersion,
          //   this.form.productType).then(response => {
          //   if (response.code === 200) {
          //     this.$message.success('修改成功')
          //     this.$emit('watchChild')
          //     this.dialogFormVisible = false
          //   }
          // })
        }
      })
    },
    // 点击取消
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    },
    // 所在项目选中值发生变化
    changeSelectProject(val) {
      console.log(val)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .el-date-editor{
    width: 100%;
  }
</style>
