<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="80px">
      <el-row>
        <el-col :span="6">
          <el-form-item label="产品编号" prop="code">
            <el-input
              v-model="queryParams.code"
              placeholder="请输入产品编号"
              clearable
              @keyup.enter.native="handleQuery"
              style="width:180px;"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="产品名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入产品名称"
              clearable
              @keyup.enter.native="handleQuery"
              style="width:180px;"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="规格" prop="spec">
            <el-input
              v-model="queryParams.spec"
              placeholder="请输入规格"
              clearable
              @keyup.enter.native="handleQuery"
              style="width:180px;"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="品牌" prop="brandCode">
            <el-select v-model="queryParams.brandCode" clearable placeholder="请选择品牌" style="width:180px;">
              <el-option
                v-for="item in brandIdOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="层次一" prop="level1">
            <el-select v-model="queryParams.level1" clearable placeholder="请选择层次一" style="width:180px;">
              <el-option
                v-for="item in level1Options"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="是否有效" prop="isValid">
            <el-select v-model="queryParams.isValid" clearable placeholder="请选择是否有效" style="width:180px;">
              <el-option
                v-for="item in isNotOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </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="['sfa:products: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="['sfa:products: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="['sfa:products: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="['sfa:products:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="产品id" align="center" prop="productId" />-->
      <el-table-column label="产品编号" align="center" prop="code" />
      <el-table-column label="产品名称" align="center" prop="name" :show-overflow-tooltip="true" width="350"/>
      <el-table-column label="序号" align="center" prop="seqence" width="70"/>
      <el-table-column label="品牌" align="center" prop="brandCode" >
        <template slot-scope="scope">
          <span>{{brandIdFormat(scope.row,scope.column)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="层次一" align="center" prop="level1" >
        <template slot-scope="scope">
          <span>{{level1Format(scope.row,scope.column)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="规格" align="center" prop="spec"/>
      <el-table-column label="是否有效" align="center" prop="isValid" >
        <template slot-scope="scope">
          <span>{{isValidFormat(scope.row,scope.column)}}</span>
        </template>
      </el-table-column>
      <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="['sfa:products:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            class="text-danger"
            v-hasPermi="['sfa:products: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="80%" append-to-body>
      <el-form ref="form" :model="form" :inline="true" :rules="rules" size="mini" label-width="90px">
        <el-form-item label="产品编号" prop="code" v-show="title=='添加产品信息'?false:true">
          <el-input v-model="form.code" disabled placeholder="请输入产品编号" style="width:193px;" />
        </el-form-item>
        <el-form-item label="产品名称" prop="name">
          <el-input v-model="form.name" clearable placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="规格" prop="spec">
          <el-input v-model="form.spec" clearable placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="品牌" prop="brandCode">
          <el-select v-model="form.brandCode" clearable placeholder="请选择品牌">
            <el-option
              v-for="item in brandIdOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="层次一" prop="level1">
          <el-select v-model="form.level1" clearable placeholder="请选择层次一">
            <el-option
              v-for="item in level1Options"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="序号" prop="seqence">
          <el-input v-model.number="form.seqence" clearable placeholder="请输入序号" />
        </el-form-item>
        <el-form-item label="是否有效" prop="isValid">
          <el-radio-group v-model="form.isValid">
            <el-radio :label="item.dictValue" :key="index" v-for="(item,index) in isNotOptions">{{item.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
<!--        <el-form-item label="删除标记" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" placeholder="请输入删除标记： 0-有效  2-无效" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProducts, getProducts, delProducts, addProducts, updateProducts } from "@/api/sfa/products";

export default {
  name: "Products",
  data() {
      var checkSeqence = (rule, value, callback) => {
          if (!value) {
              return callback(new Error('年龄不能为空'));
          }
          setTimeout(() => {
              if (!Number.isInteger(value)) {
                  callback(new Error('请输入数字值'));
              }else{
                  callback();
              }
          }, 1000);
      };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品信息表格数据
      productsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        brandCode: null,
        spec: null,
        level1: null,
        isValid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // code: [
        //   { required: true, message: "产品编号不能为空", trigger: "blur" }
        // ],
        name: [
          { required: true, message: "产品名称不能为空", trigger: "blur" }
        ],
        brandCode: [
          { required: true, message: "品牌不能为空", trigger: "change" }
        ],
        spec: [
          { required: true, message: "规格不能为空", trigger: "blur" }
        ],
        level1: [
          { required: true, message: "层次一不能为空", trigger: "change" }
        ],
        seqence: [
          { required: true, message: "序号不能为空", trigger: "blur" },
            { validator: checkSeqence, trigger: 'blur' }
        ],
        isValid: [
          { required: true, message: "是否有效不能为空", trigger: "change" }
        ],
        delFlag: [
          { required: true, message: "删除标记", trigger: "blur" }
        ],
      },
        isNotOptions:[],
        level1Options:[],
        brandIdOptions:[],
    };
  },
  created() {
      // 是否
      this.getDicts("is_not").then(response => {
          this.isNotOptions = response.data;
      });
      // 层次一
      this.getDicts("sfa_level1").then(response => {
          this.level1Options = response.data;
      });
      // 品牌
      this.getDicts("sfa_product_brand").then(response => {
          this.brandIdOptions = response.data;
      });
    this.getList();
  },
  methods: {
    /** 查询产品信息列表 */
    getList() {
      this.loading = true;
      listProducts(this.queryParams).then(response => {
        this.productsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否有效中文转换
    isValidFormat(row, column){
        return this.selectDictLabel(this.isNotOptions, row.isValid);
    },
      // 层次一中文转换
    level1Format(row, column){
        return this.selectDictLabel(this.level1Options, row.level1);
    },
    // 品牌中文转换
      brandIdFormat(row, column){
        return this.selectDictLabel(this.brandIdOptions, row.brandCode);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productId: null,
        code: null,
        name: null,
        brandCode: null,
        spec: null,
        level1: null,
        seqence: null,
        isValid: null,
        delFlag: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.productId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const productId = row.productId || this.ids
      getProducts(productId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.productId != null) {
            updateProducts(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProducts(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productIds = row.productId || this.ids;
      this.$modal.confirm('是否确认删除产品信息编号为"' + productIds + '"的数据项？').then(function() {
        return delProducts(productIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sfa/products/export', {
        ...this.queryParams
      }, `products_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
