<template>
  <div class="ele-body">
    <el-card shadow="never">
      <search
        ref="search"
        @search="search"
        :options_groupId="dict.groupId"
        :options_factory="dict.factory"
      ></search>
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        cache-key="systemRoleTable5"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit('add')"
          >
            添加
          </el-button>
        </template>
        <template v-slot:groupId="{ row }">
          {{ showgroupName(row.groupId) }}
        </template>

        <template v-slot:parent="{ row }">
          {{ showgfactory(row, 2) }}
        </template>

        <template v-slot:factory="{ row }">
          {{ showgfactory(row, 1) }}
        </template>
  
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit('edit', row)"
          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此角色吗？"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <edit
      ref="edit"
      @done="done"
      :options_groupId="dict.groupId"
      :options_factory="dict.factory"
    ></edit>
  </div>
</template>
<script>
  import search from './components/search.vue';
  import edit from './components/edit.vue';
  import { getFactoryarea, deletefactoryarea } from '@/api/factoryModel';
  import { listOrganizations } from '@/api/system/organization';
  export default {
    components: {
      search,
      edit
    },
    data () {
      return {
        columns: [
          {
            width: 45,
            type: 'index',
            columnKey: 'index',
            align: 'center'
          },
          {
            prop: 'code',
            label: '车间编码'
          },
          {
            label: '车间名称',
            prop: 'name'
          },
          {
            label: '所属公司',
            prop: 'groupId',
            slot: 'groupId'
          },
          {
            label: '工厂编码',
            prop: 'parentCode',
            slot: 'parent'
           
          },
          {
            label: '所属工厂',
            prop: 'parentId',
            slot: 'factory'
          },
   
          {
            label: '详细地址',
            prop: 'extInfo.location'
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 220,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        dict: {
          groupId: [],
          factory: []
        }
      };
    },
    created () {
      this.getGs();
      this.getFactoryarea();
    },
    methods: {
      datasource ({ page, where, limit }) {
        return getFactoryarea({
          ...where,
          pageNum: page,
          size: limit,
          type: 3
        });
      },
      search (where) {
        this.$refs.table.reload({
          where: where,
          page: 1
        });
      },
      openEdit (type, row) {
        this.$refs.edit.open(type, row);
      },
      // 获取公司数据
      getGs () {
        listOrganizations().then((list) => {
          console.log(list);
          this.dict.groupId = list;
        });
      },
      // 回显公司名称
      showgroupName (id) {
        let result = this.dict.groupId.find((n) => n.id == id);
        if (result) {
          return result.name;
        } else {
          return '';
        }
      },
      // 回显工厂名称
      showgfactory (row, type) {
        let result = row.parent.find((n) => n.id == row.parentId);
        if (result && type == 1) {
          return result.name;
        } else if(result && type == 2) {
          return result.code;
        }
        else {
          return '';
        }
      },
      // 获取工厂数据
      getFactoryarea () {
        let par = {
          type: 1,
          size: 9999
        };
        getFactoryarea(par).then((res) => {
          this.dict.factory = res.list;
        });
      },
      remove (row) {
        deletefactoryarea(row.id)
          .then((message) => {
            this.$message.success(message);
            this.done();
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      },
      done () {
        this.$refs.search.search();
      }
    }
  };
</script>
