<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <!-- <el-button @click="getDataList()">查询</el-button> -->
        <el-button type="primary" @click="addOrUpdateHandle()"
          >增加国家区域</el-button
        >
        <!-- <el-button v-if="isAuth('book:bookchapter:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
        <!-- <router-link :to="{path: 'book-book' , query:{  upPageIndex } }">
          <el-button type="primary">返回上一级</el-button>
        </router-link> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"      
      style="width: 100%;"
    >
      <!-- <el-table-column width="100"
        prop="chapter"         
        align="center"
        label="章节序号">
      </el-table-column> -->
      <el-table-column prop="title" label="国家名"> </el-table-column>
      <el-table-column
        prop="code"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        label="区域码"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.areaId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <el-dialog
      title="外层 Dialog"
      :visible.sync="addOrUpdateVisible"
      @close="closeDialog"
      width="500px"
    >
      <div>
        <el-form ref="form" :model="form" label-width="80px" :rules="formRule">
          <el-form-item label="国家名称" prop="title">
            <el-input
              v-model="form.title"
              placeholder="请输入国家名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="区域码" prop="code">
            <el-input v-model="form.code" placeholder="请输入区域码"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="countySubmit">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dataForm: {
        key: ""
      },
      form: {
        title: "",
        code: null,
        areaId: null,
      },
      formRule: {
        title: [
          {
            required: true,
            message: "国家名称不能为空",
            trigger: "blur"
          }
        ],
        code: [
          {
            required: true,
            message: "区域码不能为空",
            trigger: "blur"
          }
        ]
      },
      dataList: [],
      bookid: 0,
      upPageIndex: 0,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    };
  },
  components: {},
  activated() {   
    this.getDataList();
  },
  methods: {
    // 提交
    countySubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let murl = ''
          this.form.areaId ? murl = "/book/baseArea/editBaseArea" : murl = "/book/baseArea/addBaseArea"  
          this.$http({
            // url: this.$http.adornUrl('/forum/articles/list'),
            url: this.$http.adornUrl(murl),
            method: "post",
            data: this.$http.adornData({
              title: this.form.title,
              code: this.form.code,
              areaId: this.form.areaId
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message.success('操作成功！')
              this.getDataList()
              this.closeDialog()
            }
           
          });
        }
      });
    },
    // 关闭弹窗
    closeDialog() {
      this.addOrUpdateVisible = false;
     // this.$refs['form'].resetFields()
      this.$refs['form'].clearValidate()      
      this.form.title = ''
      this.form.code = null
      this.form.areaId = null
    //  console.log('关闭时', this.form)
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        // url: this.$http.adornUrl('/forum/articles/list'),
        url: this.$http.adornUrl("/book/baseArea/getBaseAreas"),
        method: "post",
        data: this.$http.adornData({
          page: this.pageIndex,
          limit: this.pageSize
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {         
          this.dataList = data.page.records;
          this.totalPage = data.page.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },   
    // 新增 / 修改
    addOrUpdateHandle(row) {
      if(row){  
       this.$nextTick(() => {     
        this.form = {
          title: row.title,
          code: row.code,
          areaId: row.areaId
        }
       }) 
        // console.log('传过来的值，', this.form)
      }
      this.addOrUpdateVisible = true; 

    },
    // 删除
    deleteHandle(id) {
      // var ids = id ? [id] : this.dataListSelections.map(item => {
      //   return item.id
      // })
      this.$confirm(
        `确定进行删除操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/book/baseArea/delBaseArea"),
          method: "post",
          // data: this.$http.adornData(ids, false)
          //  data: {'teachId': id}
          data: this.$http.adornData({
            areaId: id
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    }
  }
};
</script>
