<template>
  <div class="app-container">
    <div class="head">
      添加设备
      <el-button
        type="primary"
        size="mini"
        style="margin-left: 20px"
        @click="back"
      >返回</el-button>
      <el-button
        type="primary"
        size="mini"
        style="margin-left: 20px"
        @click="chooseTemplate"
      >选用模板</el-button>
    </div>
    <edit-device
      style="margin-left: 16px"
      :template-data="templateData"
      :is-dis="false"
      @finish="back"
      @back="back"
    />
    <el-dialog
      title="选用模板"
      :visible.sync="dialogVisible"
      width="500px"
      :close-on-click-modal="false"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        label-position="left"
      >
        <el-form-item label="模板名称" prop="templateId">
          <el-select
            v-model="form.templateId"
            placeholder="请选择模板名称"
            filterable
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in templateDataList"
              :key="index"
              :label="item.templateName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false"> 取消 </el-button>
        <el-button type="primary" @click="sure"> 确定 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import EditDevice from './components/edit'
import { templateList, templateDetail } from '@/api/FKJ'

export default {
  components: { EditDevice },
  data() {
    return {
      templateData: {},
      templateDataList: [],
      dialogVisible: false,
      form: {
        templateId: null
      },
      rules: {
        // templateId: [
        //   {
        //     required: true,
        //     message: "不能为空!",
        //     trigger: ["change", "blur"],
        //   },
        // ],
      }
    }
  },
  created() {
    this.templateList()
  },
  mounted() {
    // this.initData()
  },
  methods: {
    back() {
      this.$router.back()
    },
    chooseTemplate() {
      this.form.templateId = null
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    templateList() {
      templateList().then((res) => {
        this.templateDataList = res.data
      })
    },
    sure() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          templateDetail(this.form.templateId).then((res) => {
            this.dialogVisible = false
            this.templateData = JSON.parse(JSON.stringify(res.data))
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.head {
  margin-left: 16px;
  font-weight: 600;
  font-size: 18px;
  color: #333333;
}
.foot-layout {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 34px;
  padding-right: 180px;
  margin-bottom: 20px;
}
</style>
