<template>
  <div class="app-container">
    <el-card>
      <span class="show-text">焦点说明:</span>
      <el-input v-model="focusName" clearable style="width: 280px;margin-right: 26px" placeholder="请输入焦点说明"/>
      <el-button :disabled="loading" icon="el-icon-search" type="primary" @click="searchTable()">查询</el-button>
      <el-button style="margin-left: 14px" type="success" icon="el-icon-circle-plus-outline" @click="handleAdd('form')">新增</el-button>
      <div style="margin-top: 26px">
        <el-table
          v-loading="loading"
          :data="tableData"
          :header-row-style= "{fontSize: '18px',fontFamily:'MiSans Demibold',height: '64px',backgroundColor:'#fff'}"
          :row-style="{fontSize: '16px',fontFamily:'MiSans Regular',height: '64px'}"
          element-loading-text="数据加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
          stripe
          tooltip-effect="dark"
          style="width: 742px"
          height="694"
          border
          size="mini">
          <el-table-column type="index" label=" " align="center" width="100"/>
          <el-table-column align="center" label="焦点说明" width="300">
            <template slot-scope="scope">
              {{ scope.row.焦点说明 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="当前选择" width="141">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.当前选择"
                active-color="#4d77d6"
                inactive-color="#c61a1a"
                @change="changeSelectFocus(scope.row)"/>
            </template>
          </el-table-column>
          <el-table-column label="编辑" align="center" width="200">
            <template slot-scope="scope">
              <el-button type="text" style="font-size: 16px;font-family: MiSans Regular,serif;color: #4d77d6" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button type="text" style="font-size: 16px;font-family: MiSans Regular,serif;color: #c61a1a" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="margin-top: 32px;">
          <el-pagination
            v-if="!loading"
            :current-page="pageList"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            :total="total"
            background
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"/>
        </div>
      </div>
    </el-card>

    <el-dialog :visible.sync="dialogFormVisible1" title="新增焦点" center width="440px">
      <el-divider style="margin: 0"/>
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="100px">
        <el-form-item label="焦点说明" prop="焦点说明">
          <el-input
            v-model="form.焦点说明"
            clearable
            style="width: 240px;border-radius: 4px"
            placeholder="焦点说明" />
        </el-form-item>
        <el-row>
          <el-col :span="14"/>
          <el-form-item >
            <el-button style="width: 80px;height: 40px" type="primary" @click="submitAdd('form')">确 定</el-button>
            <el-button style="width: 80px;height: 40px" @click="callOff('form')">取消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible2" title="编辑焦点" center width="440px">
      <el-divider style="margin: 0"/>
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="right" label-width="100px">
        <el-form-item label="焦点说明" prop="焦点说明" style="display: inline-block">
          <el-input
            v-model="form1.焦点说明"
            clearable
            style="width: 240px;border-radius: 4px"
            placeholder="焦点说明"/>
        </el-form-item>
        <el-row>
          <el-col :span="14"/>
          <el-form-item >
            <el-button style="width: 80px;height: 40px" type="primary" @click="submitAdd1('form1')">确 定</el-button>
            <el-button style="width: 80px;height: 40px" @click="callOff1('form1')">取消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      focusName: '',
      loading: false,
      tableData: [],
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      form: {
        焦点说明: ''
      },
      form1: {
        焦点说明: '',
        焦点代码: ''
      },
      rules: {
        焦点说明: [{ required: true, message: '请输入焦点说明', trigger: 'blur' }]
      },
      rules1: {
        焦点说明: [{ required: true, message: '请输入焦点说明', trigger: 'blur' }]
      },
      total: 0, // 总条数
      pageSize: 10, // 每页显示条数
      pageList: 1 // 后台获取页
    }
  },
  created() {
    this.searchTable()
  },
  methods: {
    // 查询
    searchTable() {
      this.loading = true
      this.tableData = []
      var param = []
      param[0] = ['焦点说明', this.focusName]
      param[1] = ['PageCurrent', this.pageList]
      param[2] = ['PageSize', this.pageSize]
      param[3] = ['PageCount', '1111', 'int', '1']
      param[4] = ['ItemCount', '1111', 'int', '1']
      var Data = this.CreateData('11', '基础数据_焦点预置_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData = response.data.result
          for (const info of this.tableData) {
            info['当前选择'] = !!parseInt(info['当前选择'])
          }
        }
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
      })
    },
    // 编辑
    handleEdit(row) {
      if (this.$refs['form1'] !== undefined) {
        this.$refs['form1'].resetFields()
      }
      this.form1.焦点说明 = row.焦点说明
      this.form1.焦点代码 = row.焦点代码
      this.dialogFormVisible2 = true
    },
    // 确认编辑焦点
    submitAdd1(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          var param = []
          param[0] = ['焦点说明', this.form1.焦点说明]
          param[1] = ['焦点位置', '']
          param[2] = ['焦点代码', this.form1.焦点代码]
          var Data = this.CreateData('12', '基础数据_焦点预置_编辑', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('编辑成功')
              this.form1.焦点说明 = ''
              this.form1.焦点代码 = ''
              this.searchTable()
              this.dialogFormVisible2 = false
            } else {
              this.$message.error('编辑失败')
            }
          })
        }
      })
    },
    // 增加焦点
    handleAdd() {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.dialogFormVisible1 = true
    },
    // 确认增加焦点
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          this.tableData = []
          var param = []
          param[0] = ['焦点说明', this.form.焦点说明]
          param[1] = ['焦点位置', '']
          var Data = this.CreateData('12', '基础数据_焦点预置_新增', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('增加成功')
              this.form.焦点说明 = ''
              this.searchTable()
              this.dialogFormVisible1 = false
            } else {
              this.$message.error('增加失败')
            }
          })
        }
      })
    },
    // 重置表单
    callOff1(formName) {
      this.dialogFormVisible2 = false
      this.$refs[formName].resetFields()
    },
    // 重置表单
    callOff(formName) {
      this.dialogFormVisible1 = false
      this.$refs[formName].resetFields()
    },
    // 删除
    handleDelete(row) {
      this.$confirm('当前预置焦点将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirm-confirm-button',
        cancelButtonClass: 'confirm-cancel-button',
        type: 'warning'
      }).then(() => {
        if (row.当前选择) {
          this.$message.warning('请先释放该焦点')
          return
        }
        var param = []
        param[0] = ['焦点代码', row.焦点代码]
        var Data = this.CreateData('12', '基础数据_焦点预置_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.searchTable()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    changeSelectFocus(row) {
      var param = []
      param[0] = ['焦点代码', row.焦点代码]
      var Data = this.CreateData('12', '基础数据_焦点预置_设定选择', param)
      this.ExecDatabase(Data).then(response => {
        this.searchTable()
      })
    },
    // 分页
    handleSizeChange(val) {
      this.pageList = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageList = val
      this.searchTable()
    }
  }
}
</script>
<style scoped>
  .el-card{
    margin: 0px;
  }
</style>
<style>
  .el-pagination.is-background .el-pager li:not(.disabled){
    background-color: #f5f5f5;
    color: #4d77d6;
  }
  .el-pagination.is-background .el-pager li:not(.disabled).active {
    background-color: #4d77d6;
    color: #f5f5f5;
  }
  .el-pagination.is-background .el-pager li:not(.disabled):hover{
    background-color: #7794d5;
    color: #f5f5f5;
  }
</style>
