<template>
  <el-dialog class="scrollbar-dialog" :title="(addressid ? '修改' : '添加') + '项目'"  width="600px" :visible.sync="dialogVisible"   @close="closeDialog">
    <el-scrollbar class="layer-content">
      <el-form ref="dialogForm" :model="dialogForm" :rules="rules" label-width="120px" v-loading="dialogLoading">
        <div class="formGroup">
          <el-row :gutter="20">
            <el-col :span="20">
              <el-form-item label="名称：" prop="name">
                <el-input v-model="dialogForm.name" maxlength="100" show-word-limit style="padding-right:60px"></el-input>
              </el-form-item>
              <el-form-item label="描述：">
                <el-input type="textarea" v-model="dialogForm.description"  maxlength="255" show-word-limit style="padding-right:60px"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
    </el-scrollbar>
    <span slot="footer">
      <el-button @click="closeDialogVal">取 消</el-button>
      <el-button type="primary" @click="submit">{{addressid ? '修 改' : '添 加'}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getProjectListDetail, saveProjectList } from '@/api/project_list'
export default {
  name: 'ProjectInfo',
  data() {
    return {
      dialogVisible: this.value,
      dialogLoading: false,
      dialogForm: {
        ishttp: []
      },
      rules: {
        // 暂时没有需求
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ]
      },
      addressid: false
    }
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    ProjectInfo: {
      type: Object,
      default: {}
    }
  },
  watch: {
    value(val) {
      this.dialogVisible = val
    },
    dialogVisible(val) {
      this.$emit('changedialog', val)
    },
    ProjectInfo(val) {
      this.dialogForm = val
      this.addressid = val.id
    }
  },
  mounted() {

  },
  methods: {
    closeDialog() {
      // this.$refs.dialogForm.resetFields()
    },
    closeDialogVal() {
      this.$emit('changedialog', false)
    },
    // 查询详情
    requestData(id) {
      this.dialogLoading = true
      getProjectListDetail(id).then((rt) => {
        this.dialogLoading = false
        for (const key in this.dialogForm) {
          this.dialogForm[key] = rt.data[key]
        }
        this.addressList = rt.data.address || []
      }).catch(() => {
        this.dialogLoading = false
        this.addressList = []
      })
    },

    handleConnector(data) {
      this.dialogForm.connectorType = data.type
      this.dialogForm.xieyi = data.explain
      this.dialogForm.fuwuleixing = data.type
    },

    handleTarget(data) {
      this.dialogForm.targetName = data.name
    },
    async submit() {
      this.$refs['dialogForm'].validate((valid) => {
        if (valid) {
          // 暂时没有必填验证
          // if (this.addressList.filter(item => item.type === '0').length === 0) {
          //   this.$message({
          //     showClose: true,
          //     message: '请选择一个默认收货地址',
          //     type: 'error'
          //   })
          //   return
          // }

          const ids = this.addressid ? { id: this.addressid } : {}
          const params = {
            ...ids,
            userid: this.$store.state.app.userInfo.empuid,
            ...this.dialogForm,
            address: this.addressList
          }
          this.dialogLoading = true
          saveProjectList(params).then(() => {
            this.$emit('confirm')
            this.dialogLoading = false
            this.dialogVisible = false
            this.$message({
              message: `${this.addressid ? '修改' : '添加'}成功`,
              type: 'success'
            })
          }).catch((e) => {
            if (e.status === '500') {
              this.$message({
                message: `${e.data[0].message}`,
                type: 'error'
              })
            }
            this.dialogLoading = false
          })
        }
      })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
.scrollbar-dialog .el-dialog__body .el-scrollbar{
  height:30vh;
}
</style>
