<template>
  <el-dialog :title=" '查看模板'" :close-on-click-modal="false" :visible.sync="visible" :before-close="cleanFileList">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px" :disabled=true>
      <el-form-item label="模板名称" prop="tmpName">
        <el-input v-model="dataForm.tmpName" placeholder="请输入模板名称"></el-input>
      </el-form-item>
      <!-- <el-form-item label="密码" prop="password" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.password" type="password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="comfirmPassword" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.comfirmPassword" type="password" placeholder="确认密码"></el-input>
      </el-form-item> -->

      <!-- <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item> -->
      <el-form-item label="类别" size="mini" prop="categoryId">
        <el-radio-group v-model="dataForm.categoryId">
          <el-radio v-for="category in categoryList" :key="category.categoryId" :label="category.categoryId">{{ category.categoryName }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="文件" size="mini">
        <el-upload class="upload-demo" action="#" :on-preview="handlePreview"
          :on-remove="handleRemove" :before-remove="beforeRemove" multiple :limit="3" :on-exceed="handleExceed"
          :file-list="fileList">
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="描述信息" prop="remark">
        <el-input type="textarea" :autosize="{ minRows: 4, maxRows: 6}" placeholder="请输入内容" v-model="dataForm.remark">
        </el-input>

      </el-form-item>
      <!-- <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false,cleanFileList()">取消</el-button>
      <el-button type="primary" @click="visible = false,cleanFileList()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { MessageBox } from 'element-ui'
import { Message } from "element-ui"
export default {
  data() {
    return {
      visible: false,
      // 获取所有类别列表
      categoryList: [],
      dataForm: {
        id: 0,
        tmpName: "",
        // 本模板的类别
        categoryId: 1, 
        remark: "",
      },
      textarea2: "",
      fileList: [

      ],
      dataRule: {
        tmpName: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
        // email: [
        //   { required: true, message: "邮箱不能为空", trigger: "blur" },
        //   { validator: validateEmail, trigger: "blur" },
        // ],
        // mobile: [
        //   { required: true, message: "手机号不能为空", trigger: "blur" },
        //   { validator: validateMobile, trigger: "blur" },
        // ],
        // codes: [
        //   { required: true, message: "代码文件不能为空", trigger: "blur" },
        // ],
      },
    };
  },
  methods: {
    init(id) {
      this.fileList=[];
      this.dataForm.id = id || 0;
      this.$http({
        url: this.$http.adornUrl("/category/select"),
        method: "get",
        params: this.$http.adornParams(),
      })
      // 这块修改为获取类别
        .then(({ data }) => {
          this.categoryList = data && data.code === 0 ? data.list : [];
        })
        .then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/algs/templs/info/${this.dataForm.id}`),
              method: "get",
              params: this.$http.adornParams(),
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.dataForm.tmpName = data.temp.tmpName;
                this.dataForm.categoryId = data.temp.categoryId;
                this.dataForm.remark = data.temp.remark;
                for(var i=0;i<data.fileList.length;i++){
                  this.fileList.push({
                    name:data.fileList[i].filename,
                    url:''
                  });
                }
                // 模板文件地址
                // this.dataForm.dfsUrlv = data.temp.dfsUrlv;
              }
            });
          }
        })
        .then(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
          });
        });
    },
    // 以下4个函数是文件上传功能的
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      this.visible = false;
      this.$router.replace({ name: 'whatchtemfile',query:{temId:this.dataForm.id,fileName:file.name} });
    },
    handleExceed(files, fileList) {
      Message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return MessageBox.confirm(`确定移除 ${file.name}？`);
    },
    cleanFileList(done){
      this.fileList=[];
      done();
    }
  },
};
</script>
