<template>
  <a-card :bordered="false">
    <a-row :gutter="24">
      <!-- 左侧字典列表 -->
      <a-col :span="6">
        <a-row :gutter="24">
          <!-- <a-button style="margin-right: 10px;" type="primary" icon="plus" @click="addCode">新增字典</a-button> -->
          <a-button style="margin-right: 10px;" type="primary" icon="plus" @click="editCode">编辑字典</a-button>
          <!-- <a-select style="width: 110px" placeholder="更多操作" allowClear >
               <a-select-option @click="editCode" value="编辑字典">编辑字典</a-select-option>
               <a-select-option @click="delectCode" value="删除字典">删除字典</a-select-option>
             </a-select> -->
        </a-row>
        <a-row :gutter="24" class="textCount">
          <a-icon type="info-circle" style="color: #1890FF;margin-right: 10px;" />
          <span>当前选择：<span style="color: #40a9ff;font-weight: 600;">{{ selectedMenu }}</span><span v-if="selectedKey.length" class="imgSize" @click="cancelSel">取消选择</span></span>
        </a-row>
        <a-row :gutter="24" >
          <a-input-search v-model="code" placeholder="输入编码类别/名称 搜索字典" style="width: 100%" @search="getCodeList" id="dataDictionary-getCodeList"/>
        </a-row>
        <a-row :gutter="24" >
          <a-menu class="menu-box" style="border-right: 0;max-height: 650px;overflow: auto;" v-model="selectedKey" >
            <a-menu-item style="margin-bottom: 0px;height: 30px; line-height: 30px;" v-for="(item,index) in codeList" :key="index" :value="item">{{ item.name }}</a-menu-item>
          </a-menu>
          <span v-if="noData" style="line-height: 50px; font-weight: 600;">暂无数据</span>
        </a-row>
      </a-col>
      <!-- 右侧数据列表 -->
      <a-col :span="18" >
        <div style="padding-left: 20px;">
          <div class="table-page-search-wrapper">
            <a-form layout="inline">
              <a-row :gutter="48">
                <a-col :md="6" :sm="24">
                  <a-form-item label="数据名称">
                    <a-input allowClear v-model="queryParam.dispName" placeholder="请输入数据名称" id="dataDictionary-dispName"/>
                  </a-form-item>
                </a-col>

                <a-col :md="6" :sm="24">
                  <a-form-item label="状态">
                    <a-select
                      allowClear
                      id="dataDictionary-isEnable"
                      v-model="queryParam.isEnable"
                      placeholder="请选择"
                    >
                      <a-select-option :value="1">
                        是
                      </a-select-option>
                      <a-select-option :value="0">
                        否
                      </a-select-option>
                    </a-select>
                    <!-- <v-select code="FLAG" v-model="queryParam.enableFlag" allowClear placeholder="是否启用"></v-select> -->
                  </a-form-item>
                </a-col>
                <a-col :md="2" :sm="24">
                  <a-button type="primary" @click="$refs.table.refresh(true)" id="dataDictionary-handSubmit">查询</a-button>
                </a-col>
                <!-- <a-col :md="2" :sm="24">
                  <a-button type="primary" @click="queryParam=''">重置</a-button>
                </a-col> -->
              </a-row>
            </a-form>
          </div>
          <!-- <a-divider/> -->
          <div class="table-operator">
            <a-button type="primary" icon="plus" @click="openCodeItemModal" id="dataDictionary-openCodeItemModal">新增数据</a-button>
            <!-- <a-button type="" icon="delete" @click="delectAll">批量删除</a-button> -->
            <a-button type="" icon="reload" @click="$refs.table.refresh(true)" id="dataDictionary-refreshTable">刷新数据</a-button>
          </div>
          <!-- <div  class="textCount">
            <a-icon type="info-circle" style="color: #1890FF;margin-right: 10px;" />
            <span>已选择<span style="color: #40a9ff;font-weight: 600;margin-left: 10px;">{{selectedNum}}项</span><span class="imgSize">清空</span></span>
          </div> -->
          <s-table
            ref="table"
            size="default"
            rowKey="id"
            :columns="columns"
            :data="loadData"
            bordered
          >
            <span slot="action" slot-scope="text, record">
              <template>
                <a class="action-button" @click="handleEdit(record)" id="dataDictionary-handleEdit">编辑</a>
                <a v-if="!record.isEnable" class="action-button red-text" @click="delect(record)" id="dataDictionary-delect">删除</a>
              </template>
            </span>
          </s-table>
        </div>

      </a-col>
    </a-row>
    <codeModal :visible="showCodeModal" :data="codeData" @refresh="getCodeList" @close="showCodeModal = false"></codeModal>
    <codeItemModal :visible="showItemCodeModal" :data="itemCodeData" @refresh="$refs.table.refresh(true)" @close="showItemCodeModal = false"></codeItemModal>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import codeModal from './codeModal.vue'
import codeItemModal from './codeItemModal.vue'
import { listLookUp, delLookUp, getLookUpItem, delLookUpItem } from '@/api/lookup.js'
export default {
  name: 'DataDictionary',
  components: {
    STable, VSelect, codeModal, codeItemModal
  },
  props: {},
  data () {
    // const rowSelection = {
    //    onChange: (selectedRowKeys, selectedRows) => {
    //      console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
    //    },
    //    onSelect: (record, selected, selectedRows) => {
    //      this.selectedNum = selectedRows.length
    //      console.log(record,'record')
    //      console.log(selectedRows,'selectedRows')
    //    },
    //    onSelectAll: (selected, selectedRows, changeRows) => {
    //      this.selectedNum = selectedRows.length
    //      console.log(selected, selectedRows, changeRows)
    //    }
    //  }
    return {
      // 查询参数
      queryParam: {
        dispName: '',
        isEnable: undefined,
        lookupCode: ''
      },
      noData: false,
      code: '', // 根据输入编码类别/名称搜索字典
      selectedNum: '',
      selectedKey: [], // 选中的字典编码项
      selectedMenu: '',
      formLayout: 'horizontal',
      form: this.$form.createForm(this, { name: 'miniForm' }),
      codeList: [],
      showCodeModal: false,
      showItemCodeModal: false,
      codeData: {}, // 编辑字典行数据
      itemCodeData: {},
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'no',
          width: '40',
          align: 'center'
        },
        {
          title: '名称',
          dataIndex: 'dispName',
          width: '100',
          align: 'center'
        },
        {
          title: '项编码',
          dataIndex: 'code',
          width: '100',
          align: 'center'
        },
        {
          title: '排序值',
          dataIndex: 'sort',
          width: '100',
          align: 'center'
        },
        {
          title: '是否启用',
          width: '100',
          align: 'center',
          dataIndex: 'isEnable',
          customRender: function (text) {
            if (text == 1) {
              return '启用'
            } else {
              return '禁用'
            }
          }
        },
        {
          title: '创建时间',
          dataIndex: 'createDate',
          width: '120',
          align: 'center'
        },
        {
          title: '备注',
          dataIndex: 'remarks',
          width: '100',
          align: 'center'
        },
        {
          title: '操作',
          width: '100',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        console.log(parameter, 'parameter')

        return getLookUpItem(Object.assign(parameter, this.queryParam)).then(res => {
          const no = (res.data.pageNo - 1) * res.data.pageSize
          for (let i = 0; i < res.data.list.length; i++) {
            const _item = res.data.list[i]
            _item.no = no + i + 1
            _item.isEnable = _item.isEnable + '' === '1'
          }
          return res.data
        })
      },
      optionAlertShow: false
    }
  },
  methods: {

    // 获取字典列表
    getCodeList () {
      console.log({ code: this.code })
      listLookUp({ code: this.code }).then(res => {
        console.log(res, 'res1111111')
        if (res.status == 200) {
          this.codeList = res.data
          this.noData = false
          if (!res.data.length) {
            this.noData = true
          }
        }
      })
    },
    // 添加字典
    addCode () {
      this.showCodeModal = true
      this.codeData = {}
    },
    // 编辑字典
    editCode () {
      if (!this.selectedKey.length) {
        return this.$message.warning('您还未选择要编辑的字典')
      }
      this.showCodeModal = true
      this.codeData = this.codeList[this.selectedKey]
    },
    // 删除字典
    delectCode () {
      if (!this.selectedKey.length) {
        return this.$message.warning('您还未选择要删除的字典')
      }
      const _this = this
      const id = this.codeList[this.selectedKey].id
      this.$confirm({
        title: '警告',
        content: `确定要删除 ${this.codeList[this.selectedKey].name} 吗?`,
        okText: '确定',
        cancelText: '取消',
        onOk () {
          delLookUp({
            id: id
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success('删除成功')
              _this.getCodeList()
            }
          }).catch(() => {
            _this.$message.success('删除失败, 请稍后重试')
          })
        }
      })
    },
    // 字典 取消选择
    cancelSel () {
      this.selectedKey = []
      this.selectedMenu = ''
    },

    // 新增数据
    openCodeItemModal () {
      if (!this.selectedKey.length) {
        return this.$message.warning('请先选择一个字典类别')
      }
      this.showItemCodeModal = true
      this.itemCodeData = { lookupCode: this.codeList[this.selectedKey].code }
    },

    // 批量删除数据
    delectAll () {
      if (!this.selectedNum) {
        return this.$message.warning('您还未选择要删除的数据')
      }
      const _this = this
      this.$confirm({
        title: '警告',
        content: `确定要删除您选择的这${this.selectedNum}条数据吗?`,
        okText: '确定',
        cancelText: '取消',
        onOk () {
          // delectUserPower({
          //   id: row.id
          // }).then(res => {
          //   if (res.status == 200) {
          //     _this.$message.success('删除成功')
          //     _this.$refs.table.refresh()
          //   }
          // }).catch(() => {
          //   _this.$message.success('删除失败, 请稍后重试')
          // })
        }
      })
    },
    // 删除
    delect (row) {
      const _this = this
      this.$confirm({
        title: '警告',
        content: '确定要删除吗?',
        okText: '确定',
        cancelText: '取消',
        onOk () {
          delLookUpItem({
            id: row.id
          }).then(res => {
            console.log(res, 'res1111')
            if (res.status == 200) {
              _this.$message.success('删除成功')
              _this.$refs.table.refresh()
            }
          }).catch(() => {
            _this.$message.success('删除失败, 请稍后重试')
          })
        }
      })
    },
    handleEdit (row) {
      console.log(row, 'row')
      this.showItemCodeModal = true
      this.itemCodeData = row
    }

  },
  mounted () {
  },
  beforeCreate () {
    this.form = this.$form.createForm(this, { name: 'miniForm' })
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getCodeList()
    })
  },
  watch: {
    selectedKey (newValue, oldValue) {
      console.log(newValue, 'newValue')
      if (newValue.length) {
        this.selectedMenu = this.codeList[this.selectedKey].name + ' ' + '(' + this.codeList[this.selectedKey].code + ')'
        this.queryParam.lookupCode = this.codeList[this.selectedKey].code
        this.$refs.table.refresh(true)
      }
    }
  }
}
</script>

<style scoped>
  .menu-box::-webkit-scrollbar{
    width: 6px;
    height: 6px ;
  }

  .menu-box::-webkit-scrollbar-thumb{
    width: 6px;
    height: 6px ;
    border-radius: 4px;
    -webkit-box-shadow: inset 0 0 2px #d1d1d1;
    background: #e4e4e4;
  }

  .textCount{
    border: 1px solid #91d5ff;
    background-color: #e6f7ff;
    border-radius: 5px;
    padding: 5px 10px 5px 20px;
    margin: 10px 0;
    color: rgba(0, 0, 0, 0.85);
  }
  .imgSize{
    color: #2d8cf0;
    margin-left: 10px;
    font-size: 14px;
  }
  .action-button{
    line-height: 40px;
    white-space: nowrap;
    padding: 5px 10px;
    background-color: #1890ff;
    border-radius: 4px;
    color: #fff;
    margin-right: 5px;
  }
  .red-text{
    background-color: red;
  }
</style>
