<template>
  <el-dialog
      :title="'选择标签'"
      :visible.sync="dialogVisible"
      width="80%"
      append-to-body
    >
      <el-form :model="query" size="small" :inline="true" label-width="80px">
        <el-form-item label="标签名称">
          <el-input
            v-model="query.tagName"
            placeholder="请输入名称"
            clearable
            @keyup.enter.native="getList"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getList">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="refresh">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        :data="list"
        border
        style="width: 100%; margin-top: 16px;"
        @selection-change="handleSelection"
      >
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="标签名称" align="center" prop="tagName"/>
        <el-table-column label="是否需要拍照" align="center" prop="isPhoto">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isPhoto"/>
        </template>
      </el-table-column>
      <el-table-column label="拍照数量要求" align="center" prop="photoNum"/>
      <el-table-column label="照片存放目录" align="center" prop="saveDir">
        <template slot-scope="scope">
          {{ getCateName(scope.row.saveDir) }}
        </template>
      </el-table-column>
      <el-table-column label="标签说明" align="center" prop="tagDesc" />
      </el-table>
      <pagination
        :total="total"
        :page.sync="query.pageNum"
        :limit.sync="query.pageSize"
        @pagination="getList"
        style="margin-top: 16px;"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
</template>

<script>
import { pageListTag } from "@/api/system/tag";
import { listCata } from "@/api/gallery/cata";

export default {
  name: 'TagsDialog',
  dicts: ['sys_yes_no'],
  data() {
    return {
      cates: [],
      list: [],
      dialogVisible: false,
      total: 0,
      query: {
        tagName: '',
        pageNum: 1,
        pageSize: 10,
      },
      selections: [],
      loading: false,
    };
  },
  created() {

    listCata().then(res => {
      const list = res.data || [];
      this.cates = list;
    });
  },
  methods: {
    open() {
      this.dialogVisible = true;
      this.getList();
    },
    getCateName(saveDir) {
      const index = this.cates.findIndex(val => val.id == saveDir);
      return index > -1 ? this.cates[index].cataName : "";
    },
    getList() {
      this.loading = true;
      pageListTag(this.query).then(res => {
        this.list = res.rows || [];
        this.total = res.total || 0;
        this.selections = [];
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },
    refresh() {
      this.query = {
        tagName: '',
        pageNum: 1,
        pageSize: 10,
      };
      this.getList();
    },
    handleSelection(val) {
      this.selections = val;
    },
    confirm() {
      if (this.selections.length == 0) {
        this.$message.warning("请选择标签");
        return;
      }
      this.dialogVisible = false;
      this.$emit('confirm', this.selections);
    }
  }
}
</script>

<style scoped></style>