<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="栏目名称" prop="captionName">
        <el-cascader
          :value="queryParams.captionName ? queryParams.captionName.split('/') : []"
          @input="v => $set(queryParams, 'captionName', v.join('/'))"
          :options="captionOptions"
          placeholder="请选择栏目名称"
          :props="{ value: 'label' }"
          clearable
          style="width: 300px;"
          @change="handleQuery"
        ></el-cascader>
        <!-- <el-input
          v-model="queryParams.captionName"
          placeholder="请输入栏目名称"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      </el-form-item>
      <el-form-item label="业务名称" prop="businessName">
        <el-input
          v-model="queryParams.businessName"
          placeholder="请输入业务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="中文描述" prop="titleChinese">
        <el-input
          v-model="queryParams.titleChinese"
          placeholder="请输入图片标题中文描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="英文描述" prop="titleEnglish">
        <el-input
          v-model="queryParams.titleEnglish"
          placeholder="请输入图片标题英文描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['official:index:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['official:index:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['official:index:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['official:index:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="indexList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="栏目名称" align="center" prop="captionName" />
      <el-table-column label="图片上传地址" align="center" prop="imageurl">
        <template slot-scope="{ row }">
          <img v-if="row.imageurl" :src="row.imageurl" alt="" style="width:50px;height:50px;object-fit:cover;">
        </template>
      </el-table-column>
      <!-- <el-table-column label="文件类型" align="center" prop="fileType" /> -->
      <el-table-column label="业务名称" align="center" prop="businessName" />
      <el-table-column label="英文名称" align="center" prop="businessNameEnglish" />
      <el-table-column label="中文描述" align="center" prop="titleChinese">
        <template slot-scope="{ row }">
          <div class="multiple-line-content-show">{{ row.titleChinese }}</div>
        </template>
      </el-table-column>
      <el-table-column label="英文描述" align="center" prop="titleEnglish">
        <template slot-scope="{ row }">
          <div class="multiple-line-content-show">{{ row.titleEnglish }}</div>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="是否置顶"
        align="center"
        prop="isIndex"
        :formatter="(row, column, cellValue, index) => ['否', '是'][cellValue]"
      /> -->
      <el-table-column label="排序" align="center" prop="orders" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['official:index:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['official:index:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改首页封面信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="栏目名称" prop="captionName">
          <el-cascader
            :value="form.captionName ? form.captionName.split('/') : []"
            @input="v => $set(form, 'captionName', v.join('/'))"
            :options="captionOptions"
            placeholder="请选择栏目名称"
            :props="{ value: 'label' }"
            style="width: 100%;"
            :disabled="Boolean(form.id)"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="业务名称" prop="businessName">
          <el-input v-model="form.businessName" placeholder="请输入业务名称" />
        </el-form-item>
        <el-form-item label="英文名称" prop="businessNameEnglish">
          <el-input v-model="form.businessNameEnglish" placeholder="请输入业务名称" />
        </el-form-item>
        <!-- <el-form-item label="文件类型" prop="fileType">
          <el-radio-group v-model="form.fileType">
            <el-radio label="0">图片</el-radio>
            <el-radio label="1">视频</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="图片上传地址" prop="imageurl">
          <ImageUploadLochost v-model="form.imageurl" :limit="1" :fileSize="10" />
        </el-form-item>
        <el-form-item label="中文描述" prop="titleChinese">
          <el-input
            type="textarea"
            v-model="form.titleChinese"
            placeholder="请输入中文描述"
            :autosize="{ minRows: 4, maxRows: 10 }"
          />
        </el-form-item>
        <el-form-item label="英文描述" prop="titleEnglish">
          <el-input
            type="textarea"
            v-model="form.titleEnglish"
            placeholder="请输入英文描述"
            :autosize="{ minRows: 4, maxRows: 10 }"
          />
        </el-form-item>
        <!-- <el-form-item label="是否置顶" prop="isIndex">
          <el-radio-group v-model="form.isIndex">
            <el-radio label="0">否</el-radio>
            <el-radio label="1">是</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="排序" prop="orders">
          <el-input-number v-model="form.orders" :min="0" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listIndex, getIndex, delIndex, addIndex, updateIndex } from "@/api/official/index";

export default {
  name: "Index",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 首页封面信息表格数据
      indexList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        imageurl: undefined,
        fileType: undefined,
        businessType: undefined,
        businessName: undefined,
        titleEnglish: undefined,
        titleChinese: undefined,
        isIndex: undefined,
        captionName: undefined,
        createUserId: undefined,
        updateUserId: undefined,
      },
      // 表单参数
      form: {
        imageurl: '',
        fileType: '0',
        isIndex: '0',
        captionName: '',
        orders: 0,
      },
      // 表单校验
      rules: {
        captionName: [
          { required: true, message: "栏目名称不能为空", trigger: "blur" }
        ],
      },
      captionOptions: [
        {
          label: '首页',
          children: [{ label: 'banner' }, { label: 'about' }, { label: 'businessBanner' }],
        },
        {
          label: '业务范围',
          children: [
            {
              label: '石油化工业务',
              children: [{ label: 'banner' }],
            },
            {
              label: '基建业务',
              children: [
                { label: 'banner' },
                { label: '主要内容' },
                { label: '项目描述' },
                { label: '项目案例' },
                { label: '技术支持' },
                { label: '重点案例' },
                { label: '重点案例主题' },
                { label: 'joinus' },
              ],
            },
            {
              label: '铁矿石业务',
              children: [
                { label: 'banner' },
                { label: '主要内容' },
                { label: '行业支持' },
                { label: '行业示例' },
                { label: '发表宣言' },
                { label: '保障行动' },
                { label: '专业团队' },
                { label: 'joinus' },
              ],
            },
            {
              label: 'LNG&LPG',
              children: [
                { label: 'banner' },
                { label: '主要内容' },
                { label: '燃气介绍' },
                { label: '供应链' },
                { label: '技术支持' },
                { label: 'joinus' },
              ],
            },
          ],
        },
        {
          label: '新闻动态',
          children: [
            { label: 'banner' },
            {
              label: '新闻详情',
              children: [{ label: 'banner' }]
            },
          ],
        },
        {
          label: '关于我们',
          children: [
            {
              label: '公司介绍',
              children: [{ label: 'banner' }, { label: 'content' }, { label: '使命' }, { label: '愿景' }, { label: '价值观' }]
            },
            {
              label: '社会责任',
              children: [{ label: 'banner' }, { label: 'content' }]
            },
            {
              label: '昊坤党建',
              children: [
                { label: 'banner' },
                { label: 'content' },
                {
                  label: '党建详情',
                  children: [{ label: 'banner' }]
                },
              ],
            },
            {
              label: '能源发展委员会',
              children: [{ label: 'banner' }, { label: 'content' }]
            },
          ],
        },
        {
          label: '招贤纳士',
          children: [
            {
              label: '美好昊坤',
              children: [{ label: 'banner' }]
            },
            {
              label: '社会招聘',
              children: [{ label: 'banner' }]
            },
          ],
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询首页封面信息列表 */
    getList() {
      this.loading = true;
      listIndex(this.queryParams).then(response => {
        this.indexList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        imageurl: '',
        fileType: '0',
        businessType: undefined,
        businessName: undefined,
        titleEnglish: undefined,
        titleChinese: undefined,
        isIndex: '0',
        captionName: '',
        createBy: undefined,
        createUserId: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateUserId: undefined,
        updateTime: undefined,
        orders: 0,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加图文信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getIndex(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改图文信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateIndex(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addIndex(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除首页封面信息编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delIndex(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('official/index/export', {
        ...this.queryParams
      }, `index_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
