<template>
  <!-- 弹出框表单 -->
  <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="24" v-if="form.parentId !== 0">
          <el-form-item label="上级站点" prop="parentId">
            <treeselect
              v-model="form.parentId"
              :options="siteOptions"
              :normalizer="normalizer"
              placeholder="选择上级站点"
            />
          </el-form-item>
        </el-col>
          <el-form-item label="站点名称" prop="siteName">
            <el-input v-model="form.siteName" placeholder="请输入站点名称" />
          </el-form-item>
          <el-form-item label="显示排序" prop="orderNum">
            <el-input-number
              v-model="form.orderNum"
              controls-position="right"
              :min="0"
            />
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input
              v-model="form.address"
              placeholder="请输入地址"
              maxlength="20"
            />
          </el-form-item>
          <el-form-item label="联系电话" prop="mobile">
            <el-input
              v-model="form.mobile"
              placeholder="请输入联系电话"
              maxlength="11"
            />
          </el-form-item>
          <el-form-item label="站点编号" prop="number">
            <el-input
              v-model="form.number"
              placeholder="请输入站点编号"
              maxlength="50"
            />
          </el-form-item>
          <el-form-item label="站点状态">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="(label, value) in dict.status"
                :key="value"
                :label="value"
                >{{ label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>

        <el-col :span="24">
          <el-form-item label="单位介绍" prop="introduce">
            <el-input
              v-model="form.introduce"
              placeholder="请输入单位介绍"
              type="textarea"
              :rows="2"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="中心介绍" prop="summary">
            <el-input
              v-model="form.summary"
              placeholder="请输入中心介绍"
              type="textarea"
              :rows="2"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import form from "@/assets/mixins/form";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  mixins: [form],
  components: { Treeselect },
  data() {
    return {
      siteOptions: [],
      // 遮罩层
      loading: true,
      // 站点树选项
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      toString: ["status"],
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: "上级站点不能为空", trigger: "blur" },
        ],
        siteName: [
          { required: true, message: "站点名称不能为空", trigger: "blur" },
        ],
        orderNum: [
          { required: true, message: "显示排序不能为空", trigger: "blur" },
        ],
      },
    };
  },

  methods: {
    //编辑
    edit(row, siteOptions) {
      this.reset()
      this.query = { id: row.id };
      this.urls.currUrl =this.pageInfo.editUrl;;
      this.getData();
      this.siteOptions = siteOptions;
      this.open = true;
      this.title = "修改站点";
    },
    //新增
    add(row, siteOptions) {
      this.reset()
      this.query = { id: row.id };
      this.urls.currUrl = this.pageInfo.addUrl;
      this.getData();
      this.siteOptions = siteOptions;
      this.open = true;
      this.title = "新增站点";
    },
    //查看
    view(row, siteOptions) {
      this.reset()
      this.query = { id: row.id };
      this.urls.currUrl =this.pageInfo.viewUrl;;
      this.getData();
      this.siteOptions = siteOptions;
      this.open = true;
      this.title = "站点详细";
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },

    afterSubmit(data) {
      this.open = false;
      this.$emit("ok");
    },

    /** 转换站点数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.siteName,
        children: node.children,
      };
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: 0,
        siteName: "",
        orderNum: 0,
        address: "",
        mobile: "",
        introduce: "",
        summary: "",
        ancestors: "",
        status: "1",
      };
      this.resetForm("form");
    },
    resetForm(refName) {
      if (this.$refs[refName]) {
        this.$refs[refName].resetFields();
      }
    },
  },
};
</script>
