<template>
  <div class="app-container">
    <card-table :modalData="selectItemList"  labelWidth="120px" :searchParams="queryParams" @pagination="getList" @selection-change="handleSelectionChange" :loading="loading"
                :tableSearch="true" @columnSearch="handleQuery" @resetList="resetQuery">
      <template #operations="{ selections }">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['business:selectItem:add']"
            >{{ $t('common.add') }}</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              plain
              icon="el-icon-upload2"
              size="mini"
              @click="handleExport"
              v-hasPermi="['business:selectItem:export']"
            >{{ $t('common.export') }}</el-button>
          </el-col>
        </el-row>
      </template>

      <template #default="{ list }">
        <el-table-column label="No" align="center" :min-width="50"  fixed="left">
          <template slot-scope="scope">{{ scope.$index + 1 }}</template>
        </el-table-column>
<!--        <Column :label="$t('selectItem.paraCode')" align="center" prop="paraCode" :min-width="100"/>-->
        <Column :label="$t('selectItem.value')" align="center" prop="value" :min-width="150" :searchParams="queryParams" :showSearch="true" searchType="input"/>
        <Column :label="$t('selectItem.label')" align="center" prop="label" :min-width="150" :searchParams="queryParams" :showSearch="true" searchType="input"/>
        <Column :label="$t('selectItem.labelUS')" align="center" prop="labelUS" :min-width="150" :searchParams="queryParams" :showSearch="true" searchType="input"/>
        <Column :label="$t('selectItem.labelRU')" align="center" prop="labelRU" :min-width="150" :searchParams="queryParams" :showSearch="true" searchType="input"/>
        <Column :label="$t('selectItem.labelES')" align="center" prop="labelES" :min-width="150" :searchParams="queryParams" :showSearch="true" searchType="input"/>
        <Column :label="$t('selectItem.paraCode')" align="center" prop="paraCode" :min-width="150" :searchParams="queryParams" :showSearch="true" searchType="input"/>
        <Column :label="$t('selectItem.prarSource')" align="center" prop="prarSource" :min-width="100" :searchParams="queryParams" :showSearch="false">
          <template #default="{ row }">

            <el-button size="mini" type="text" v-if="row.prarSource"
                       icon="el-icon-view" @click="handleView(row)">
              {{ $t('common.view') }}</el-button>
            <span v-else>-</span>
          </template>
        </Column>
        <Column :label="$t('selectItem.sortNumber')" align="center" prop="sortNumber" :min-width="100" />
        <Column :label="$t('common.remark')" prop="remark" width="250px" />
        <Column :label="$t('common.operate')" :min-width="250" fixed="right" :showSearch="true"
                searchType="operate">
          <template #default="{ row }">
            <el-button size="mini" type="text"
                       icon="el-icon-edit" @click="handleUpdate(row)"
                       v-hasPermi="['business:selectItem:edit']">{{ $t('common.edit') }}</el-button>
            <el-button size="mini" type="text"
                       icon="el-icon-delete" @click="handleDelete(row)"
                       v-hasPermi="['business:selectItem:remove']">{{ $t('common.delete') }}</el-button>
          </template>
        </Column>
      </template>
    </card-table>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" top="5%" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="10.5vw">
        <el-row>
<!--          <el-col :span="12">-->
<!--            <el-form-item :label="$t('selectItem.paraCode')" prop="paraCode">-->
<!--              <el-input v-model="form.paraCode"  :disabled="true" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="12">
            <el-form-item :label="$t('selectItem.value')" prop="value">
              <el-input v-model="form.value" :placeholder="$t('common.pleaseInput')" :maxlength="100"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('selectItem.label')" prop="label">
              <el-input v-model="form.label" :placeholder="$t('common.pleaseInput')" :maxlength="100"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item :label="$t('selectItem.labelUS')" prop="labelUS">
              <el-input v-model="form.labelUS" :placeholder="$t('common.pleaseInput')" :maxlength="100"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('selectItem.labelRU')" prop="labelRU">
              <el-input v-model="form.labelRU" :placeholder="$t('common.pleaseInput')" :maxlength="100"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item :label="$t('selectItem.labelES')" prop="labelES">
              <el-input v-model="form.labelES" :placeholder="$t('common.pleaseInput')" :maxlength="100"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--            <el-form-item :label="$t('selectItem.sortNumber')" prop="sortNumber">-->
            <!--              <el-input-number style="width: 100%" :min="0" :max="9999" v-model="form.sortNumber" :placeholder="$t('common.pleaseInput')" />-->
            <!--            </el-form-item>-->

            <InputNumber :label="$t('paraLibrary.sortNumber')" :max="1000" :min="0" :precision=0 prop="sortNumber" :placeholder="$t('common.pleaseInput')" width="100%"/>

          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('selectItem.paraCode')" prop="paraCode">
              <el-input v-model="form.paraCode" :placeholder="$t('common.pleaseInput')" :maxlength="100"  />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <!-- 备注 -->
            <el-form-item :label="$t('common.remark')">
              <el-input v-model="form.remark" type="textarea" style="width: 100%;" maxlength="500"
                        show-word-limit :placeholder="$t('common.pleaseInput')"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('selectItem.prarSource')" prop="prarSource">
              <ImageUpload v-model="form.prarSource" :isShowTip="false" :limit="1" controllerUrl="/common/upload-paraPic"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" icon="el-icon-circle-check">{{ $t('common.confirm') }}</el-button>
        <el-button icon="el-icon-remove-outline" @click="cancel">{{ $t('common.cancel') }}</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="$t('selectItem.picView')" :visible.sync="openview" top="5%" width="50%" append-to-body>
      <ImagePreview :src="picUrl"  height="100%"/>
    </el-dialog>
  </div>
</template>

<script>
import {listItem, getItem, delItem, addItem, updateItem} from "../../../api/business/crm/paralibrary/selectItem";
import InputNumber from '../../../components/element/form/items/InputNumber'
import ImageUpload from "@/components/ImageUpload/index";
export default {
  name: "Item",
  components: { InputNumber,ImageUpload },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 选项表格数据
      selectItemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //  参数Id
      paraId: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        paraId: null,
        value: null,
        label: null,
        labelUS: null,
        labelRU: null,
        labelES: null,
        sortNumber: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules:{
        value:[
          {required:true, message: this.$t('common.pleaseInput'), trigger:'blur'}
        ],
        label:[
          {required:true, message: this.$t('common.pleaseInput'), trigger:'blur'}
        ],
        labelUS:[
          {required:true, message: this.$t('common.pleaseInput'), trigger:'blur'}
        ],
        labelRU:[
          {required:true, message: this.$t('common.pleaseInput'), trigger:'blur'}
        ],
        labelES:[
          {required:true, message: this.$t('common.pleaseInput'), trigger:'blur'}
        ],
        sortNumber:[
          {required:true, message: this.$t('common.pleaseInput'), trigger:'blur'}
        ],
      },
      picUrl:"",
      openview:false,
      paraCode:""
    };
  },
  created() {
    const paraId = this.$route.params && this.$route.params.id;
    this.paraId = paraId;
    const paraCode = this.$route.query && this.$route.query.paraCode;
    this.paraCode = paraCode;
    this.getList();
  },
  mounted() {
    //this.setPageTitle();
  },
  methods: {
    handleView(row){
      this.picUrl = row.prarSource;
      this.openview = true;
    },
    setPageTitle() {
      let title;
      title = this.$t('meta title');
      document.title = title;
    },
    /** 查询选项列表 */
    getList() {
      this.loading = true;
      this.queryParams.paraId = this.paraId;
      listItem(this.queryParams).then(response => {
        this.selectItemList = response.rows;
        this.total = response.total;
        this.queryParams.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        paraId: this.paraId,
        value: null,
        label: null,
        labelUS: null,
        labelRU: null,
        labelES: null,
        sortNumber: 0,
        prarSource:'',
        paraCode:""
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams= {
        pageNum: 1,
        pageSize: 10,
        paraId: null,
        value: null,
        label: null,
        labelUS: null,
        labelRU: null,
        labelES: null,
        sortNumber: null,
      },
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      //this.form.paraCode = this.paraCode
      this.open = true;
      this.title = this.$t('selectItem.addItem');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getItem(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('selectItem.editItem');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateItem(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.editSuccess'));
              this.open = false;
              this.getList();
            });
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.addSuccess'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(this.$t('common.isDelete')).then(function() {
        return delItem(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('common.deleteSuccess'));
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const fileName = '参数选项_' + this.parseTime(new Date(), '{y}{m}{d}') + '.xlsx'
      this.download('business/selectItem/export', {
        ...this.queryParams
      }, fileName)
    }
  }
};
</script>

<style lang="scss" scoped>
  ::v-deep .no-plus .el-upload--picture-card {
    display: none;
  }
</style>
