<template>
  <el-dialog
    append-to-body
    :title="textMap[dialogStatus]"
    :visible.sync="dialogFormVisible"
  >
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="deviceForm"
      label-width="100px"
      label-well-code="right"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="项目编码" prop="tenantCode">
            <el-input
              v-model="deviceForm.tenantCode"
              clearable
              placeholder="请输入与项目编码"
              :disabled="dialogStatus === 'create' ? false : true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目名称" prop="tenantName">
            <el-input
              v-model="deviceForm.tenantName"
              placeholder="请输入项目名称"
              clearable
              :disabled="dialogStatus === 'create' ? false : true"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="项目联系人" prop="linkPerson">
            <el-input
              v-model="deviceForm.linkPerson"
              placeholder="请输入项目联系人"
              clearable
              :disabled="dialogStatus === 'detail' || dialogStatus === 'makeDep' ? true : false"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人电话" prop="tel">
            <el-input
              v-model="deviceForm.tel"
              placeholder="请输入联系人电话"
              clearable
              :disabled="dialogStatus === 'detail' || dialogStatus === 'makeDep' ? true : false"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="历史保存天数" prop="clearDay">
            <el-input
              v-model="deviceForm.clearDay"
              placeholder="请输入历史数据保存天数"
              clearable
              :disabled="dialogStatus === 'detail' || dialogStatus === 'makeDep' ? true : false"
            />
          </el-form-item>
        </el-col>
        <el-col v-show="dialogStatus === 'makeDep' ? true : false" :span="12">
          <el-form-item label="组织名称" prop="deptName">
            <el-select
              v-model="deviceForm.deptName"
              placeholder="请选择关联组织名称"
              clearable
            >
              <el-option
                v-for="item in deptNameList"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div
      v-show="dialogStatus === 'detail' ? false : true"
      slot="footer"
      class="dialog-footer"
    >
      <el-button type="primary" @click="saveData">
        保存
      </el-button>
      <el-button @click="dialogFormVisible = false">
        取消
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDevice, updateTenantSys, addTenantSys, deptTenantSys } from '@/api/system/tenant'
export default {
  name: 'EditSysTenant',
  data() {
    return {
      dialogFormVisible: false, // 对话框显示隐藏
      deviceTypeList: [], // 设备类型
      dialogStatus: '', // 对话框类型：create,update,detail
      // 对话框标题
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情',
        makeDep: '项目绑定组织'
      },
      // 组织关系列表
      deptNameList: [
        {
          name: '组织名称一',
          value: '0'
        },
        {
          name: '组织名称二',
          value: '1'
        },
        {
          name: '组织名称三',
          value: '2'
        }
      ],
      // 表单内容
      deviceForm: {
        tenantCode: '',
        tenantName: '',
        linkPerson: '',
        tel: '',
        clearDay: '',
        deptName: ''
      },
      // 表单校验规则
      rules: {
        tenantCode: [{ required: true, message: '项目编码不能为空', trigger: ['blur', 'change'] }],
        tenantName: [{ required: true, message: '项目名称不能为空', trigger: ['blur', 'change'] }],
        linkPerson: [{ required: true, message: '项目联系人不能为空', trigger: ['blur', 'change'] }],
        tel: [{ required: true, message: '联系人电话不能为空', trigger: ['blur', 'change'] }],
        deptName: [{ required: true, message: '	关联组织名称必选', trigger: ['blur', 'change'] }]
      }
    }
  },
  created() {
    this.fetchDeviceType()
  },
  methods: {
    // 获取设备类型
    fetchDeviceType() {
      getDevice().then(response => {
        this.deviceTypeList = response.data
      })
    },
    // 重置表单
    resetForm() {
      this.deviceForm = {
        tenantCode: '',
        tenantName: '',
        linkPerson: '',
        tel: '',
        clearDay: '',
        deptName: ''
      }
    },
    // 父子组件通信
    initDialog(dialogStatus, row = null) {
      this.dialogFormVisible = true
      this.dialogStatus = dialogStatus
      if (this.dialogStatus === 'create') {
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (this.dialogStatus === 'update' || this.dialogStatus === 'detail' || this.dialogStatus === 'makeDep') {
        this.deviceForm = {
          tenantCode: row.tenantCode,
          tenantName: row.tenantName,
          linkPerson: row.linkPerson,
          tel: row.tel,
          clearDay: row.clearDay,
          deptName: row.deptName
        }
      }
    },
    // 保存数据
    saveData() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      } else if (this.dialogStatus === 'makeDep') {
        this.makeDept()
      }
    },
    // 绑定组织关系
    makeDept() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          deptTenantSys(this.deviceForm).then(response => {
            if (response.code === 200) {
              this.$message.success('绑定成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    // 修改数据
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateTenantSys(this.deviceForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        console.log(this.deviceForm)
        if (valid) {
          addTenantSys(this.deviceForm).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
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style>
  .el-select__tags {
    flex-wrap: inherit !important;
    overflow-x: auto !important;
  }
  ::-webkit-scrollbar {
    height: 4px;
  }
  /* //滚动条的滑块 */
  ::-webkit-scrollbar-thumb {
    background-color: #eaecf1;
    border-radius: 3px;
  }
</style>
