<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="cell">
          <div class="cell-title">
            <div class="title-left">
              <div class="title">房型管理</div>
            </div>
          </div>
          <div class="cell-body">
            <div class="controls">
              <el-form :inline="true" :model="formInline" class="demo-form-inline">
                <el-form-item label="">
                  <el-input v-model="formInline.h_type" prefix-icon="el-icon-search" clearable placeholder="请输入房型"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" class="btn-search" @click="btn_search">查询</el-button>
                </el-form-item>
              </el-form>
              <el-button type="primary" class="export" @click="add_house_dialog">添加房型</el-button>
            </div>
            <el-table :data="tableData" height="510" style="width: 100%" :cell-style="cell_style" v-loading="loading" :header-cell-style="header_cell_style">
              <el-table-column label="序号" align="center" width="100">
                <template slot-scope="scope">
                  <span>{{ (pagination.currentPage - 1) * pagination.pageSize + scope.$index + 1 }}</span>
                </template>
              </el-table-column>
              <el-table-column label="房型" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.h_type }}</span>
                </template>
              </el-table-column>
              <el-table-column label="房费" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.price }}</span>
                </template>
              </el-table-column>
              <el-table-column label="数量" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.number }}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" width="300">
                <template slot-scope="scope">
                  <el-button size="mini" round type="primary" @click="modify_house_dialog(scope.$index, scope.row)">编辑</el-button>
                  <el-button size="mini" round type="danger" @click="delete_house_dialog(scope.$index, scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="pagination-table">
              <el-pagination @current-change="currentPageChange" :current-page.sync="pagination.currentPage" :page-size="pagination.pageSize"
                layout="prev, pager, next, jumper" :total="pagination.total">
              </el-pagination>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <!-- 添加房型 -->
    <el-dialog title="添加房型" custom-class="add-house-class" :visible.sync="dialogAddHouseFormVisible" :close-on-click-modal="false"
      :close-on-press-escape="false" @close="add_house_dialog_close">
      <el-form :model="formAddHouse" ref="addAccountForm" :rules="addHouseFormRules">
        <el-form-item label="房型：" :label-width="formLabelWidth" :required="true" prop="h_type">
          <el-input v-model="formAddHouse.h_type" autocomplete="off" placeholder="请输入房型" maxlength="16">
          </el-input>
        </el-form-item>
        <el-form-item label="房费：" :label-width="formLabelWidth" :required="true" prop="price">
          <el-input v-model="formAddHouse.price" autocomplete="off" placeholder="请输入房费" maxlength="4">
          </el-input>
        </el-form-item>
        <el-form-item label="数量：" :label-width="formLabelWidth" :required="true" prop="number">
          <el-input v-model="formAddHouse.number" autocomplete="off" placeholder="请输入数量" maxlength="4">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAddHouseFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add_house_dialog_handler">确 认</el-button>
      </div>
    </el-dialog>
    <!-- 修改账号对话框 -->
    <el-dialog title="修改房型" custom-class="modify-house-class" :visible.sync="dialogModifyHouseFormVisible" :close-on-click-modal="false"
      :close-on-press-escape="false" @close="modify_house_dialog_close">
      <el-form :model="formModifyHouse" ref="modifyAccountForm" :rules="modifyHouseFormRules">
        <el-form-item label="房型：" :label-width="formLabelWidth" :required="true" prop="h_type">
          <el-input v-model="formModifyHouse.h_type" autocomplete="off" placeholder="请输入房型" maxlength="16"></el-input>
        </el-form-item>
        <el-form-item label="房费：" :label-width="formLabelWidth" :required="true" prop="price">
          <el-input v-model="formModifyHouse.price" autocomplete="off" placeholder="请输入房费" maxlength="4">
          </el-input>
        </el-form-item>
        <el-form-item label="数量：" :label-width="formLabelWidth" :required="true" prop="number">
          <el-input v-model="formModifyHouse.number" autocomplete="off" placeholder="请输入数量" maxlength="4">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogModifyHouseFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="modify_house_dialog_handler">确 认</el-button>
      </div>
    </el-dialog>
    <!-- 删除确认对话框 -->
    <el-dialog custom-class="el-dialog-delete-house" :visible.sync="dialogDeleteHouseVisible" :close-on-click-modal="false" :close-on-press-escape="false"
      :show-close="false">
      <div slot="" class="del-account-body">
        <img src="../../icons/serveAC/del_warning.png" alt="">
        <div class="del-account-body-txt">是否确定将该【{{ deleteHouseData.h_type }}】从列表中移除？</div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogDeleteHouseVisible = false"> 否 </el-button>
        <el-button type="primary" @click="delete_house_dialog_handler"> 是 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getTableData,
    addHouseType,
    modifyHouseType,
    delHouseType
  } from '@/api/house'

  export default {
    data() {
      var checkHType = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入房型'));
        }
        setTimeout(() => {
          var reg = /^[\u4e00-\u9fa5\w]{3,16}$/
          if (!reg.test(value)) {
            callback(new Error('3-16个由汉字、字母或数字组成字符'));
          } else {
            callback();
          }
        }, 100);
      };
      var checkPrice = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入房费'));
        }
        setTimeout(() => {
          var reg = /^[1-9]\d{0,3}$/
          if (!reg.test(value)) {
            callback(new Error('必须为小于9999的正整数'));
          } else {
            callback();
          }
        }, 100);
      };
      var checkNumber = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入数量'));
        }
        setTimeout(() => {
          var reg = /^[1-9]\d{0,3}$/
          if (!reg.test(value)) {
            callback(new Error('必须为小于9999的正整数'));
          } else {
            callback();
          }
        }, 100);
      };
      return {
        formLabelWidth: '120px',
        // 表格单元格样式
        cell_style: {
          color: '#1A202B',
          'font-size': '14px',
          'font-family': 'Microsoft YaHei-3970(82674968)'
        },
        // 表格头部样式
        header_cell_style: {
          background: '#E6ECFE',
          color: '#1A202B',
          'font-size': '16px',
          'font-family': 'Microsoft YaHei-3970(82674968)'
        },
        search_datatime: '',
        // 表单数据
        formInline: {
          h_type: ''
        },
        deleteHouseData: {
          h_type: '',
          id: ''
        },
        tableData: [],
        cond_data: {},
        // 分页参数
        pagination: {
          currentPage: 1,
          pageSize: 9,
          total: 0
        },
        loading: true,
        dialogAddHouseFormVisible: false,
        formAddHouse: {
          h_type: '',
          number: '',
          price: ''
        },
        addHouseFormRules: {
          h_type: [{
            validator: checkHType
          }],
          price: [{
            validator: checkPrice
          }],
          number: [{
            validator: checkNumber
          }]
        },
        dialogModifyHouseFormVisible: false,
        formModifyHouse: { // 修改账号对话框 数据
          id: '',
          h_type: '',
          price: '',
          number: ''
        },
        modifyHouseFormRules: {
          h_type: [{
            validator: checkHType
          }],
          price: [{
            validator: checkPrice
          }],
          number: [{
            validator: checkNumber
          }]
        },
        dialogDeleteHouseVisible: false
      }
    },
    mounted() {
      // 获取消费记录,表格数据
      this.get_table_data('list')
      document.getElementsByClassName("el-pagination__jump")[0].childNodes[0].nodeValue = "跳至";
    },
    methods: {
      /**
       * 显示添加对话框
       */
      add_house_dialog() {
        // 显示添加对话框
        this.dialogAddHouseFormVisible = true;
      },
      /**
       * 关闭添加对话框
       */
      add_house_dialog_close() {
        // 关闭添加对话框
        this.dialogAddHouseFormVisible = false;
      },
      /**
       * 添加操作
       */
      add_house_dialog_handler() {
        // 调用添加接口
        this.$refs["addAccountForm"].validate(validate => {
          if (validate) {
            // console.log(this.formAddHouse);
            addHouseType(this.formAddHouse).then((res) => {
              // console.log(res);
              if (typeof res.code == 'undefined' || res.code == '') {
                this.$message.error('返回数据格式问题，code未获取到！');
                return;
              }

              if (res.code === 200) {
                // 重新读取，刷新表格显示
                this.get_table_data();

                this.dialogAddHouseFormVisible = false;
                this.$message.success(res.message);
              } else {
                this.$message.error(res.message);
              }
            }).catch((err) => {
              // console.log(err);
              this.$message.error(err.message);
            })
          } else {
            this.$message.error('验证不通过');
            return false;
          }
        })
      },
      modify_house_dialog(index, row) {
        // console.log(index, row);
        // 赋值准备修改
        this.formModifyHouse.h_type = row.h_type;
        this.formModifyHouse.id = row.id;
        this.formModifyHouse.price = row.price;
        this.formModifyHouse.number = row.number;
        // 显示修改对话框
        this.dialogModifyHouseFormVisible = true;
      },
      /**
       * 修改操作
       */
      modify_house_dialog_handler() {
        // 调用修改接口
        this.$refs["modifyAccountForm"].validate(validate => {
          if (validate) {
            // console.log(this.formAddHouse);
            modifyHouseType(this.formModifyHouse).then((res) => {
              // console.log(res);
              if (typeof res.code == 'undefined' || res.code == '') {
                this.$message.error('返回数据格式问题，code未获取到！');
                return;
              }

              if (res.code === 200) {
                // 重新读取，刷新表格显示
                this.get_table_data();

                this.dialogModifyHouseFormVisible = false;
                this.$message.success(res.message);
              } else {
                this.$message.error(res.message);
              }
            }).catch((err) => {
              // console.log(err);
              this.$message.error(err.message);
            })
          } else {
            this.$message.error('验证不通过');
            return false;
          }
        })
      },
      /**
       * 关闭修改对话框
       */
      modify_house_dialog_close() {
        // 关闭修改对话框
        this.dialogModifyHouseFormVisible = false;
      },
      /**
       * 删除房型对话框
       */
      delete_house_dialog(index, row) {
        // console.log(index, row);
        // 需要删除的数据关键字，赋值准备删除
        this.deleteHouseData.id = row.id;
        this.deleteHouseData.h_type = row.h_type;
        // 显示删除对话框
        this.dialogDeleteHouseVisible = true;
      },
      /**
       * 删除房型操作
       */
      delete_house_dialog_handler() {
        // 调用删除接口
        delHouseType(this.deleteHouseData).then((res) => {
          // console.log(res);
          if (typeof res.code == 'undefined' || res.code == '') {
            this.$message.error('返回数据格式问题，code未获取到！');
            return;
          }

          if (res.code === 200) {
            // 重新读取，刷新表格显示
            this.get_table_data();

            this.dialogDeleteHouseVisible = false;
            this.$message.success(res.message);
          } else {
            this.$message.error(res.message);
          }
        }).catch((err) => {
          // console.log(err);
          this.$message.error(err.message);
        })
      },
      /**
       * 获取消费记录,表格数据
       */
      get_table_data(param) {
        this.loading = true
        this.get_condtion();

        let data = this.cond_data;
        if (param == 'search') {
          data.page = 1
          this.pagination.currentPage = 1
        }
        // console.log(data);
        getTableData(data).then((res) => {
          // console.log(res);
          if (typeof res.rows !== 'undefined' && res.rows !== '') {
            this.pagination.total = res.total
            this.tableData = []
            for (var i = 0; i < res.rows.length; i++) {
              this.tableData.push(res.rows[i])
            }
          } else {
            this.tableData = []
            this.$message.warning('没有符合条件的数据！')
          }
        }).catch((err) => {
          // console.log(err);
          this.$message.error(err.message)
        });
        this.loading = false;
      },
      /**
       * 条件
       */
      get_condtion() {
        let data = {
          page: this.pagination.currentPage,
          rows: this.pagination.pageSize
        }
        if (this.formInline.h_type !== '') {
          data.h_type = this.formInline.h_type
        }

        this.cond_data = data;
      },
      /**
       * 搜索
       */
      btn_search() {
        this.get_table_data('search')
      },
      /**
       * currentPage 改变时会触发
       * @param {Object} val
       */
      currentPageChange(val) {
        this.pagination.currentPage = val
        this.get_table_data('pages')
      }
    }
  }
</script>

<style lang="scss" scoped>
  .app-container {
    background-color: #EFF2F7;
    padding: 10px;

    .el-row {

      .el-col {
        padding: 10px;

        .cell {
          padding: 30px;
          border-radius: 10px;
          background-color: #FFFFFF;
          // box-shadow: 5px 5px 15px #979797;
          box-shadow: 0px 3px 21px 0px rgba(60, 108, 254, 0.16);

          .cell-title {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 30px;
            border-bottom: 1px solid #CCCCCC;

            .title-left {
              display: flex;
              align-items: center;

              .title {
                font-size: 22px;
                font-family: Microsoft YaHei-3970(82674968);
                font-weight: bold;
                color: #1A202B;
              }

              .title-detail {
                margin-left: 50px;
                font-size: 18px;
                font-family: Microsoft YaHei-3970(82674968);
                color: #1A202B;
              }
            }

            .title-right {
              display: flex;
              justify-content: space-between;
              align-items: center;

              .el-button {
                width: 110px;
                height: 46px;
                background: #2B4CFE;
                font-size: 18px;
                color: #FFFFFF;
                font-family: Microsoft YaHei-3970(82674968);
                border-radius: 5px;
              }

              .el-button--warning {
                background: #F88A64;
              }
            }
          }

          .cell-body {

            .controls {
              display: flex;
              justify-content: space-between;
              align-items: center;

              .demo-form-inline {
                display: inline-flex;
                flex-wrap: wrap;
                align-items: center;
                justify-content: flex-start;

                .btn-search {
                  background: #3762FE;
                  border-color: #3762FE;
                  font-size: 14px;
                  color: #FFFFFF;
                  font-family: Microsoft YaHei-3970(82674968);
                  border-radius: 5px;
                }
              }

              .export {
                background: #3762FE;
                border-color: #3762FE;
                font-size: 14px;
                color: #FFFFFF;
                font-family: Microsoft YaHei-3970(82674968);
                border-radius: 5px;
                margin-bottom: 22px;
              }
            }

            .btn-cell-blue {
              color: #2B4CFE;
              border: none;
              font-size: 18px;
            }

            .btn-cell-black {
              color: #53575A;
              border: none;
              font-size: 18px;
            }

            .txt-cell-green {
              color: #67C23A;
            }

            .txt-cell-red {
              color: #F56C6C;
              font-size: 16px;
            }

            .pagination-table {
              display: flex;
              justify-content: flex-end;
              align-items: center;
              height: 30px;
              margin-top: 20px;
              padding-right: 100px;

              ::v-deep .el-pagination {
                display: flex;
                justify-content: flex-end;
                align-items: center;
              }

              ::v-deep .el-pagination ul {
                display: flex;

              }

              ::v-deep .el-pagination button,
              ::v-deep .el-pagination li {
                display: flex;
                justify-content: center;
                align-items: center;
                width: 50px;
                height: 36px;
                border: 1px solid #626262;
                border-radius: 3px;
                font-size: 14px;
                margin: 0 5px;
              }

              ::v-deep .el-pagination span {
                margin-left: 10px;
              }

              ::v-deep .el-pagination .el-pagination__jump {
                font-size: 16px;
              }

              ::v-deep .el-pagination .el-pagination__editor {
                width: 50px;
                height: 36px;
                margin: 0 5px;
              }

              ::v-deep .el-pagination .el-input__inner {
                width: 50px;
                height: 36px;
                border: 1px solid #626262;
                margin: -4px 0 0 0;
              }
            }
          }
        }
      }
    }

    ::v-deep .el-dialog {
      margin: 0 !important;
      width: 400px;
      height: 460px;
      background: #FFFFFF;
      box-shadow: 0px 0px 13px 0px rgba(0, 0, 0, 0.29);
      border-radius: 10px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);

      .el-dialog__header {
        display: flex;
        align-items: center;
        width: 100%;
        height: 58px;
        padding: 30px;
        background: #E6EBFE;
        border-radius: 10px 10px 0px 0px;
        font-weight: bold;
      }

      .el-dialog__body {
        padding-bottom: 0;

        .el-form-item__label,
        .el-form-item__content {
          font-size: 16px;
          font-family: Microsoft YaHei-3970(82674968);
          color: #53575A;
        }

        .el-input__inner {
          width: 180px;
        }
      }

      // 删除对话框的样式
      .del-account-body {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        height: 180px;

        img {
          width: 72px;
        }

        .del-account-body-txt {
          height: 58px;
          line-height: 58px;
          font-size: 18px;
          font-family: Microsoft YaHei-3970(82674968);
          color: #333333;
        }
      }

      .el-dialog__footer {
        padding-bottom: 0 !important;
        text-align: center;

        .el-button.el-button--default {
          width: 75px;
          height: 40px;
          border: 1px solid #2B4CFE;
          border-radius: 6px;
          font-size: 16px;
          font-family: Microsoft YaHei-3970(82674968);
          color: #2B4CFE;
        }

        .el-button.el-button--primary {
          width: 75px;
          height: 40px;
          background: #2B4CFE;
          border-radius: 6px;
          font-size: 16px;
          font-family: Microsoft YaHei-3970(82674968);
          color: #FFFFFF;
          margin-left: 60px;
        }
      }
    }
  }
</style>

<style>
  .add-house-class,
  .modify-house-class {
    height: 360px !important;
  }

  .el-dialog-delete-house {
    height: 300px !important;
    width: 500px !important;
  }

  .el-dialog-delete-house .el-dialog__header {
    display: none !important;
  }
</style>
