<template>
  <div>
    <!-- 普通卡片布局 -->
    <div v-if="cardInfo.value == 1" class="flex-card" :style="cardBoxStyle">
      <div v-for="(dataInfo, dataIndex) in cardList" :key="dataIndex" class="box-card">
        <el-card :style="cardStyle">
          <div slot="header" :style="cardInfo.title.class != '' && cardInfo.title.class ? JSON.parse(cardInfo.title.class) : {}" class="clearfix">
            <div class="titleLeft" :style="titleStyle">
              <span v-if="cardInfo.title.text != ''">
                {{ cardInfo.title.text + ':' }}
              </span>
              <el-tooltip v-if="cardInfo.title.model != ''" effect="dark" placement="right-start">
                <div slot="content">
                  {{ getModelName(cardInfo.title, dataInfo, 'title') }}
                </div>
                <span>
                  {{ getModelName(cardInfo.title, dataInfo, 'title') }}
                </span>
              </el-tooltip>
            </div>
            <div class="titleRight" :style="subTitleStyle">
              <span v-if="cardInfo.subTitle.text != ''">
                {{ cardInfo.subTitle.text + ':' }}
              </span>
              <el-tooltip v-if="cardInfo.subTitle.model != ''" effect="dark" placement="right-start">
                <div slot="content">
                  {{ getModelName(cardInfo.subTitle, dataInfo, 'subTitle') }}
                </div>
                <span>
                  {{ getModelName(cardInfo.subTitle, dataInfo, 'subTitle') }}
                </span>
              </el-tooltip>
            </div>
          </div>
          <div v-if="cardInfo.info.data.length > 0" :style="cardInfo.info.class != '' && cardInfo.info.class ? JSON.parse(cardInfo.info.class) : {}">
            <el-row :gutter="10">
              <el-col v-for="(item, index) in cardInfo.info.data" :key="index" :span="cardInfo.info.col">
                <div class="flex-row" :style="infoStyle">
                  <div class="left" v-if="item.text != ''">{{ item.text + ':' }}</div>
                  <div class="right">
                    <el-tooltip v-if="item.model != ''" effect="dark" placement="right-start">
                      <div slot="content">
                        {{ getModelName(item, dataInfo, 'info') }}
                      </div>
                      <span>
                        {{ getModelName(item, dataInfo, 'info') }}
                      </span>
                    </el-tooltip>
                  </div>
                </div>
              </el-col>
            </el-row>
            <div class="footer" v-if="btnList.length > 0">
              <div class="btns">
                <template v-for="(item, i) in btnList">
                  <template v-if="item.value == 'edit'">
                    <el-button size="mini" type="text" :key="i" :disabled="config.webType == 3 && [1, 2, 5].indexOf(data.flowState) > -1" @click="columnBtnsHandel(item.value, data)"
                      >{{ item.label }}
                    </el-button>
                  </template>
                  <template v-if="item.value == 'remove'">
                    <el-button
                      size="mini"
                      type="text"
                      :key="i"
                      class="JNPF-table-delBtn"
                      :disabled="config.webType == 3 && [1, 2, 3, 5].indexOf(dataInfo.flowState) > -1"
                      @click="columnBtnsHandel(item.value, dataInfo, item)"
                      >{{ item.label }}
                    </el-button>
                  </template>
                  <template v-if="item.value == 'detail'">
                    <el-button size="mini" type="text" :key="i" @click="columnBtnsHandel(item.value, dataInfo)">
                      {{ item.label }}
                    </el-button>
                  </template>
                  <template v-if="item.value == 'rowJournal'">
                    <el-button size="mini" type="text" :key="i" @click="columnBtnsHandel(item.value, dataInfo)">{{ item.label }}</el-button>
                  </template>
                </template>
              </div>
              <div class="check">
                <el-checkbox v-model="dataInfo.checked" @change="checkChange"></el-checkbox>
              </div>
            </div>
            <div v-else>
              <el-button size="mini" type="text">操作1 </el-button>
              <el-button size="mini" type="text">操作2 </el-button>
              <el-button size="mini" type="text">操作3 </el-button>
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <!-- 图文卡片布局(上下) -->
    <div v-else-if="cardInfo.value == 2" class="flex-card" :style="cardBoxStyle">
      <div v-for="(dataInfo, dataIndex) in cardList" :key="dataIndex" class="box-card">
        <el-card :style="cardStyle">
          <div :style="cardInfo.title.class != '' && cardInfo.title.class ? JSON.parse(cardInfo.title.class) : {}">
            <div :style="titleStyle">
              <span v-if="cardInfo.title.text != ''">
                {{ cardInfo.title.text + ':' }}
              </span>
              <el-tooltip v-if="cardInfo.title.model != ''" effect="dark" placement="right-start">
                <div slot="content">
                  {{ getModelName(cardInfo.title, dataInfo, 'title') }}
                </div>
                <span>
                  {{ getModelName(cardInfo.title, dataInfo, 'title') }}
                </span>
              </el-tooltip>
            </div>
          </div>
          <el-image class="img" :style="{ height: cardInfo.imgHeight + 'px' }" :src="imgVal(dataInfo)" alt="" />
          <div v-if="cardInfo.info.data.length > 0" :style="cardInfo.info.class != '' && cardInfo.info.class ? JSON.parse(cardInfo.info.class) : {}">
            <el-row :gutter="10">
              <el-col v-for="(item, index) in cardInfo.info.data" :key="index" :span="cardInfo.info.col">
                <div class="flex-row" :style="infoStyle">
                  <div class="left" v-if="item.text != ''">{{ item.text + ':' }}</div>
                  <div class="right">
                    <el-tooltip v-if="item.model != ''" effect="dark" placement="right-start">
                      <div slot="content">
                        {{ getModelName(item, dataInfo, 'info') }}
                      </div>
                      <span>
                        {{ getModelName(item, dataInfo, 'info') }}
                      </span>
                    </el-tooltip>
                  </div>
                </div>
              </el-col>
            </el-row>
            <div class="footer" v-if="btnList.length > 0">
              <div class="btns">
                <template v-for="(item, i) in btnList">
                  <template v-if="item.value == 'edit'">
                    <el-button size="mini" type="text" :key="i" :disabled="config.webType == 3 && [1, 2, 5].indexOf(data.flowState) > -1" @click="columnBtnsHandel(item.value, dataInfo)"
                      >{{ item.label }}
                    </el-button>
                  </template>
                  <template v-if="item.value == 'remove'">
                    <el-button
                      size="mini"
                      type="text"
                      :key="i"
                      class="JNPF-table-delBtn"
                      :disabled="config.webType == 3 && [1, 2, 3, 5].indexOf(dataInfo.flowState) > -1"
                      @click="columnBtnsHandel(item.value, dataInfo, item)"
                      >{{ item.label }}
                    </el-button>
                  </template>
                  <template v-if="item.value == 'detail'">
                    <el-button size="mini" type="text" :key="i" @click="columnBtnsHandel(item.value, dataInfo)">
                      {{ item.label }}
                    </el-button>
                  </template>
                  <template v-if="item.value == 'rowJournal'">
                    <el-button size="mini" type="text" :key="i" @click="columnBtnsHandel(item.value, dataInfo)">{{ item.label }}</el-button>
                  </template>
                </template>
              </div>
              <div class="check">
                <el-checkbox v-model="dataInfo.checked" @change="checkChange"></el-checkbox>
              </div>
            </div>
            <div v-else>
              <el-button size="mini" type="text">操作1 </el-button>
              <el-button size="mini" type="text">操作2 </el-button>
              <el-button size="mini" type="text">操作3 </el-button>
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <!-- 图文卡片布局(左右) -->
    <div v-else-if="cardInfo.value == 3" class="flex-card" :style="cardBoxStyle">
      <div v-for="(dataInfo, dataIndex) in cardList" :key="dataIndex" class="box-card">
        <el-card :style="cardStyle">
          <div class="flex">
            <div class="flex-left">
              <el-image class="img" :style="{ height: cardInfo.imgHeight + 'px' }" :src="imgVal(dataInfo)" alt="" />
            </div>
            <div class="flex-right">
              <div class="item-left" :style="cardInfo.info.class != '' && cardInfo.info.class ? JSON.parse(cardInfo.info.class) : {}">
                <div v-if="cardInfo.info.data.length > 0">
                  <el-row :gutter="10">
                    <el-col v-for="(item, index) in cardInfo.info.data" :key="index" :span="cardInfo.info.col">
                      <div class="flex-row" :style="infoStyle">
                        <div class="left" v-if="item.text != ''">{{ item.text + ':' }}</div>
                        <div class="right">
                          <el-tooltip v-if="item.model != ''" effect="dark" placement="right-start">
                            <div slot="content">
                              {{ getModelName(item, dataInfo, 'info') }}
                            </div>
                            <span>
                              {{ getModelName(item, dataInfo, 'info') }}
                            </span>
                          </el-tooltip>
                        </div>
                      </div>
                    </el-col>
                  </el-row>
                </div>
              </div>
              <div class="item-right">
                <div class="item-footer" v-if="btnList.length > 0">
                  <div class="item-btns" v-for="(item, i) in btnList" :key="i">
                    <div v-if="item.value == 'edit'">
                      <el-button size="mini" type="primary" :disabled="config.webType == 3 && [1, 2, 5].indexOf(data.flowState) > -1" @click="columnBtnsHandel(item.value, dataInfo)"
                        >{{ item.label }}
                      </el-button>
                    </div>
                    <div v-if="item.value == 'remove'">
                      <el-button size="mini" type="danger" :disabled="config.webType == 3 && [1, 2, 3, 5].indexOf(dataInfo.flowState) > -1" @click="columnBtnsHandel(item.value, dataInfo, item)"
                        >{{ item.label }}
                      </el-button>
                    </div>
                    <div v-if="item.value == 'detail'">
                      <el-button size="mini" type="warning" @click="columnBtnsHandel(item.value, dataInfo)">
                        {{ item.label }}
                      </el-button>
                    </div>
                    <div v-if="item.value == 'rowJournal'">
                      <el-button size="mini" type="success" @click="columnBtnsHandel(item.value, dataInfo)">{{ item.label }}</el-button>
                    </div>
                  </div>
                  <div class="check">
                    <el-checkbox v-model="dataInfo.checked" @change="checkChange"></el-checkbox>
                  </div>
                </div>
                <div class="item-footer" v-else>
                  <div class="item-btns">
                    <el-button size="mini" type="primary">编辑 </el-button>
                  </div>
                  <div class="item-btns">
                    <el-button size="mini" type="danger">删除 </el-button>
                  </div>
                  <div class="item-btns">
                    <el-button size="mini" type="warning">详情 </el-button>
                  </div>
                  <div class="check">
                    <el-checkbox disabled></el-checkbox>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script>
import { dataapiDetail, dataapiUrl } from '@/api/systemData/dataInterface'
import { comUrl } from '@/utils/define'
export default {
  props: {
    cardInfo: {
      type: Object,
      default: () => {
        return {}
      }
    },
    modelList: {
      type: Array,
      default: () => {
        return []
      }
    },
    cardData: {
      type: Array,
      default: () => {
        return []
      }
    },
    columnList: {
      type: Array,
      default: () => {
        return []
      }
    },
    btnList: {
      type: Array,
      default: () => {
        return []
      }
    },
    config: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  computed: {
    cardStyle() {
      let style = {
        backgroundColor: `${this.cardInfo.bgColor} !important`,
        border: `1px solid ${this.cardInfo.borderColor}`,
        boxShadow: `0 2px ${this.cardInfo.shadowPx}px 0 ${this.cardInfo.shadowColor} !important`
      }
      return { ...style }
    },
    cardBoxStyle() {
      let style = {
        '--gridTemplateColumns': this.cardInfo.rowNum,
        gap: `${this.cardInfo.gap}px`
      }
      return { ...style }
    },
    infoStyle() {
      let style = {
        fontSize: this.cardInfo.info.size + 'px',
        color: this.cardInfo.info.color
      }
      return { ...style }
    },
    titleStyle() {
      let style = {
        fontSize: this.cardInfo.title.size + 'px',
        color: this.cardInfo.title.color
      }
      return { ...style }
    },
    subTitleStyle() {
      let style = {
        fontSize: this.cardInfo.subTitle.size + 'px',
        color: this.cardInfo.subTitle.color
      }
      return { ...style }
    }
  },
  data() {
    return {
      cardList: []
    }
  },
  async created() {
    await this.columnList.forEach(async (item) => {
      if (item.propsUrl && item.propsUrl.length) {
        await dataapiDetail(item.params.propsUrl).then(async (res) => {
          let { data, code } = res.data
          if (code == 200) {
            await dataapiUrl(data.apiUrl).then((result) => {
              // console.log(result)
              if (this.cardInfo.title.model == item.propId) {
                this.cardInfo.title.apiData = result.data.data
                this.cardInfo.title.apiProps = item.params.apiProps
              }
              if (this.cardInfo.subTitle.model == item.propId) {
                this.cardInfo.title.apiData = result.data.data
                this.cardInfo.title.apiProps = item.params.apiProps
              }
              if (this.cardInfo.info.data.length > 0) {
                this.cardInfo.info.data.map((val) => {
                  if (val.model == item.propId) {
                    val.apiData = result.data.data
                    val.apiProps = item.params.apiProps
                    return val
                  }
                })
              }
            })
          }
          // if(res.)
        })
      }
      if (this.cardInfo.title.model == item.propId) {
        this.cardInfo.title.field = item.field
      }
      if (this.cardInfo.subTitle.model == item.propId) {
        this.cardInfo.subTitle.field = item.field
      }
      if (this.cardInfo.info.data.length > 0) {
        this.cardInfo.info.data.map((val) => {
          if (val.model == item.propId) {
            val.field = item.field
            return val
          }
        })
      }
    })

    setTimeout(() => {
      if (this.modelList.length > 0) {
        this.cardList = 10
      } else {
        if (this.cardData.length > 0) {
          this.cardList = this.cardData.map((item) => {
            return {
              ...item,
              checked: false
            }
          })
        }
      }
    }, 1000)
  },
  methods: {
    checkChange() {
      let records = this.cardList.filter((item) => item.checked == true)
      this.$parent.handleSelectionChange({ records: records })
    },
    imgVal(data) {
      if (this.modelList.length > 0) {
        return 'https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg'
      } else {
        if (this.cardInfo.imgModel && this.cardInfo.imgModel != '') {
          let imgFiledId = this.columnList.find((val) => val.propId == this.cardInfo.imgModel)?.field
          if (imgFiledId && data[imgFiledId] != '' && data[imgFiledId] != null && data[imgFiledId] != undefined) {
            let url = data[imgFiledId].split(',')[0]
            return comUrl + '/api/file/getFileInputStreamById/' + url
          }
        }
      }
    },
    columnBtnsHandel(value, data) {
      this.$parent.columnBtnsHandel(value, data)
    },
    getModelName(model, item, type) {
      if (this.modelList.length > 0) {
        let name = this.modelList.find((m) => m.__vModel__ == model.model)
        if (name != undefined) {
          return `$${name.__config__.label}$`
        } else {
          return ''
        }
      } else {
        if (type == 'title') {
          if (this.cardInfo.title.model == model.model) {
            if (model.apiData) {
              let result = model.apiData.find((val) => {
                if (val[model.apiProps.value] == item[this.cardInfo.title.field]) {
                  return val
                }
              })?.[model.apiProps.label]
              if (result != undefined) {
                return result
              } else {
                return item[this.cardInfo.title.field]
              }
            } else {
              return item[this.cardInfo.title.field]
            }
          }
        } else if (type == 'subTitle') {
          if (model.apiData) {
            let result = model.apiData.find((val) => {
              if (val[model.apiProps.value] == item[this.cardInfo.subTitle.field]) {
                return val
              }
            })?.[model.apiProps.label]
            if (result != undefined) {
              return result
            } else {
              return item[this.cardInfo.subTitle.field]
            }
          } else {
            return item[this.cardInfo.subTitle.field]
          }
        } else if (type == 'info') {
          let field = this.cardInfo.info.data.find((val) => val.model == model.model)?.field
          if (field != undefined) {
            if (model.apiData) {
              let result = model.apiData.find((val) => {
                if (val[model.apiProps.value] == item[field]) {
                  return val
                }
              })?.[model.apiProps.label]
              if (result != undefined) {
                return result
              } else {
                return item[field]
              }
            } else {
              return item[field]
            }
          } else {
            return ''
          }
        } else {
          return ''
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.flex-card {
  display: grid;
  grid-template-columns: repeat(var(--gridTemplateColumns), 1fr);
  padding: 10px 0;
  // grid-template-rows: repeat(var(--gridTemplateRows), 1fr);
  // justify-content: space-between;
}
@media screen and (max-width: 1400px) {
  .flex-card {
    grid-template-columns: 1fr 1fr 1fr;
  }
}
@media screen and (max-width: 1200px) {
  .flex-card {
    grid-template-columns: 1fr 1fr;
  }
}
.box-card {
  cursor: pointer;
  box-sizing: border-box;
  // margin-left: 15px;
  margin-top: 15px;
  padding-left: 15px;
}

.img {
  width: 100%;
  height: 150px;
}

.clearfix {
  width: 100%;
  display: flex;
  justify-content: space-between;
  .titleLeft {
    width: 50%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .titleRight {
    flex: 1;
    text-align: right;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
.flex-row {
  width: 100%;
  display: flex;
  flex-wrap: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 10px;
  .right {
    margin-left: 6px;
  }
}
.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 10px;
  // border-top: 1px solid #ebeef5;
  .btns {
    width: 90%;
  }
  .check {
    flex: 1;
    text-align: right;
  }
}

:deep(.el-card__header) {
  padding: 0 !important;
}
:deep(.el-card__body) {
  padding: 0 !important;
}

.flex-left {
  width: 40%;
}

.flex-right {
  flex: 1;
  display: flex;
  .item-left {
    width: 70%;
  }
  .item-right {
    flex: 1;
    .item-footer {
      height: 100%;
      display: flex;
      flex-direction: column;
      padding: 0 10px;
      position: relative;

      .item-btns {
        margin-top: 10px;
        text-align: right;
      }
      .check {
        position: absolute;
        right: 10px;
        bottom: 10px;
      }
    }
  }
}
</style>
