<template>
  <div>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      ref="crud"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu-left>
        <el-button type="primary" @click="addFn()">新增</el-button>
        <el-button type="danger" @click="handleDelete()">删除</el-button>
      </template>
      <template #menu-right> </template>
      <template #menu="scope">
        <el-button
          type="primary"
          text
          plain
          style="border: 0; background-color: transparent !important"
          size="medium"
          @click.stop="addFn(scope.row)"
          >修改</el-button
        >
      </template>
    </avue-crud>
    <addAnnmualEditDialog
      v-if="isOpen"
      :showDialog="isOpen"
      @closeDialog="closeDialog"
      :row-id="rowId"
    ></addAnnmualEditDialog>
  </div>
</template>
<script>
import { getYearData,deleteYear } from "@/api/qualityManagement/qualityObjective"
import addAnnmualEditDialog from './components/addAnnmualEditDialog.vue';
export default {
  components: {
    addAnnmualEditDialog,
  },
  data() {
    return {
      selectionList: [],
      loading:false,
      option: {
        height: 'auto',
        calcHeight: 32,
        tip: false,
        // size: 'medium',
        simplePage: true,
        searchShow: true,
        searchMenuSpan: 18,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: true,
        selection: true,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        addBtn: false,
        editBtnText: '修改',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        viewBtnText: '详情',
        labelWidth: 120,
        menuWidth: 70,
        dialogWidth: 640,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        columnSort: true,
        excelBtn: true,
        columnSort: true,
        index: false,
        showOverflowTooltip: true,
        searchLabelPosition: 'left',
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: 'left',
        gridBtn: false,
        searchMenuPosition: 'right',
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        align: 'center',
        column: [
          {
            label: '类别名称',
            prop: 'indicatorType',
            type:'select',
            sortable: true,
            filter: true,
            span: 24,
            search: true,
            headerAlign: 'center',
            // align: 'left',
            dicUrl:'/api/blade-system/dict/dictionary?code=IndicatorYear-IndicatorType',
            props:{
              label:'dictValue',
              value:'dictKey'
            }
          },
          {
            label: '年份',
            prop: 'year',
            sortable: true,
            filter: true,
            span: 24,
            search: false,
          },
          {
            label: '目标值',
            prop: 'target',
            sortable: true,
            filter: true,
            span: 24,
            search: false,
          },
          {
            label: '作业中心',
            prop: 'workstationId',
            // bind: 'bsWorkCenter.wcName',
            sortable: true,
            filter: true,
            span: 24,
            search: false,
            headerAlign: 'center',
            // align: 'left',
            dicUrl: '/api/blade-desk/BA/WorkCenter/listForSelect',
            props: {
              label: 'wcName',
              value: 'id',
            },
          },
          {
            label: '班组',
            prop: 'teamName',
            // bind: 'bsTeamSet.tsName',
            sortable: true,
            filter: true,
            span: 24,
            search: false,
            headerAlign: 'center',
            // align: 'left',
          },
        ],
      },
      form: {
        name: '',
      },
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      isOpen: false,
      rowId:'',
      query:{}
    };
  },
  methods: {
    addFn(row) {
      this.isOpen = true;
      this.rowId = row ? row.id : ''
    },
    closeDialog(val) {
      this.isOpen = false;
      if(val){
        this.onLoad()
      }
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad();
      done();
    },
    searchReset(){
      this.query = {}
      this.onLoad()
    },
    refreshChange() {
      this.onLoad();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteYear({
          ids:this.selectionList.map(item => item.id).join(',')
        }).then(res =>{
          if(res.data.code == 200){
            this.$message.success('删除成功')
            this.onLoad()
          }
        })
      });
    },
    // 多选
    selectionChange(list) {
      this.selectionList = list;
    },
    onLoad() {
      this.loading = true;
      getYearData({
        current:this.page.currentPage,
        size:this.page.pageSize,
        ...this.query
      }).then(res =>{
        this.data = res.data.data.records
        this.page.total = res.data.data.total
        this.loading = false
      })
    },
  },
};
</script>
<style lang="scss" scoped></style>
