<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="材质" prop="material">
        <el-select v-model="queryParams.material" placeholder="材质" clearable style="width: 240px">
          <el-option v-for="item in material_data" :key="item.value" :label="item.name" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="工艺" prop="technique">
        <el-select v-model="queryParams.technique" placeholder="工艺" clearable style="width: 140px">
          <el-option v-for="item in technique_data" :key="item.value" :label="item.name" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="覆膜" prop="productMembrane">
        <el-select v-model="queryParams.productMembrane" placeholder="覆膜" clearable style="width: 140px">
          <el-option v-for="item in productMembrane_data" :key="item.value" :label="item.name" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="切割" prop="productSlice">
        <el-select v-model="queryParams.productSlice" placeholder="切割" clearable style="width: 140px">
          <el-option v-for="item in productSlice_data" :key="item.value" :label="item.name" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否加棉绳" prop="isRope" label-width="120px">
        <el-select v-model="queryParams.isRope" placeholder="是否加棉绳" clearable style="width: 140px">
          <el-option :key="true" label="加棉绳" :value="true" />
          <el-option :key="false" label="不加棉绳" :value="false" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否多面" prop="isMulti">
        <el-select v-model="queryParams.isMulti" placeholder="是否多面" clearable style="width: 140px">
          <el-option :key="true" label="多面" :value="true" />
          <el-option :key="false" label="单面" :value="false" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @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="Plus" @click="handleAdd" v-hasPermi="['product:clothingTag:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['product:clothingTag:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['product:clothingTag:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="材质" align="center" prop="material">
        <template #default="scope">
          <span>{{ material_data[scope.row.material]?.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工艺" align="center" prop="technique">
        <template #default="scope">
          <span>{{ technique_data[scope.row.technique]?.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="覆膜" align="center" prop="productMembrane">
        <template #default="scope">
          <span>{{ productMembrane_data[scope.row.productMembrane]?.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="切割" align="center" prop="productSlice">
        <template #default="scope">
          <span>{{ productSlice_data[scope.row.productSlice]?.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否加棉绳" align="center" prop="isRope">
        <template #default="scope">
          <template v-if="scope.row.isRope != null">
            <span v-if="scope.row.isRope">加棉绳</span>
            <span v-else>不加棉绳</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="是否多面" align="center" prop="isMulti">
        <template #default="scope">
          <template v-if="scope.row.isMulti != null">
            <span v-if="scope.row.isMulti">多面</span>
            <span v-else>单面</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['product:clothingTag:edit']">修改</el-button>
          <el-button link type="primary" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['product:clothingTag: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" v-model="open" append-to-body>
      <el-form ref="clothingTagForm" :model="form" :rules="rules" @submit.native.prevent label-width="120px">
        <el-form-item label="计算方式">
          <editTable :columnList="columnList" :datas="datas"></editTable>
        </el-form-item>
        <el-form-item label="材质" prop="material">
          <el-radio-group v-model="form.material">
            <el-radio v-for="item in material_data" :key="item.value" :label="item.value">{{ item.name }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="工艺" prop="technique">
          <el-radio-group v-model="form.technique">
            <template v-for="item in technique_data">
              <el-radio :key="item.value" :label="item.value" v-if="!item.require || item.require.material == form.material">{{ item.name }}</el-radio>
            </template>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="覆膜" prop="productMembrane" v-if="form.material == 0">
          <el-radio-group v-model="form.productMembrane">
            <el-radio v-for="item in productMembrane_data" :key="item.value" :label="item.value">{{ item.name }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="切割" prop="productSlice" v-if="form.material == 0">
          <el-radio-group v-model="form.productSlice">
            <el-radio v-for="item in productSlice_data" :key="item.value" :label="item.value">{{ item.name }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否加棉绳" prop="isRope" v-if="form.material == 1">
          <el-radio-group v-model="form.isRope">
            <el-radio :key="true" :label="true">加棉绳</el-radio>
            <el-radio :key="false" :label="false">不加棉绳</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="加棉绳计算方式" prop="ropeFormula" v-if="form.material == 1 && form.isRope">
          <el-input v-model="form.ropeFormula" placeholder="请输入参数键值" />
        </el-form-item>
        <el-form-item label="是否多面" prop="isMulti">
          <el-radio-group v-model="form.isMulti">
            <el-radio :key="true" :label="true">多面</el-radio>
            <el-radio :key="false" :label="false">单面</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="总计算方式" prop="formula">
          <el-input v-model="form.formula" placeholder="请输入参数键值" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="clothingTag">
import editTable from '@/components/editTable';

const { proxy } = getCurrentInstance();
const title = ref('');
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const dateRange = ref([]);
const total = ref(0);

const material_data = [
  { value: 0, name: '350克铜版纸' },
  { value: 1, name: '600克铜版纸' },
];

const technique_data = [
  { value: 0, name: '无' },
  { value: 1, name: '单面烫金' },
  { value: 2, name: '凹凸', require: { material: 0 } },
  { value: 3, name: '双面烫金', require: { material: 1 } },
  { value: 4, name: '单面烫金+凹凸' },
];
const productMembrane_data = [
  { value: 0, name: '不覆膜' },
  { value: 1, name: '双面覆哑膜' },
];
const productSlice_data = [
  { value: 0, name: '不操作' },
  { value: 1, name: '直角打孔' },
  { value: 3, name: '异形模切' },
];
const columnList = ref([
  { prop: 'area', label: '面积/数量' },
  { prop: '10', label: '10' },
  { prop: '20', label: '20' },
  { prop: '30', label: '30' },
  { prop: '40', label: '40' },
]);
const datas = ref([
  { area: '5*5', 10: '', 20: '', 30: '', 40: '' },
  { area: '10*10', 10: '', 20: '', 30: '', 40: '' },
]);

const dataList = ref([]);

const data = reactive({
  form: {
    material: 0,
    technique: null,
    productMembrane: null,
    productSlice: null,
    isRope: null,
    isMulti: false,
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    material: null,
    technique: null,
    productMembrane: null,
    productSlice: null,
    isRope: null,
    isMulti: null,
  },
  rules: {
    material: [{ required: true, message: '材质不能为空', trigger: 'blur' }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询服装吊牌列表 */
function getList() {
  loading.value = true;
  listTag(proxy.addDateRange(queryParams.value)).then(response => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryForm');
  handleQuery();
}
// 表单重置
function reset() {
  form.value = {
    id: null,
    material: 0,
    technique: 0,
    productMembrane: 0,
    productSlice: 1,
    isRope: null,
    isMulti: null,
  };
  datas.value = [
    { area: '5*5', 10: '', 20: '', 30: '', 40: '' },
    { area: '10*10', 10: '', 20: '', 30: '', 40: '' },
  ];
  columnList.value = [
    { prop: 'area', label: '面积/数量' },
    { prop: '10', label: '10' },
    { prop: '20', label: '20' },
    { prop: '30', label: '30' },
    { prop: '40', label: '40' },
  ];
  proxy.resetForm('clothingTagForm');
}

function cancel() {
  open.value = false;
  reset();
}
function submitForm() {
  proxy.$refs['clothingTagForm'].validate(valid => {
    if (valid) {
      let list = {};
      datas.value.map(item => {
        let json = { ...item };
        let key = json.area;
        delete json.area;
        delete json.row_index;
        list[key] = json;
      });
      form.value.compute = JSON.stringify(list);
      if (form.value.id != null) {
        updateTag(form.value).then(response => {
          proxy.$modal.msgSuccess('修改成功');
          open.value = false;
          getList();
        });
      } else {
        addTag(form.value).then(response => {
          proxy.$modal.msgSuccess('新增成功');
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = '添加服装吊牌';
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids;
  getTag(id).then(response => {
    let data = response.data.compute;
    data = JSON.parse(data);
    columnList.value = [{ prop: 'area', label: '面积/数量' }];
    datas.value = [];
    let first = true;
    Object.keys(data).map(item => {
      if (first) {
        Object.keys(data[item]).map(k => {
          columnList.value.push({ prop: k, label: k });
        });
        first = false;
      }
      datas.value.push({ area: item, ...data[item] });
    });
    form.value = response.data;
    open.value = true;
    title.value = '修改服装吊牌';
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除服装吊牌编号为"' + ids + '"的数据项？')
    .then(function () {
      return delTag(ids);
    })
    .then(() => {
      this.getList();
      proxy.$modal.msgSuccess('删除成功');
    })
    .catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'product/clothingTag/export',
    {
      ...this.queryParams,
    },
    `tag_${new Date().getTime()}.xlsx`,
  );
}
getList();
</script>
<style lang="less"></style>
