<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="1000px"
    @close="close"
  >
    <div class="manage-container">
      <div class="manage-input">
        <el-form ref="queryForm" :model="queryForm" label-width="100px">
          <vab-query-form>
            <el-form-item width="100" prop="type" label="素材类型">
              <el-select
                v-model="queryForm.type"
                size="small"
                placeholder="请选择素材类型"
              >
                <el-option
                  v-for="item in typeData"
                  :key="item.dataCode"
                  :label="item.dataValue"
                  :value="item.dataCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </vab-query-form>
        </el-form>
      </div>
      <div class="manage-button">
        <el-button size="small" @click="resetForm()">
          {{ $t('defalult.chongz') }}
        </el-button>
        <el-button type="primary" size="small" @click="search">
          {{ $t('defalult.cx') }}
        </el-button>
      </div>
    </div>
    <el-table
      ref="singleTable"
      v-loading="listLoading"
      :data="tableData"
      highlight-current-row
      style="width: 100%"
      @current-change="currentChange"
    >
      <el-table-column property="media_id" label="media_id"></el-table-column>
      <el-table-column
        property="name"
        :label="$t('trigger.mc')"
      ></el-table-column>
      <el-table-column property="img" label="素材">
        <template #default="{ row }">
          <div v-if="queryForm.type == 'news'">
            <div v-for="(item, index) in row.content.news_item" :key="index">
              <p>标题：{{ item.title }}</p>
              <el-link target="_blank" :href="item.url" type="primary">
                点击查看
              </el-link>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column property="img" label="选择">
        <template #default="{ row }">
          <el-button type="primary" size="mini" @click="currentChange(row)">
            选择
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.size"
      :total="total"
      style="text-align: right"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <material-detail ref="show"></material-detail>
  </el-dialog>
</template>

<script>
import { getMaterialType, getMaterialList } from '@/api/wechat';
import materialDetail from './material_detail';
export default {
  components: {
    materialDetail,
  },
  data() {
    return {
      tableData: [],
      typeData: [],
      dialogFormVisible: false,
      listLoading: false,
      title: '',
      currentRow: '',
      layout: 'total, sizes, prev, pager, next, jumper',
      background: true,
      total: 0,
      queryForm: {
        page: 1,
        size: 20,
        type: '',
      },
    };
  },
  methods: {
    showData(row) {
      this.$refs['show'].showMaterialData(row, this.queryForm.type);
    },
    currentChange(val) {
      this.currentRow = val;
      this.$emit('getMaterialId', val.media_id);
      this.dialogFormVisible = false;
    },
    //搜索
    search() {
      this.getMaterialListData();
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    selectData() {
      this.getMaterialTypeData();
      this.title = '素材列表';
      this.dialogFormVisible = true;
    },
    async getMaterialListData() {
      const { data } = await getMaterialList(this.queryForm);
      this.listLoading = true;
      this.tableData = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    async getMaterialTypeData() {
      const { data } = await getMaterialType();
      this.typeData = data;
      this.queryForm.type = data[0].dataCode;
      this.getMaterialListData();
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.getMaterialListData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.getMaterialListData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.getMaterialListData();
    },
  },
};
</script>

<style scoped></style>
