<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="KEYWORDS" placeholder="搜索" class="filter-item" style="width: 200px;"/>
      <div class="filter-lable w80">企业类型：</div>
      <el-select v-model="SMALL_TYPE" placeholder="请选择" clearable style="width: 100px" class="filter-item" >
        <el-option v-for="item in smallList" :key="item.DICTIONARIES_ID" :label="item.NAME" :value="item.DICTIONARIES_ID" @click.native="getCorpType2(item.DICTIONARIES_ID)"/>
      </el-select>
      <el-select v-model="SMALL_TYPE2" placeholder="请选择" clearable style="width: 100px" class="filter-item" >
        <el-option v-for="item in small2List" :key="item.DICTIONARIES_ID" :label="item.NAME" :value="item.DICTIONARIES_ID" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getQuery">
        搜索
      </el-button>
      <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="goKeyReset">
        重置
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="varList"
      :row-key="getRowKey"
      :header-cell-style="{
        'font-weight': 'bold',
        'color': '#000'
      }"
      tooltip-effect="dark"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        :reserve-selection="true"
        type="selection"
        width="55"
        align="center"/>
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="TITLEDRY" label="题干" />
      <el-table-column prop="SMALL_TYPE_NAME" label="十小类型" />
      <el-table-column prop="SMALL_TYPE2_NAME" label="十小类型" />
      <el-table-column prop="TYPE" label="检查类型" >
        <template slot-scope="{row}">
          <span v-if="row.TYPE==1">日常</span>
          <span v-if="row.TYPE==2">专项</span>
        </template>
      </el-table-column>
      <el-table-column prop="REMARKS" label="备注" />
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="{row}">
          <el-button v-show="edit" type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(row.SECURITYCOMMITMENT_ID)">编辑</el-button>
          <el-button v-show="del" type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(row.SECURITYCOMMITMENT_ID)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div>
        <el-button v-show="add" type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
        <el-button v-show="del" type="danger" icon="el-icon-delete" plain @click="batchDel">删除</el-button>
      </div>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>

    <el-dialog :visible.sync="dialogFormEdit" :title="dialogType==='edit'?'修改':'新增'" width="600px">
      <el-form ref="form" :rules="rules" :model="form" label-width="110px" style="width: 500px;">
        <el-form-item label="题干" prop="TITLEDRY">
          <el-input id="TITLEDRY" ref="TITLEDRY" v-model="form.TITLEDRY" maxlength="255" placeholder="这里输入题干..." title="题干"/>
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="类型" prop="SMALL_TYPE">
              <el-select v-model="form.SMALL_TYPE" placeholder="请选择" style="width: 100px" class="filter-item" >
                <el-option v-for="item in editType.smallList" :key="item.DICTIONARIES_ID" :label="item.NAME" :value="item.DICTIONARIES_ID" @click.native="getTypeCorpType2(item.DICTIONARIES_ID)"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="editType.small2List.length>0" :span="8">
            <el-form-item label="子类型" prop="SMALL_TYPE2">
              <el-select v-model="form.SMALL_TYPE2" placeholder="请选择" style="width: 100px" class="filter-item" >
                <el-option v-for="item in editType.small2List" :key="item.DICTIONARIES_ID" :label="item.NAME" :value="item.DICTIONARIES_ID" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="检查类型" prop="TYPE">
          <el-select v-model="form.TYPE" clearable placeholder="请选择">
            <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="REMARKS">
          <el-input id="REMARKS" ref="REMARKS" v-model="form.REMARKS" maxlength="255" placeholder="这里输入备注..." title="备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormEdit = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      KEYWORDS: '',
      smallList: [], // 请选择企业类型
      small2List: [],
      typeList: [{ value: 1, label: '日常' }, { value: 2, label: '专项' }],
      SMALL_TYPE: '',
      SMALL_TYPE2: '',
      varList: [],
      pd: [],
      editType: {
        smallList: [], // 请选择企业类型
        small2List: []
      },
      form: {
        TITLEDRY: '', // 题干
        OPTIONS: '', // 选项
        PROMPT: '', // 提示
        REMARKS: '', // 备注
        SMALL_TYPE: '', // 十小类型
        SMALL_TYPE2: '', // 十小类型
        TYPE: ''// 1日常2专项
      },
      multipleSelectionAll: [], // 所有选中的数据包含跨页数据
      multipleSelection: [], // 当前页选中的数据
      dialogFormEdit: false,
      dialogType: 'add',
      rules: {
        TITLEDRY: [{ required: true, message: '题干不能为空', trigger: 'blur' }],
        SMALL_TYPE: [{ required: true, message: '十小类型不能为空', trigger: 'change' }],
        SMALL_TYPE2: [{ required: true, message: '十小类型不能为空', trigger: 'change' }],
        TYPE: [{ required: true, message: '检查类型不能为空', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
    this.getCorpType()
  },
  methods: {
    getRowKey(row) {
      return row.SECURITYCOMMITMENT_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.SMALL_TYPE = ''
      this.SMALL_TYPE2 = ''
      this.listQuery = {
        page: 1,
        limit: 10
      }
      this.getQuery()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/securitycommitment/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          SMALL_TYPE: this.SMALL_TYPE,
          SMALL_TYPE2: this.SMALL_TYPE2
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
        this.pd = data.pd
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 添加
    handleAdd() {
      this.dialogType = 'add'
      this.resetForm()
      this.editType.small2List = []
      this.dialogFormEdit = true
    },
    // 修改
    handleEdit(ID) {
      this.dialogType = 'edit'
      this.editType.small2List = []
      requestFN(
        '/securitycommitment/goEdit',
        {
          SECURITYCOMMITMENT_ID: ID
        }
      ).then((data) => {
        this.form = Object.assign({}, data.pd) // copy obj
        requestFN(
          '/dictionaries/getLevels',
          {
            tm: new Date().getTime(),
            DICTIONARIES_ID: this.form.SMALL_TYPE
          }
        ).then((data) => {
          this.editType.small2List = data.list
        })
          .catch((e) => {
            this.listLoading = false
          })
      }).catch((e) => {
        this.listLoading = false
      })
      this.dialogFormEdit = true
    },
    // 保存
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.listLoading = true
          requestFN(
            '/securitycommitment/' + this.dialogType,
            this.form
          ).then((data) => {
            this.listLoading = false
            this.dialogFormEdit = false
            this.getList()
          }).catch((e) => {
            this.listLoading = false
          })
        } else {
          return false
        }
      })
    },

    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/securitycommitment/delete',
          {
            SECURITYCOMMITMENT_ID: id
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },

    batchDel() {
      const _selectData = this.$refs.multipleTable.selection
      if (_selectData == null || _selectData.length == 0) {
        this.$message({
          message: '请选中要删除的项...',
          type: 'error'
        })
        return false
      }
      const ids = _selectData.map((item, index) => {
        return item.SECURITYCOMMITMENT_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/securitycommitment/deleteAll',
          {
            DATA_IDS: ids
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.$refs.multipleTable.clearSelection()
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 判断按钮权限，用于是否显示按钮
    hasButton: function() {
      var keys = 'securitycommitment:add,securitycommitment:del,securitycommitment:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.securitycommitmentfhadminadd		// 新增权限
        this.del = data.securitycommitmentfhadmindel		// 删除权限
        this.edit = data.securitycommitmentfhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    getCorpType() {
      requestFN(
        '/dictionaries/getLevels',
        {
          tm: new Date().getTime(),
          DICTIONARIES_ID: '5330766f60af4876b4a515935a0a85f6'
        }
      ).then((data) => {
        this.smallList = data.list
        this.editType.smallList = data.list
      })
        .catch((e) => {
          this.listLoading = false
        })
    },
    getCorpType2(pID) {
      this.SMALL_TYPE = pID
      requestFN(
        '/dictionaries/getLevels',
        {
          tm: new Date().getTime(),
          DICTIONARIES_ID: pID
        }
      ).then((data) => {
        this.small2List = data.list
      })
        .catch((e) => {
          this.listLoading = false
        })
    },
    getTypeCorpType2(pID) {
      this.form.SMALL_TYPE2 = ''
      requestFN(
        '/dictionaries/getLevels',
        {
          tm: new Date().getTime(),
          DICTIONARIES_ID: pID
        }
      ).then((data) => {
        this.editType.small2List = data.list
      })
        .catch((e) => {
          this.listLoading = false
        })
    },
    resetForm() {
      this.form = {
        TITLEDRY: '', // 题干
        OPTIONS: '', // 选项
        PROMPT: '', // 提示
        REMARKS: '', // 备注
        SMALL_TYPE: '', // 十小类型
        SMALL_TYPE2: '', // 十小类型
        TYPE: ''// 1日常2专项
      }
    }
  }
}

</script>
