<template>
  <ele-modal
    :visible.sync="visible"
    :title="title"
    width="40vw"
    append-to-body
    @close="cancel"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px"
      class="create-form"
    >
      <el-row :gutter="15">
        <el-col v-bind="styleResponsive ? { lg: 12, md: 12 } : { span: 12 }">
          <el-form-item label="名称:" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
        </el-col>
        <el-col v-bind="styleResponsive ? { lg: 12, md: 12 } : { span: 12 }">
          <el-form-item label="类型:" prop="type">
            <el-select
              v-model="form.type"
              size="small"
              style="width: 100%"
              clear
            >
              <el-option
                v-for="item in typeList"
                :key="item.value"
                :value="item.value"
                :label="item.name"
                @click.native="form.value = ''"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-bind="styleResponsive ? { lg: 12, md: 12 } : { span: 12 }">
          <el-form-item label="内容:" prop="value">
            <!-- <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="form.value"
              v-if="form.type == 1"
            >
            </el-input>
            <deptSelect v-model="form.value" v-if="form.type == 2" /> -->
            <formContent :type="form.type" v-model="form.value" />
          </el-form-item>
        </el-col> </el-row
    ></el-form>

    <template v-slot:footer v-if="type != 'detail'">
      <el-button @click="cancel">取消</el-button>

      <el-button type="primary" @click="save" :loading="loading">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
import { saveDisposal } from '@/api/equipmentDisposal/index.js';
import deptSelect from '@/components/CommomSelect/dept-select.vue';
import formContent from './formContent.vue';

const defForm = {
  name: '',
  id: '',
  value: '',
  type: '1'
};
export default {
  components: { deptSelect, formContent },
  data() {
    return {
      typeList: [
        {
          value: '1',
          name: '输入框'
        },
        {
          value: '2',
          name: '部门选择框'
        },
        {
          value: '3',
          name: '日期选择框'
        }
      ],
      visible: false,
      loading: false,
      title: '',
      type: '',
      form: { ...defForm },
      rules: {
        name: [{ required: true, message: '请选择', trigger: 'change' }]
      }
    };
  },

  computed: {
    // 是否开启响应式布局
    styleResponsive() {
      return this.$store.state.theme.styleResponsive;
    }
  },
  created() {},
 
  methods: {
    async open(row, type) {
      this.type = type;

      if (type == 'edit') {
        this.form = JSON.parse(JSON.stringify(row));
        this.form.type = this.form.type + '';
      }
      this.visible = true;
    },

    cancel() {
      this.form = { ...defForm };
      this.visible = false;
    },

    /* 保存编辑 */
    async save() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.loading = true;

          saveDisposal(this.form)
            .then((res) => {
              this.loading = false;
              this.$message.success('成功');
              this.cancel();
              this.$emit('refresh');
            })
            .catch((e) => {
              this.loading = false;
            });
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.basic-details-title {
  margin: 10px 0;
}
.title {
  font-size: 16px;
  line-height: 45px;
}
.add-product {
  width: 100%;
  display: flex;
  align-items: center;
  // justify-content: flex-end;
  font-size: 30px;
  color: #1890ff;
  margin: 10px 0;
  cursor: pointer;
}
</style>
